Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV how to imwrite a RGB image

Tags:

c++

opencv

How do I write a RGB image with the function cv::imwrite()? So far all my attempts lead into writing a BGR image instead.

My matrix object is a cv::Mat.

like image 732
Arkerone Avatar asked Apr 29 '12 18:04

Arkerone


People also ask

Does cv2 Imwrite save RGB or BGR?

Use cv2. imread() for input. So, imread() will internally convert from rgb to bgr and imwrite() will do the opposite, all under the hood.

How do I convert RGB to BGR in OpenCV?

We can use cvtColor() method to convert a BGR image to RGB and vice-versa. Parameter: cv2. COLOR_BGR2RGB – BGR image is converted to RGB.

How does cv2 Imwrite work?

cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.


1 Answers

The cv::imwrite() function correctly writes an image file if the input cv::Matis in BGR order (which is the case if you let OpenCV create it). If you created the image by yourself, you have to convert the color ordering before, for example by calling as suggested by bamboove cv::cvtColor(in, out, CV_RGB2BGR); if you created an RGB image.

(Pay attention to the color conversion code, it's slightly different from bamboon's.)

like image 59
sansuiso Avatar answered Nov 15 '22 06:11

sansuiso