Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert Gray to BGR in OpenCV

Tags:

c

opencv

Following is a snippet of a simple code to convert a grayscale image to RGB using cvCvtColor function in OpenCV.

input = cvLoadImage("test.jpg", CV_LOAD_IMAGE_GRAYSCALE);
output = cvCreateImage(cvSize(input->width, input->height), 8, 3);
cvCvtColor(input, output, CV_GRAY2BGR);
cvSaveImage("output.jpg", output);

Where test.jpg is a grayscale image.

But it doesn`t seem to be working properly, because output.jpg i.e the final output also is grayscale, same as the input itself. Why so ?

Any kind of help would be highly appreciated. Thanks in advance !

like image 414
Koustav Avatar asked Oct 06 '12 16:10

Koustav


People also ask

How do I convert gray to BGR in OpenCV?

Following is a snippet of a simple code to convert a grayscale image to RGB using cvCvtColor function in OpenCV. input = cvLoadImage("test. jpg", CV_LOAD_IMAGE_GRAYSCALE); output = cvCreateImage(cvSize(input->width, input->height), 8, 3); cvCvtColor(input, output, CV_GRAY2BGR); cvSaveImage("output. jpg", output);

Is cv2 BGR or RGB?

OpenCV uses BGR image format. So, when we read an image using cv2. imread() it interprets in BGR format by default. We can use cvtColor() method to convert a BGR image to RGB and vice-versa.

How do I convert BGR to RGB in cv2?

Convert BGR and RGB with OpenCV function cvtColor() Various color spaces such as RGB, BGR, HSV can be mutually converted using OpenCV function cvtColor() . Refer to the following document for the value to be specified for the parameter code . When code is cv2. COLOR_BGR2RGB , BGR is converted to RGB.


1 Answers

I think you misunderstand cvCvtColor. cvCvtColor(input, output, CV_GRAY2BGR); will change single channel image to 3-channel image. But if you look at the image, it will still look like a gray image because, for example, a gray pixel of 154 has been converted to RGB(154,154,154).

When you convert color image to gray image, all color information will be gone and not recoverable. Therefore you can't really make a gray image to visibly color image without additional information and corresponding operations.

like image 112
Tae-Sung Shin Avatar answered Nov 01 '22 22:11

Tae-Sung Shin