Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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";
}