How cv2.imshow() function works in BGR model?

Viewed 550

In RGB color model, cv2.imshow() works by bgr order,when use cv2.imshow() to load a image,and show it in matplotlib.plot.show() function,it goes wrong cause plot.show() use rgb order,it can be solved by reverse the channels of b,g,r in image,but when i make RGB color model transformation into other color model,like HSV,YUV,YCbCr,when use plot.show() it still be different to cv2.show().So when RGB->HSV,YUV,YCbCr color model,does cv2.show() still works like v,s,h maps to b,g,r?And plt.show() use the order h,s,v maps to r,g,b?No matter what color model use cv2.show() and plot.show(),it just use b,g,r and r,g,b orders maps to changed channels?Like v,s,h and h,s,v in HSV,v,u,y and y,u,v in YUV?

img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV)
plt.show(img_HSV )
cv2.show(img_HSV )
plt.show(img_HSV [:,:,::-1])

the result in line 1 code is different with line2,but line 3 is same as line2. So plt.show works by h,s,v maps to r,g,b and cv2.show works by v,s,h maps to b,g,r?

2 Answers

plt.show(img_HSV ) assumes the input is RGB cv2.show(img_HSV ) assumes the input is BGR

So if you give input in a different format they take the channel as they assume anyway. For example, cv2.imshow(img_HSV ) method will take the Hue value as Blue so there will be a different image representation.

So just convert them back to RGB or BGR for imshow() functions after your processes done with the other color spaces.

I assume you mean cv2.imshow (and plt.imshow) as in your title, right? Most of the time (and in all your examples) you pass an image with three channels to the functions plt.imshow and cv2.imshow.

Those are numpy arrays and the data type (uint8, float32, ect) is the only information you implicitly pass along. So those functions simply don't know which color encoding you are currently using. They simply interpret it as RGB (matplotlib) respectively BGR (OpenCV). So the second channel will always be interpreted as green and the other ones as red and blue respectively. That's all they do.

PS: If you pass a single-channel array you'll get a gray image.

PPS: Consider the range of the values in your numpy array. For example if you are passing float the values should be in the interval [0-1], whereas with uint8 it's [0-255] for matplotlib.

Related