0

For reading an image file, I have to use either of

Mat img = imread(file,CV_LOAD_IMAGE_COLOR);

or

Mat img = imread(file,CV_LOAD_IMAGE_GRAYSCALE);

This means I have to know in advance whether the file contains a color or mono image. Isn't there a way to know the number of channels in advance so that I can apply the image read according to the number of channels?

gnoejh
  • 325
  • 4
  • 16

1 Answers1

1

According to the imread documentation, you should use <0 Return the loaded image as is (with alpha channel).

In highui_c.h there is this definition:

CV_LOAD_IMAGE_UNCHANGED  =-1,

Oddly CV_LOAD_IMAGE_UNCHANGED is not mentioned in the imread documentation, but is used in one of the OpenCV tutorials. This will do what you want:

Mat img = imread(file, CV_LOAD_IMAGE_UNCHANGED);
Bull
  • 11,771
  • 9
  • 42
  • 53
  • Mr. B, thanks for the answer. It works. I found that some black and white image returns with 3, img.channel(). That kind images might be defective, not properly converted from color to mono. – gnoejh Jul 23 '14 at 07:17