0

I would like to know if the image I read is a Black and white or colored image. I use Opencv for all my process. In order to detect it, I currently read my image, convert it from BGR2GRAY and I compare Histogram of the original(read as BGR) to the histogram of the second (known as B&W).

In pseudo code this looks like that:

cv::Mat img = read("img.png", -1);
cv::Mat bw = cvtColor(img.clone(), bw, CV_BGR2GRAY);

if (computeHistogram(img) == computeHistogram(bw))
     cout << "Black And White !"<< endl;

Is there a better way to do it ? I am searching for the lightest algo I can Implement and best practices.

Thanks for the help.

Edit: I forgot to say that I convert my images in HSL in order to compare Luminance Histograms.

bjou
  • 1,107
  • 1
  • 7
  • 19
Poko
  • 792
  • 2
  • 8
  • 24

1 Answers1

2

Storing grayscale images in RGB format causes all three fields to be equal. It means for every pixel in a grayscale image saved in RGB format we have R = G = B. So you can easily check this for your image.

Kourosh
  • 274
  • 1
  • 6