Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Python cv2.mixChannels()

Tags:

python

opencv

I was trying to convert this from C++ to Python, but it is giving different hue results.

In C++:

/// Transform it to HSV
cvtColor( src, hsv, CV_BGR2HSV );

/// Use only the Hue value
hue.create( hsv.size(), hsv.depth() );
int ch[] = { 0, 0 };
mixChannels( &hsv, 1, &hue, 1, ch, 1 );

I tried this in Python:

# Transform it to HSV
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)

# Use only the Hue value
hue = np.zeros(hsv.shape, dtype=np.uint8)
ch = [0] * 2
cv2.mixChannels(hsv, hue, ch)
like image 667
João Cartucho Avatar asked Feb 19 '17 16:02

João Cartucho


1 Answers

When you look in the documentation, you can see the C++ functions taking as arguments arrays (or vectors) of Mat as input and output.

C++: void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs)

C++: void mixChannels(const vector<Mat>& src, vector<Mat>& dst, const int* fromTo, size_t npairs)

Similarly, in Python you need to provide lists of np.array for both source and destination.

Code Sample

import cv2
import numpy as np
img = cv2.imread('cage.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Baseline for comparison
h,_,_ = cv2.split(hsv)

hue = np.zeros(hsv.shape, dtype=np.uint8)
cv2.mixChannels([hsv], [hue], [0,0])

print np.array_equal(h, hue[:,:,0])

Console Output

>python mix.py
True
like image 52
Dan Mašek Avatar answered Sep 21 '22 04:09

Dan Mašek