1 | initial version |
You can use the function countNonZero and divide by the total number of pixels of image. A example of use:
vector<Mat> channels;
split(hsv_img,channels);
Mat red, blue, green;
inRange(channels[0], Scalar(0), Scalar(10), red); // red
// ... do the same for blue, green, etc only changing the Scalar values and the Mat
double image_size = hsv_img.cols*hsv_img.rows;
double red_percent = ((double) cv::countNonZero(red))/image_size;
But it could be not optimal depending of the application (for example, if you need to scan lots of image). Anyway, you can use it to compare the values.