Ask Your Question

Changing color space in opencv without changing color of the image

asked 2014-07-21 14:26:39 -0500

mainul gravatar image

updated 2014-07-21 14:28:33 -0500

Hi, I am working on a project where I read an image load the data into Mat data type. The I do some operations on it. All my operations are done assuming the color space is RGB (BGR as opencv stores in that way). Everuything is working fine. I was doing experiment on converting the output image to YUV format. But when I transform the output image from BGR2YUV using the following command cvtColor(img,out,CV_RGB2YCrCb); I found that the resulting image color is changed completely.

How can I change the color space from RGB to YUV without changing the colors in the image? For example, my output RGB image is green. WHen I convert this to YUV format and show the resulting image I found it blue and NOT green. I want a way to convert so that the output also become green.

edit retag flag offensive close merge delete



"WHen I convert this to YUV format and show the resulting image I found it blue and NOT green."

don't worry, that's the expected result. if you convert it back, it will look all normal again.

berak gravatar imageberak ( 2014-07-22 11:03:57 -0500 )edit

Actually, like Pedro already suggested, you are misinterpreting the color channels of OpenCV. OpenCV assumes and uses BGR color channels, while you are focussing on RGB color channels. The mixup is completely normal :)

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-23 04:05:19 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-07-22 09:35:52 -0500

If you are working with BGR images, you have to call cvtColor like this:


instead of what you are doing.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-07-21 14:26:39 -0500

Seen: 269 times

Last updated: Jul 22 '14