Ask Your Question

Get the color percentage

asked 2013-09-10 19:14:49 -0500

yassine gravatar image


I'm using Delphi XE4 with openCV. I tried to get the color percentage of RGB(ex red 50%, green 25% and blue %25) in an image, so i tried with this:

    mat : TCvMat;
    res : TCvScalar;
    object_filename := 'd:\1.bmp';
    image := cvLoadImage(pcvchar(@object_filename[1]), CV_LOAD_IMAGE_UNCHANGED);

    mat := cvMat(image.width, image.height, 0, image);
   for i := 0 to mat.rows - 1 do
     for j := 0 to mat.cols - 1 do
         res := cvGet2D(@mat, i, j);
         writeln('red: ', res.val[0]:2:2);
         writeln('green: ', res.val[1]:2:2);
         writeln('Blue: ', res.val[2]:2:2);
         writeln('Gray: ', res.val[3]:2:2);


the problem is that only "red.val[0]" got a real value, the rest of the array is 0.

what am i doing wrong here.

Thank you.

edit retag flag offensive close merge delete


Have you verified that this image also has 4 channels? You could get the type number with CV_MAT_TYPE( mat.type() ) in c++. I dont know if it works the same in delphi

Moster gravatar imageMoster ( 2013-09-11 01:33:22 -0500 )edit

yep, writeln(image.nChannels); gives me 3

yassine gravatar imageyassine ( 2013-09-11 05:33:38 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2013-09-11 02:45:19 -0500

updated 2013-09-11 02:46:26 -0500

You can compute percentage of colors with cvSum.

   img: pIplImage;;
    channels:array[0..2] of   pIplImage;
    BGRSum :Array[0..2] of TCvScalar;
    object_filename := 'd:\1.bmp';
    img := cvLoadImage(pcvchar(@object_filename[1]), 1);

    for i :=0 to 2 do
     channels[i] := cvCreateImage(cvGetImageSize(img),8,1);


    for i:=0 to 2 do
       BGRSum[i] := cvSum(channels[i]);

      total := img^.width * img^.height * 255;
    writeln('red:',BGRSum[2].val[0] / total * 100);
    writeln('green:',BGRSum[1].val[0] / total * 100);
    writeln('blue:',BGRSum[0].val[0] / total * 100);

edit flag offensive delete link more


i dont have cvGetImageSize(i used cvGetSize intsead) nor cvSum, and im uing this

yassine gravatar imageyassine ( 2013-09-11 05:27:12 -0500 )edit

OK.You use cvGetSize instead of cvGetImageSize then have to append cvSum signature to core_c.pas

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-09-11 06:21:37 -0500 )edit

i tried to add function cvSum(arr: pIplImage): TCvScalar; cdecl;but i get : Unsatisfied forward or external declaration: 'cvSum'

yassine gravatar imageyassine ( 2013-09-11 06:38:39 -0500 )edit

Then append below line to implementation part. function cvSum; external Core_Dll;

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-09-11 07:12:18 -0500 )edit

here is what i ve done: in core_c function cvSum(arr: pIplImage): TCvScalar; external 'opencv_core246.dll'; and when i use it i get : OpenCv Error: Bad argument <Unknown array type>

is there any other way to calculate the sum without using the CvSum

yassine gravatar imageyassine ( 2013-09-11 16:13:49 -0500 )edit

See last commit on Added cvSum

Laex gravatar imageLaex ( 2013-09-12 04:52:21 -0500 )edit

answered 2013-09-11 02:06:04 -0500

engine gravatar image

I don't use Delphi! but to answer your question here's what you can do, split the color planes of your image and convert each of them to the a binary cv::Mat, and then you can run the functioncountNonzero on those 3 binary images, you'll get how many pixel and then you can get the number of pixel in each plane. divide the result by the resolution of you image you'll you percentage. I hope that's possible in Delphi.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-09-10 19:14:49 -0500

Seen: 1,147 times

Last updated: Sep 11 '13