图像分割算法

图像经过处理后,还需要进行分割,才能进行比较或识别,下面就是图像分割的算法
原始图

分割后的图形
12345678 
图像分割代码如下
  TImageInfo = record
    Bmp: TBitmap;
    Rect: TRect;
  end;
  pImageInfo = ^TImageInfo;

function BTSegment(Bmp: TBitmap; CType, MaxValue, MinValue: Integer): TList;
  procedure Connect1(tbmp: tbitmap; x, y: integer; var _xmin, _xmax, _ymin, _ymax: integer; Color: TColor = 1);
  begin
    tbmp.PixelFormat := pf24bit;
    tbmp.canvas.pixels[x,y] := Color;
    _xmin := min(x, _xmin); _xmax := max(x, _xmax); _ymin := min(y, _ymin); _ymax := max(y, _ymax);
    if tbmp.canvas.pixels[x + 1, y + 0] = ClBlack then connect1(tbmp, x + 1, y + 0, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 1, y - 1] = ClBlack then connect1(tbmp, x + 1, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 0, y - 1] = ClBlack then connect1(tbmp, x + 0, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y - 1] = ClBlack then connect1(tbmp, x - 1, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y + 0] = ClBlack then connect1(tbmp, x - 1, y + 0, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y + 1] = ClBlack then connect1(tbmp, x - 1, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 0, y + 1] = ClBlack then connect1(tbmp, x + 0, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 1, y + 1] = ClBlack then connect1(tbmp, x + 1, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
  end;

  procedure Connect2(tbmp: tbitmap; x, y: integer; var _xmin, _xmax, _ymin, _ymax: integer; Color: TColor = 1);
  begin
    tbmp.PixelFormat := pf24bit;
    tbmp.canvas.pixels[x,y] := Color;
    _xmin := min(x, _xmin); _xmax := max(x, _xmax); _ymin := min(y, _ymin); _ymax := max(y, _ymax);
    if tbmp.canvas.pixels[x + 0, y + 2] = ClBlack then connect2(tbmp, x + 0, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y + 2] = ClBlack then connect2(tbmp, x - 2, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y + 2] = ClBlack then connect2(tbmp, x - 1, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 1, y + 2] = ClBlack then connect2(tbmp, x + 1, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y + 2] = ClBlack then connect2(tbmp, x + 2, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y + 1] = ClBlack then connect2(tbmp, x + 2, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y + 0] = ClBlack then connect2(tbmp, x + 2, y + 0, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y - 1] = ClBlack then connect2(tbmp, x + 2, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y - 2] = ClBlack then connect2(tbmp, x + 2, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 1, y - 2] = ClBlack then connect2(tbmp, x + 1, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 0, y - 2] = ClBlack then connect2(tbmp, x + 0, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y - 2] = ClBlack then connect2(tbmp, x - 1, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y - 2] = ClBlack then connect2(tbmp, x - 2, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y - 1] = ClBlack then connect2(tbmp, x - 2, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y - 0] = ClBlack then connect2(tbmp, x - 2, y - 0, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y + 1] = ClBlack then connect2(tbmp, x - 2, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
  end;

  procedure Connect3(tbmp: tbitmap; x, y: integer; var _xmin, _xmax, _ymin, _ymax: integer; Color: TColor = 1);
  begin
    tbmp.PixelFormat := pf24bit;
    tbmp.canvas.pixels[x,y] := Color;
    _xmin := min(x, _xmin); _xmax := max(x, _xmax); _ymin := min(y, _ymin); _ymax := max(y, _ymax);
    if tbmp.canvas.pixels[x - 3, y + 3] = ClBlack then Connect3(tbmp, x - 3, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y + 3] = ClBlack then Connect3(tbmp, x - 2, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y + 3] = ClBlack then Connect3(tbmp, x - 1, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 0, y + 3] = ClBlack then Connect3(tbmp, x - 0, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 1, y + 3] = ClBlack then Connect3(tbmp, x + 1, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y + 3] = ClBlack then Connect3(tbmp, x + 2, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y + 3] = ClBlack then Connect3(tbmp, x + 3, y + 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 3, y - 3] = ClBlack then Connect3(tbmp, x - 3, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 2, y - 3] = ClBlack then Connect3(tbmp, x - 2, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 1, y - 3] = ClBlack then Connect3(tbmp, x - 1, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 0, y - 3] = ClBlack then Connect3(tbmp, x - 0, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 1, y - 3] = ClBlack then Connect3(tbmp, x + 1, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 2, y - 3] = ClBlack then Connect3(tbmp, x + 2, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y - 3] = ClBlack then Connect3(tbmp, x + 3, y - 3, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 3, y + 2] = ClBlack then Connect3(tbmp, x - 3, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 3, y + 1] = ClBlack then Connect3(tbmp, x - 3, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 3, y + 0] = ClBlack then Connect3(tbmp, x - 3, y + 0, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 3, y - 2] = ClBlack then Connect3(tbmp, x - 3, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x - 3, y - 1] = ClBlack then Connect3(tbmp, x - 3, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y + 2] = ClBlack then Connect3(tbmp, x + 3, y + 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y + 1] = ClBlack then Connect3(tbmp, x + 3, y + 1, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y + 0] = ClBlack then Connect3(tbmp, x + 3, y + 0, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y - 2] = ClBlack then Connect3(tbmp, x + 3, y - 2, _xmin, _xmax, _ymin, _ymax, Color);
    if tbmp.canvas.pixels[x + 3, y - 1] = ClBlack then Connect3(tbmp, x + 3, y - 1, _xmin, _xmax, _ymin, _ymax, Color);
  end;
var
  x, y, _x, _y, xmin, xmax, ymin, ymax: integer;
  pII: pImageInfo;
begin
  Result := TList.Create;
  Bmp.PixelFormat := pf24bit;
  for x := 1 to Bmp.Width - 2 do for y := 1 to Bmp.Height - 2 do
  begin
    if Bmp.Canvas.Pixels[x, y] <> ClBlack then Continue;
    xmin := x; xmax := x; ymin := y; ymax := y;
    if CType = 1 then Connect1(Bmp, x, y, xmin, xmax, ymin, ymax, 1);
    if CType = 2 then Connect2(Bmp, x, y, xmin, xmax, ymin, ymax, 1);
    if CType = 3 then Connect3(Bmp, x, y, xmin, xmax, ymin, ymax, 1);
    if (xmax - xmin < MaxValue) and (xmax - xmin > MinValue) and (ymax - ymin < MaxValue) and (ymax - ymin > MinValue) then
    begin
      New(pII); pII.Bmp := TBitmap.Create; pII.Bmp.PixelFormat := pf24bit; pII.Bmp.Width := xmax - xmin + 4; pII.Bmp.Height := ymax - ymin + 4;
      for _x := xmin - 2 to xmax + 2 do for _y := ymin - 2 to ymax + 2 do if bmp.Canvas.Pixels[_x, _y] = 1 then pII.Bmp.Canvas.Pixels[_x - xmin + 2, _y - ymin + 2] := ClBlack else pII.Bmp.Canvas.Pixels[_x - xmin + 2, _y - ymin + 2] := ClWhite;
      pII.Rect.Left := xmin; pII.Rect.Right := xmax; pII.Rect.Top := ymin; pII.Rect.Bottom := ymax;
      Result.Add(pII);
    end;
  end;
  FreeAndNil(Bmp);
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值