1

How can I print whether a cvMat is RGB, BGR or GRAY? The reason I need to do this is because I need to convert an image to GRAY and have been given errors such as this when trying CV_RGB2GRAY

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor

Clip
  • 3,018
  • 8
  • 42
  • 77
  • 1
    "whether a cv::Mat is RGB, BGR" - unfortunately, that information is nowhere saved. you can tell the number of channels and their type ( see answer below), but not the semantics. – berak Apr 19 '14 at 08:36

1 Answers1

5

You need to know the number of channels input image has. cvtColor is expecting it to have 3 or 4 channels. Use channels() to determine what you have. The following information is taken directly from the answer in: Can I determine the number of channels in cv::Mat Opencv

cv::Mat img(1,1,CV_8U,cvScalar(0));
std::cout<<img.channels();

Output:

1

which is the number of channels.

This error also occurs when the image is empty. You can check for this using:

img.empty();

Source: openCV Error: Assertion failed (scn == 3 || scn == 4)

Community
  • 1
  • 1
Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79