Delphi使用Hough变换查找直线


Hough变换用来在图像中查找直线和圆,下面给出的一段程序,用来找出图像中最长的直线。该程序给出Hough变换的4种实现方法,分别采用了4种不同的模板:

 │-1  0  1│  │-1  -1  0│  │-1  -1  -1│  │0  -1  -1│
(1)│-1  0  1 │ (2) │-1  0  1│ (3)│ 0  0  0 │  (4)│1  0  -1│
 │-1  0  1 │  │ 0  1  1│  │ 1  1  1 │  │1  -1  0│


程序实现:


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


procedure Hough(Bitmap: TBitmap; iMob: Integer; z: Word = 1);
//Bitmap:传入的位图,iMob:使用模板序号(取值:1-4),z:模板参数,取1
const
 Ray: array[1..4]of array[0..8] of Integer = (
 (-1, 0, 1,-1, 0, 1,-1, 0, 1),
 (-1,-1, 0,-1, 0, 1, 0, 1, 1),
 (-1,-1,-1, 0, 0, 0, 1, 1, 1),
 (0, -1,-1, 1, 0, -1,1,-1, 0));
var
 O, T, C, B: pRGBArray;
 i, x, y: Integer;
 tBuff: TBitmap;
begin
 i := Max(1, Min(4, iMob));
 tBuff := TBitmap.Create;
 tBuff.Assign(Bitmap);
 for x := 1 to Bitmap.Height - 2 do
 begin
 O := Bitmap.ScanLine[x];
 T := tBuff.ScanLine[x-1];
 C := tBuff.ScanLine[x];
 B := tBuff.ScanLine[x+1];
 for y := 1 to (Bitmap.Width - 2) do
 begin
 //红色分量
 O[y].rgbtRed := Max(0, Min(255, (
 T[y-1].rgbtRed * Ray[i][0] + T[y].rgbtRed * Ray[i][1] + T[y+1].rgbtRed * Ray[i][2] +
 C[y-1].rgbtRed * Ray[i][3] + C[y].rgbtRed * Ray[i][4] + C[y+1].rgbtRed * Ray[i][5] +
 B[y-1].rgbtRed * Ray[i][6] + B[y].rgbtRed * Ray[i][7] + B[y+1].rgbtRed * Ray[i][8]) div Z));
 //蓝色分量
 O[y].rgbtBlue := Max(0, Min(255, (
 T[y-1].rgbtBlue * Ray[i][0] + T[y].rgbtBlue * Ray[i][1] + T[y+1].rgbtBlue * Ray[i][2] +
 C[y-1].rgbtBlue * Ray[i][3] + C[y].rgbtBlue * Ray[i][4] + C[y+1].rgbtBlue * Ray[i][5] +
 B[y-1].rgbtBlue * Ray[i][6] + B[y].rgbtBlue * Ray[i][7] + B[y+1].rgbtBlue * Ray[i][8]) div Z));
 //绿色分量
 O[y].rgbtGreen := Max(0, Min(255, (
 T[y-1].rgbtGreen * Ray[i][0] + T[y].rgbtGreen * Ray[i][1] + T[y+1].rgbtGreen * Ray[i][2] +
 C[y-1].rgbtGreen * Ray[i][3] + C[y].rgbtGreen * Ray[i][4] + C[y+1].rgbtGreen * Ray[i][5] +
 B[y-1].rgbtGreen * Ray[i][6] + B[y].rgbtGreen * Ray[i][7] + B[y+1].rgbtGreen * Ray[i][8]) div Z));
 end;
 end;
 tBuff.Free;
end;

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