图片进行二值化 ( 积分: 100 )

  • 主题发起人 主题发起人 pyc2001
  • 开始时间 开始时间
P

pyc2001

Unregistered / Unconfirmed
GUEST, unregistred user!
谁有对图片进行二值化的代码。
 
谁有对图片进行二值化的代码。
 
没有人知道在Delphi中对图片进行二值化吗?
 
网上找找,很简单的算法
 
var
p: PByteArray;
Gray, x, y: Integer;
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
Bmp.Assign(Image1.Picture.Bitmap);
//设置为24位真彩色
Bmp.PixelFormat := pf24Bit;
randomize;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
//一个象素点三个字节
Gray := Round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x
* 3] * 0.11);
if gray > 128 then //全局阀值128
begin
p[x * 3] := 255;
p[x * 3 + 1] := 255;
p[x * 3 + 2] := 255;
end
else
begin
p[x * 3] := 0;
p[x * 3 + 1] := 0;
p[x * 3 + 2] := 0;
end;
end;
end;
Image2.Picture.Bitmap.Assign(Bmp);
Bmp.Free;

end;
这样就行了
 
  上面的全局阀值T取为128,对太亮或太暗的图像效果不佳。

更好的办法是先将彩色图像I变为灰度图像G,然后统计各灰度值出现的概率(即频率),按
T=∑gi*pi
求得全局阀值T后,再二值化G即可
 
后退
顶部