Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When open an image using imread with no second argument, what is the color mode? BGR or RGB?

Tags:

c++

opencv

The problem is when I need to convert it to HSV, CV_BGR2HSV and CV_RGB2HSV give me different results: So I really need to know what is the order of color when open by imread or how to force the imread to open an image in any particular order.

enter image description here

like image 929
Max Avatar asked May 07 '13 02:05

Max


People also ask

Is Imread BGR or RGB?

imread() it interprets in BGR format by default.

What is BGR to RGB?

What's the Difference between RGB versus BGR? The main difference between RGB versus BGR is the arrangement of the subpixels for Red, Green, and Blue. RGB is arranged like that, but BGR is essentially in reverse with no adverse effect on color vibrancy and accuracy.

What is BGR color?

When the image file is read with the OpenCV function imread() , the order of colors is BGR (blue, green, red). On the other hand, in Pillow, the order of colors is assumed to be RGB (red, green, blue).

Is cv2 Imread RGB?

IMREAD_COLOR reads the image with RGB colors but no transparency channel. This is the default value for the flag when no value is provided as the second argument for cv2.


1 Answers

The OpenCV docs for imread state that by default for 3-channel color images the data is stored in BGR order, e.g. in your Mat, the data is stored as a 1D unsigned char pointer, such that any given color pixel at index px_idx is 3 elements in order, with [px_idx + 0]: blue channel, [px_idx + 1]: green channel, [px_idx + 2]: red channel

Note In the case of color images, the decoded images will have the channels stored in B G R order.

You have some (limited) control over the color type via the flag parameter you pass to imread, although you can't specify the channel ordering (you should assume all color images will be BGR)

CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one

CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

Or more simply,

>0 Return a 3-channel color image. (same as CV_LOAD_IMAGE_COLOR)

=0 Return a grayscale image. (same as CV_LOAD_IMAGE_GRAYSCALE)

<0 Return the loaded image as is (with alpha channel). (same as CV_LOAD_IMAGE_ANYDEPTH)

like image 63
alrikai Avatar answered Sep 23 '22 04:09

alrikai