您的位置:首页 > 理论基础 > 计算机网络

验证码 -图形图像识别的算法。http://blog.csdn.net/xtalk2008/archive/2007/11/01/1861310.aspx

2011-06-21 23:00 639 查看
图像经过处理后,还需要进行分割,才能进行比较或识别,下面就是图像分割的算法
原始图



分割后的图形
1

2

3

4

5

6

7

8


图像分割代码如下
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐