还是以上一个图片为例,经过图像二值化算法以后,图像只有黑白两色,这样才能为图像识别做准备
处理前图像
处理后图像
代码如下
function BTTwoValue(Bmp: TBitmap; TV: Integer): TBitmap;
var
x, y: Integer;
begin
Bmp.PixelFormat := pf24bit; Result := TBitmap.Create; Result.PixelFormat := pf24bit; Result.Width := Bmp.Width; Result.Height := Bmp.Height;
for x := 0 to Bmp.Width - 1 do for y := 0 to Bmp.Height - 1 do if Bmp.Canvas.Pixels[x, y] > TV then Result.Canvas.Pixels[x, y] := ClWhite else Result.Canvas.Pixels[x, y] := ClBlack;
FreeAndNil(Bmp);
end;