Ask Your Question

Why the cvtColor don't work?

asked 2017-09-08 02:26:46 -0500

yode gravatar image

updated 2017-09-08 02:48:37 -0500

I am confusing,why this code don't work

mask = imread("mask.jpg")>200;
cvtColor(mask, red, COLOR_GRAY2BGR);

It cannot run normally and will pop an abort window.But if I specify the color space when load the mask like

mask1 = imread("mask.jpg",IMREAD_GRAYSCALE)>200;
cvtColor(mask1, red, COLOR_GRAY2BGR);

Then the code is work well. I think this problem is result to the mask is not grayscale image. But how to know the color space of a image in OpenCV?

This is my image mask.

edit retag flag offensive close merge delete


Check number of channels : Mat::channels

Ziri gravatar imageZiri ( 2017-09-08 02:32:02 -0500 )edit

@Ziri Such as the channels is 3,but it can be BGR,RGB,HSV or other something..

yode gravatar imageyode ( 2017-09-08 02:34:21 -0500 )edit

Yes. you' ll be able to tell if image is (GRAY/BINARY) or (Color). You can't tell if its RGB , HSV ..... using Opencv. if you load color image its BGR by default.

Ziri gravatar imageZiri ( 2017-09-08 02:39:37 -0500 )edit

In this case you have to reformulate your question as well : How to find color space of an image ?

Ziri gravatar imageZiri ( 2017-09-08 02:45:18 -0500 )edit

@Ziri You think the color space of the mask is grayscale or binary?If I want to change it into BGR.I should use COLOR_GRAY2BGR or COLOR_Binary2BGR´╝čAnd I cannot find the COLOR_Binary2BGR

yode gravatar imageyode ( 2017-09-08 02:48:02 -0500 )edit

cvtColor crashes because you're trying to convert color to color using COLOR_GRAY2BGR . if you use COLOR_GRAY2BGR , source image should be (GRAY or Binary 1 channel) and you can make sure using Mat::channels. What matters for cvtColor is the number of channels not color space.

Ziri gravatar imageZiri ( 2017-09-08 02:52:14 -0500 )edit

1 answer

Sort by ┬╗ oldest newest most voted

answered 2017-09-08 02:57:32 -0500

LBerger gravatar image

updated 2017-09-08 03:03:01 -0500

I'm not sure that mask.jpg is grayscale image. Your link is a png with 24 bits depth image (some site change image format).

Now in jpg format I don't think it's possible to use a specific color space. When you use imread wit a jpg file you will get a three channel image : imread("mask.jpg",IMREAD_UNCHANGED )

three channels means B G R. (of course some users can save YUV as a jpg and everything is broken but you cannot know this before display image)

Now your code is wrong :

mask = imread("mask.jpg")>200;
cvtColor(mask, red, COLOR_GRAY2BGR);

because in imread second parameter default value is IMREAD_COLOR and if image is a grayscale image then image will be convert in color.

edit flag offensive delete link more


But after I >200,it just have one channel as this document.It is BGR still?

yode gravatar imageyode ( 2017-09-08 03:02:51 -0500 )edit

I think after I >200, mask just have one channel as this documentation.It is BGR still?

yode gravatar imageyode ( 2017-09-08 03:04:08 -0500 )edit

operator > apply to each channel : you will get an image with three channels

LBerger gravatar imageLBerger ( 2017-09-08 03:05:25 -0500 )edit

I don't think.Note the text single channel,please check here

yode gravatar imageyode ( 2017-09-08 03:08:59 -0500 )edit

No doc is wrong!

Mat xxx=imread("g:/lib/opencv/samples/data/lena.jpg");
cout<<" lena channels "<<xxx.channels()<<endl;
Mat www=xxx>200;
cout << " lena channels after >" << www.channels() << endl;

 lena channels 3
 lena channels after >3

You can post an issue (you will need a github account) and feel free to make a Pull Request

LBerger gravatar imageLBerger ( 2017-09-08 03:14:10 -0500 )edit

You are right..I have checked just now..

yode gravatar imageyode ( 2017-09-08 03:15:08 -0500 )edit

Sorry for my poor English, and If you help to post that issue I will apreaciate. And what meaning of your PR? I cannot check it in my dictionary.

yode gravatar imageyode ( 2017-09-08 03:20:18 -0500 )edit

@LBerger@yode thanks. i opened an issue about lackness of documentation

sturkmen gravatar imagesturkmen ( 2017-09-08 13:55:50 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-09-08 02:26:46 -0500

Seen: 1,136 times

Last updated: Sep 08 '17