Is it possible to find the color percentage of an Image Using OpenCv?
thanx in advance to all friends who will participate.
add a comment
thanx in advance to all friends who will participate.
This actually really depends on what you define as the color percentage. Here is an example that calculates how much of your image is red, green and blue. You can also modify it to determine how much of the total red, green and blue spectrum is covered.
#include "core/core.hpp"
#include "highgui/highgui.hpp"
#include "imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main( int argc, char** argv )
{
if (argc != 2) {
cout << "No image input" << endl;
return -1;
}
cv::Mat image;
image = cv::imread(argv[1]);
if( !image.data ) {
cout << "Image read fail\n";
return -1;
}
cv::Scalar sums;
sums = cv::sum(image);
double totalSum = sums[0] + sums[1] + sums[2];
cout << "R: " << sums[2]/totalSum * 100 << "% \n";
cout << "G: " << sums[1]/totalSum * 100 << "% \n";
cout << "B: " << sums[0]/totalSum * 100 << "% \n";
}
Asked: 2013-10-11 23:54:01 -0600
Seen: 2,186 times
Last updated: Oct 12 '13