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.
The local variables r,g,b: integer
should be initialized to zero first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With