Ask Your Question
1

Is it possible to find the color percentage of an Image Using OpenCv?

asked 2013-10-11 23:54:01 -0600

Js Bajwa gravatar image

thanx in advance to all friends who will participate.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2013-10-12 03:46:19 -0600

Moster gravatar image

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";
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-10-11 23:54:01 -0600

Seen: 2,154 times

Last updated: Oct 12 '13