获取图像的平均颜色

Vib*_*nRC 5 delphi

我正在尝试获取图像的平均颜色。我尝试了各种方法,现在使用下面的代码,但无法获得正确的结果。

谁能解释我的代码有什么问题?

  //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;
Run Code Online (Sandbox Code Playgroud)

img1image1类型TImageBox在我的表格上。

klu*_*udg 4

局部变量r,g,b: integer应首先初始化为零。

  • Vibeeshan,编译器没有警告您变量未初始化吗? (7认同)