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 !
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);
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With