腾讯公司的发展战略:有没有能识别所有角度直线的hough变换delphi代码?

来源:百度文库 编辑:高校问答 时间:2024/05/01 09:26:15
把图中所有直线识别出来(不止一条)
我想急要Delphi现成的

我以前做的,不是十分完美,自己改一改吧!

procedure TForm2.Button16Click(Sender: TObject);
var
maxline:boolean;
p: PByteArray;
Gray, x, y,i,j,t,hh: Integer;
Bmp: TBitmap;
hougharray,hougharray2: array of array of longint;
// hougharray:array[0..179,0..256*180] of longint;
dis,r:longint;
begin
Bmp := TBitmap.Create;
Bmp.Assign(Image3.Picture.Bitmap);
//设置为24位真彩色
Bmp.PixelFormat := pf24Bit;
// xMax:=Bmp.Width;
// yMax:=Bmp.Height;
hh:=strtoint(edit2.Text);
dis:= round(sqrt(bmp.Width * bmp.Width +bmp.Height * bmp.Height )+0.5);
setlength(hougharray,180,180*dis);
setlength(hougharray2,180,180*dis);
// for t := 0 to 179 do
// for r := -180*dis+1 to 180*dis do
// begin
// hougharray[t][r]:=0;
// end;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
//一个象素点三个字节
Gray := p[x * 3];
if gray > 10 then //全局阀值
begin
for t:=0 to 179 do
begin
r := round(((x*cos(t*3.14/180)+y*sin(t*3.14/180)))+0.5) ;
if r > 0 then
hougharray[t][r]:=hougharray[t][r]+1
else
hougharray2[t][-r]:=hougharray2[t][-r]+1;
end;
end;
end;
end;

Bmp.Free;

for t := 1 to 179 do
for r := 0 to 180*dis do
if (hougharray[t][r] > hh) then
begin
maxline := true;
for i := (t - 2) to (t + 2) do
for j := (r - 2) to (r + 2) do
if ((j >= 0) and (j < 180) and (i >= 0) and (i < 180) and (hougharray[i][j] > hougharray[t][r])) then
begin
maxline := false;
break;
end;

if (maxline = TRUE) then
begin
for x:=0 to image3.Width -1 do
begin
y:=round((r - x*cos(t*0.0174)) / sin(t*0.0174));
image3.Canvas.Pixels[x,y]:= clRed;
end;
end;
end;

for t := 1 to 179 do
for r := -180*dis to 0 do
if (hougharray2[t][-r] > max(hh-20,0)) then
begin
maxline := true;
for i := (t - 2) to (t + 2) do
for j := (-r - 2) to (-r + 2) do
if ((j >= 0) and (j < 180) and (i >= 0) and (i < 180) and (hougharray2[i][j] > hougharray2[t][-r])) then
begin
maxline := false;
break;
end;

if (maxline = TRUE) then
begin
for x:=0 to image3.Width -1 do
begin
y:=round((r - x*cos(t*0.0174)) / sin(t*0.0174));
image3.Canvas.Pixels[x,y]:= clRed;
end;
end;
end;

end;