Hough变换直线检测MatLab代码.docx
《Hough变换直线检测MatLab代码.docx》由会员分享,可在线阅读,更多相关《Hough变换直线检测MatLab代码.docx(7页珍藏版)》请在冰豆网上搜索。
Hough变换直线检测MatLab代码
Hough变换直线检测MatLab代码
一.
functionImg_hough=hough_s(Img,bw)
%该函数实现hough变换提取直线的功能。
%输入图像x,运行之后直接画出直线。
%选择进行Hough变换的图像行
%Img为原图像;bw为边缘图像
%%
[H,W,D]=size(Img);
Img_hough=Img;
ifD==1
channel=Img_hough;
Img_hough=cat(3,channel,channel,channel);
end
[M,N]=size(bw);
%求出图像大小。
%%
dtheta=1;
drho=1;
md=ceil((N+round(sqrt(M^2+N^2)))/drho);
%确定网格的最大区域。
ma=ceil(180/dtheta);
numrhotheta=zeros(md,ma);
%产生计数矩阵。
coordrhotheta=cell(1,1);
%para=cell(1,3);
�ll数组相当于c语言中的指针,可动态的改变大小。
fori=1:
md
forj=1:
ma
coordrhotheta{i,j}=[];
end
end
%产生空网格。
ymin=5;
ymax=M-4;
fori=ymin:
ymax
forj=1:
N
ifbw(i,j)==1
fork=1:
ma
rho=round((j*cos(dtheta*k*pi/180)+i*sin(dtheta*k*pi/180))/drho);
%根据直线的法线式表示,计算出平面上不同点的hough变换值。
rho=rho+ceil(N/drho);%可能的最大负值。
numrhotheta(rho+1,k)=numrhotheta(rho+1,k)+1;
%将hough变换值相应位置的计数值加1。
coordrhotheta{rho+1,k}=[coordrhotheta{rho+1,k};[i,j]];
%记录hough变换值相应位置对应的点的坐标。
end
end
end
end
%%
figure;imshow(Img);
holdon
num=8;
fori=1:
num
[y1,col1]=max(numrhotheta);
[y2,col]=max(y1);
row=col1(col);
%求出hough变换值最大值的坐标。
numrhotheta(row,col)=0;
%为了避免重复计算,将计算过的点置0。
rhood=1;
chood=0;
top=max(row-rhood,1);
down=min(row+rhood,md);
left=max(col-chood,1);
right=min(col+chood,ma);
numrhotheta(top:
down,left:
right)=0;
% nds=coordrhotheta{row,col};
nds=[];
forr=top:
down
forc=left:
right
nds=[nds;coordrhotheta{r,c}];
end
end
Img_hough=draw(Img_hough,nds);
end
imwrite(mat2gray(numrhotheta),'numrhotheta.bmp')
二.
RGB=imread('gantrycrane.png');
I=rgb2gray(RGB);%converttointensity
BW=edge(I,'canny');%extractedges
[H,T,R]=hough(BW,'RhoResolution',0.5,'ThetaResolution',0.5);%displaytheoriginalimage
subplot(2,1,1);
imshow(RGB);
title('gantrycrane.png');
%displaythehoughmatrix
subplot(2,1,2);
imshow(imadjust(mat2gray(H)),'XData',T,'YData',R,...'InitialMagnification','fit');
title('Houghtransformofgantrycrane.png');
xlabel('\theta'),ylabel('\rho');
axison,axisnormal,holdon;
colormap(hot);
三.
I=imread('D:
\MATLAB7\aaa.bmp');
rotI=imrotate(I,33,'crop');
BW=edge(rotI,'canny');
[H,T,R]=hough(BW);
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axison,axisnormal,holdon;
P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:
))));
x=T(P(:
2));y=R(P(:
1));
plot(x,y,'s','color','white');
lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure,imshow(rotI),holdon
max_len=0;
fork=1:
length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:
1),xy(:
2),'LineWidth',2,'Color','green');
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
len=norm(lines(k).point1-lines(k).point2);
if(len>max_len)
max_len=len;
xy_long=xy;
end
end
plot(xy_long(:
1),xy_long(:
2),'LineWidth',2,'Color','cyan');
四.
clear
I = imread('taj1small3.jpg');
I = rgb2gray(I);
%I = imrotate(I,33,'crop');
% figure
% imshow(rotI);
figure
imshow(I);
BW = edge(I,'canny');
figure
imshow(BW);
[H,T,R] = hough(BW);
figure
imshow(H,[],'XData',T,'YData',R,
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on
axis normal
hold on
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:
))));
x = T(P(:
2)); y = R(P(:
1));
plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(I),hold on
max_len = 0;
for k = 1:
length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:
1),xy(:
2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:
1),xy_long(:
2),'LineWidth',2,'Color','blue');
程序如下:
I=imread('circuit.tif');%旋转图像并寻找边缘
rotI=imrotate(I,33,'crop');
subplot(221),fig1=imshow(rotI);
BW=edge(rotI,'canny');%使用canny方法检测目标边界
subplot(222),imshow(BW);
%执行Hough变换并显示Hough矩阵
[H,T,R]=hough(BW);
subplot(223),imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axison,axisnormal,holdon;
%在Hough矩阵中寻找前5个大于Hough矩阵中最大值0.3倍的峰值
P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:
))));%hough变换的极值点
x=T(P(:
2));y=R(P(:
1));%由行、列索引转换成实际坐标
plot(x,y,'s','color','white');%在Hough矩阵图像中标出峰值位置
%找出并绘制直线