Ask Your Question
0

Why setting U and V channels to 0 leads to displaying a Blue picture?

asked 2015-03-11 04:49:56 -0600

begueradj gravatar image

updated 2015-03-11 05:11:28 -0600

What I did:

  • I read a BGR image and convert it to YUV color space.

  • Then I set its U and V channels to 0

  • I display the image and I see it is all blue (as if I set R and B to 0, instead)

Questions:

  • Why setting U and V values to 0 leads the the G and R channels of the picture set to 0 also ?
  • Why the image is displayed in blue (BGR color space) instead of YUV color space ?

Code:

import cv2
import numpy as np

img=cv2.imread('rgb_pic.png')
yuv_img=cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
cv2.imwrite('yuv_pic',yuv_img)

cv2.namedWindow('YUV',cv2.WINDOW_AUTOSIZE)
cv2.imshow('YUV',yuv_img) # Displayed image is YUV, that is ok
cv2.waitKey(0)
cv2.destroyAllWindows()

im=yuv_img # Copy the YUV image to an other one
for in in range(im.shape[0]):
    for j in range(im.shape[1]):
       im[i,j,1]=0 # Set U to 0
       im[i,j,2]=0 # Set V to 0
cv2.namedWindow('Result',cv2.WINDOW_AUTOSIZE)
cv2.imshow('Result',im) # Displayed image is totally BLUE :(
cv2.waitKey(0)
cv2.destroyAllWindows()

Results:

yuv_img displayed like this (that is ok for me):

enter image description here

im displayed like this (I do not understand why it is blue):

enter image description here

Regards, from BEGUERADJ

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-03-11 05:13:59 -0600

kbarni gravatar image

updated 2015-03-11 05:16:40 -0600

Because imshow displays the image az a BGR image.

In fact the im variable is a WxHx3 matrix, and OpenCV doesn't know if it's RGB, HSV, YUV or something else. so it considers that it's BGR. The fact that you set to 0 the second and third channels basically erases the green and red channels. That's why your image turned blue.

To display it correctly, you have to convert it back to BGR color space:

cvtColor(im,result,COLOR_YUV2BGR);
imshow(result);

(add the cv2. before the expressions for python)

It should show correctly the grayscale image.

edit flag offensive delete link more

Comments

come back to you again if I may: I did exactly what you said. By curiosity, I run print im[130,290,1] and I get a value other than 0 whereas I am supposed to get 0 since I set it to 0 previously. Why is this ?

begueradj gravatar imagebegueradj ( 2015-03-12 05:40:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-11 04:49:56 -0600

Seen: 1,637 times

Last updated: Mar 11 '15