Delphi图像细化处理


所谓细化,就是从原来的图中去掉一些点,但仍要保持原来的形状。实际上,是保持原图的骨架。

type
 TRGBArray = array[0..32767] of TRGBTriple;
 PRGBArray = ^TRGBArray;

procedure Xihua(Bitmap: TBitmap);
var
 bmp: TBitmap;
 X, Y: integer;
 O, T, C, B: pRGBArray;
 nb: array[1..3, 1..3] of integer;
 c1, c2, c3, c4: boolean;
 ncount: integer;
begin
 bmp := TBitmap.Create;
 bmp.Assign(bitmap);
 for Y := 1 to bmp.Height - 2 do
 begin
 O := bmp.ScanLine[Y];
 T := bitmap.ScanLine[Y - 1];
 C := bitmap.ScanLine[Y];
 B := bitmap.ScanLine[Y + 1];
 for X := 1 to bmp.Width - 2 do
 begin
 c1 := false;
 c2 := false;
 c3 := false;
 c4 := false;
 // 设立四个条件的初始值
 nb[1, 1] := T[X - 1].rgbtRed div 255;
 nb[1, 2] := T[X].rgbtRed div 255;
 nb[1, 3] := T[X + 1].rgbtRed div 255;
 nb[2, 1] := C[X - 1].rgbtRed div 255;
 nb[2, 2] := C[X].rgbtRed div 255;
 nb[2, 3] := C[X + 1].rgbtRed div 255;
 nb[3, 1] := B[X - 1].rgbtRed div 255;
 nb[3, 2] := B[X].rgbtRed div 255;
 nb[3, 3] := B[X + 1].rgbtRed div 255;
 //将[x,y]周围的八个象素点和它自己0-1化
 nCount := nb[1, 1] + nb[1, 2] + nb[1, 3] + nb[2, 1] + nb[2, 3] + nb[3, 1] + nb[3, 2] + nb[3, 3];
 // 获得ncount的值
 if (ncount >= 2) and (ncount <= 6) then c1 := True;
 //condition1
 ncount := 0;
 if (nb[1, 1] = 0) and (nb[1, 2] = 1) then inc(ncount);
 if (nb[1, 2] = 0) and (nb[1, 3] = 1) then inc(ncount);
 if (nb[1, 3] = 0) and (nb[2, 3] = 1) then inc(ncount);
 if (nb[2, 3] = 0) and (nb[3, 3] = 1) then inc(ncount);
 if (nb[3, 3] = 0) and (nb[3, 2] = 1) then inc(ncount);
 if (nb[3, 2] = 0) and (nb[3, 1] = 1) then inc(ncount);
 if (nb[3, 1] = 0) and (nb[2, 1] = 1) then inc(ncount);
 if (nb[2, 1] = 0) and (nb[1, 1] = 1) then inc(ncount);
 if ncount = 1 then c2 := true;
 //condition2
 if (nb[1, 2] * nb[3, 2] * nb[2, 3] = 0) then c3 := true;
 // condition3
 if (nb[2, 1] * nb[2, 3] * nb[3, 2] = 0) then c4 := true;
 //condition4
 if (c1 and c2 and c3 and c4) then
 begin
 O[X].rgbtRed := 255;
 O[X].rgbtGreen := 255;
 O[X].rgbtBlue := 255; //设置O[X]为白色
 end;
 end;
 end;
 bitmap.Assign(bmp);
 bmp.Free;
end;

联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678