Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get average color of an image

Tags:

delphi

I am trying to get the average color of an image. I tried various methods and now I use the following code, but I could not get the correct result.

Can anyone explain what is wrong with my code?

  //load bitmap to curimg
  img1.Picture.Bitmap := curimg ; //testing the previous line

  //My image is always greater than 25x25 but I only need a 25x25 box

  for I := 0  to 25 do
  begin
    for y := 0  to 25 do
    begin
      r := r + GetRValue(curimg.Canvas.Pixels[y, I]);
      g := g + GetGValue(curimg.Canvas.Pixels[y, I]);
      b := b + GetBValue(curimg.Canvas.Pixels[y, I]);
    end;
  end;
  r := r div (25 * 25);
  g := g div (25 * 25);
  b := b div (25 * 25);
  rgbk := RGB(r, g, b);
  Result = rgbk;
end;

img1 and image1 of type TImageBox are on my form.

like image 980
VibeeshanRC Avatar asked Oct 11 '11 06:10

VibeeshanRC


1 Answers

The local variables r,g,b: integer should be initialized to zero first.

like image 71
kludg Avatar answered Sep 18 '22 17:09

kludg