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
.
Use cv2. imread() for input. So, imread() will internally convert from rgb to bgr and imwrite() will do the opposite, all under the hood.
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.
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.
The cv::imwrite()
function correctly writes an image file if the input cv::Mat
is 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.)
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