gnoejh
gnoejh

Reputation: 335

How to determine the number of input image channels?

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?

Upvotes: 0

Views: 759

Answers (1)

Bull
Bull

Reputation: 11941

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);

Upvotes: 1

Related Questions