When an image is opened using imread without a second argument, what is the color mode? BGR or RGB?

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 color order when opening imread or how to make imread open the image in any particular order.

enter image description here

+7
source share
2 answers

OpenCV docs for imread indicate that by default for three-channel color images, data is stored in BGR order, for example. in your Mate, the data is saved as a 1D unsigned char pointer, so any given color pixel with the index px_idx has 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 channels stored in the order of BG R.

You have (limited) control over the color type using the flag parameter that you pass to imread , although you cannot specify the order of the channels (you must assume that all color images will be BGR)

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

CV_LOAD_IMAGE_COLOR - if set, always convert the image to color

CV_LOAD_IMAGE_GRAYSCALE - if set, always convert the image to grayscale text

Or easier

>0 Return a 3-channel color image. (similar to CV_LOAD_IMAGE_COLOR)

=0 Return a grayscale image. (similar to CV_LOAD_IMAGE_GRAYSCALE)

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

+10
source

Do you display images using imshow() ?

imshow() does not actually know the color space, but will print images as BGR , even if you convert them to a different format. This is why you get weird results.

To convince yourself, try converting the image from RGB to BGR and print it with imshow() , then do the same by switching from BGR to RGB:

 cv::cvtColor(src, dst, CV_RGB2BGR); imshow("RGB2BGR", dst); cv::cvtColor(src, dst, CV_BGR2RGB); imshow("BGR2RGB", dst); 

Then your example proves that converting the BGR image to HSV will not result in the same matrix as converting the RGB image to HSV.

+4
source

All Articles