验证码 -图形图像识别的算法。http://blog.csdn.net/xtalk2008/archive/2007/11/01/1861310.aspx...
圖像經(jīng)過處理后,還需要進行分割,才能進行比較或識別,下面就是圖像分割的算法
原始圖
分割后的圖形
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;
轉(zhuǎn)載于:https://www.cnblogs.com/linyawen/archive/2011/06/21/2086440.html
總結(jié)
以上是生活随笔為你收集整理的验证码 -图形图像识别的算法。http://blog.csdn.net/xtalk2008/archive/2007/11/01/1861310.aspx...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RPC 远程过程调用协议
- 下一篇: 赫夫曼编码c++中的实现