Get average color of an image
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 开发者_StackOverflow中文版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.
One thing which seems wrong with this is that in comments you say that you have 25*25 image, but you loop over 26*26 pixels, so the loops should be:
for I := 0 to 24 do
begin
for y := 0 to 24 do
精彩评论