0

I want to calculate which color (bgr) presents more in an image.

I know I should use:

calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);

But how do I read the value of each color detected in this lines (without comparing) - how do I know the value detected in the whole image?

Thank you.

Danaro
  • 61
  • 1
  • 2
  • 11
  • Take a look [here](http://stackoverflow.com/a/35482205/5008845), specifically the section _"Get the different colors of an image"_, and the function `getPalette` – Miki Jun 30 '16 at 12:59

1 Answers1

0

If you want to know the dominating colour in an entire image then a simpler method would be to just go through the image with a nested loop and sum up the individual channel values. An example of the code MAY look like this:

float blue_sum = 0;
float green_sum = 0;
float red_sum = 0;
for(unsigned int y = 0; y < your_image_mat.rows; y++)
  for(unsigned int x = 0; x < your_image_mat.cols; x++)
  {
    blue_sum += (float)your_image_mat.at<cv::Vec3b>(y, x)[0];
    green_sum += (float)your_image_mat.at<cv::Vec3b>(y, x)[1];
    red_sum += (float)your_image_mat.at<cv::Vec3b>(y, x)[2];
  }
//Then just compare which of the sum values is largest

I'm not too sure what your Mat type is, I'm assuming it's a a fairly standard RGB Mat, you may have to tweek the code slightly to work with whatever Mat you're using.

ZoSal
  • 561
  • 4
  • 21
  • This **won't** give you the most present color, but just an indication on which is the most used channel, which is not useful here. For example, an image composed only by pure red and green color, will tell you that the most present color is yellow. This is basically the same as picking to the _mean_ color of the image, and take the highest channel – Miki Jun 30 '16 at 13:02
  • @Miki Ah yes, woops. Bit of a thought failure there, what I've described is actually for finding the most dominant channel like you said. When I was writing the answer I had that "dominant channel" idea in my head so firmly that I didn't re-read the question. Sorry for that. – ZoSal Jul 01 '16 at 00:16