Ask Your Question
0

Get the color percentage

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

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.

edit retag flag offensive close merge delete

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 ( 2013-09-11 01:33:22 -0600 )edit

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

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

2 answers

Sort by ยป oldest newest most voted
1

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

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

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.
edit flag offensive delete link more

Comments

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

yassine gravatar imageyassine ( 2013-09-11 05:27:12 -0600 )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 -0600 )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 -0600 )edit

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

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-09-11 07:12:18 -0600 )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 -0600 )edit

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

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

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

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

Question Tools

Stats

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

Seen: 2,230 times

Last updated: Sep 11 '13