zoukankan      html  css  js  c++  java
  • 验证码 图形图像识别的算法。http://blog.csdn.net/xtalk2008/archive/2007/11/01/1861310.aspx

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

    分割后的图形
    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;

  • 相关阅读:
    谈谈入职新公司1月的体会
    来点高逼格的,使用前端Sendmessage实现SSO
    2019做的第一个艰难决定
    Golang中设置函数默认参数的优雅实现
    linux系统shell基础知识入门二
    在AWS中自定义Credential Provider实现Client连接
    linux系统shell基础知识入门
    初学者学习golang可能遇到的坑
    【Menu】 目录索引
    rsync 服务介绍及相关实验
  • 原文地址:https://www.cnblogs.com/linyawen/p/2086440.html
Copyright © 2011-2022 走看看