Ask Your Question
0

Get the color percentage

asked Sep 11 '13

yassine gravatar image

Hello,

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:

var
    mat : TCvMat;
    res : TCvScalar;
begin
    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
     begin
         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);
         writeln;
    end;

    readln;
 end.

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.

Preview: (hide)

Comments

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 (Sep 11 '13)edit

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

yassine gravatar imageyassine (Sep 11 '13)edit

2 answers

Sort by » oldest newest most voted
1

answered Sep 11 '13

updated Sep 11 '13

You can compute percentage of colors with cvSum.

var
   img: pIplImage;;
    channels:array[0..2] of   pIplImage;
    BGRSum :Array[0..2] of TCvScalar;
begin
    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);

     cvSplit(img,channels[0],channels[1],channels[2],0);

    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);

    readln;
 end.
Preview: (hide)

Comments

i dont have cvGetImageSize(i used cvGetSize intsead) nor cvSum, and im uing this https://github.com/Laex/Delphi-OpenCV

yassine gravatar imageyassine (Sep 11 '13)edit

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

Mostafa Sataki gravatar imageMostafa Sataki (Sep 11 '13)edit

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

yassine gravatar imageyassine (Sep 11 '13)edit

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

Mostafa Sataki gravatar imageMostafa Sataki (Sep 11 '13)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 (Sep 11 '13)edit

See last commit on https://github.com/Laex/Delphi-OpenCV Added cvSum

Laex gravatar imageLaex (Sep 12 '13)edit
0

answered Sep 11 '13

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.

Preview: (hide)

Question Tools

Stats

Asked: Sep 11 '13

Seen: 2,259 times

Last updated: Sep 11 '13