Hough变换直线检测MatLab代码.doc

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Hough变换直线检测MatLab代码
function Img_hough = hough_s(Img, bw)
%该函数实现hough变换提取直线的功能。

%输入图像x,运行Z后直接画出直线。

%选择进行Hough变换的图像行
%Img为原图像;bw为边缘图像
%%
[H,W,D]=size(Img);
Img_hough = Img;
讦D==l
channel = Img_hough;
Img_hough = cal(3,channel, channel, channel); end
[M,N]=size(bw);
%求出图像大小。

%%
dtheta=l;
drho=l;
md=ceil((N+round(sqrt(M A2+N A2)))/drho);
%确定网格的最大区域。

ma=ceil(l 8O/dtheta); numrhotheta=zeros(md,ma);
%产生计数矩阵。

coordrhotheta=cell( 1,1);
% para=cell(l,3);
❷II数组相当于c语言中的指针,可动态的改变大小。

for i=l:md
for j=l:ma
coordrhotheta{ i,j}=[J;
end
end
%产生空网格。

ymin = 5;
ymax = M - 4;
for i=ymin:ymax
forj=l:N
if bw(i,j) == 1
for k=l: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+ l,k}=[coordrhotheta {rho+ l,k);[ij]];
%记录hough变换值相应位置对应的点的坐标。

end
end
end
end
%% figure;imshow(Img);
hold on
num = 8;
for i=l : num
[y 1 $col 1 ]=max(numrhotheta);
[y2,col]二max(yl);
row=coll(col);
%求出hough变换值最大值的坐标。

numrhotheta(row,col)=0;
%为了避免重复计算,将计算过的点置0。

rhood=1;
chood=0;
top=max(row-rhood, 1);
down=min(row+rhood,md);
Ieft=max(col-chood, 1);
right=min(col+chood,ma);
numrhotheta(top:down,left:right)=0;
% nds=coordrhotheta{ row.col);
nils 二[];
for r = top : down
fore = left: right
nds = [nds; coordrhothetafr, c}];
end
end
Img_hough=draw(Img_hough, nds);
end imwrite(mat2gray(numihotheta)/numrhotheta.bmp,)
RGB = imreadCga ntrycrane.png);
I = rgb2gray(RGB); % convert to intensity
BW = edged'canny*); % extract edges
[H,T,R] = hough但W,RhoResolutiorT,0・5,ThetaResolution;0・5); % display the original image subplot(231,1);
imshow(RGB);
titlef^antrycrane.png');
% display the hough matrix
subplot(2,1,2);
imshow(imadjust(mat2gray(H)),'XData',T,'YData',R,... 'lnitialMagnification','fit');
title('Hough transform of gantrycrane.png);
xlabelCXtheta'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(hot);
l= imreadrD:\MATLAB7\aaa・bmp‘); rotl=jmrotate(l,33,,crop,);
BW=edge(rotl,'canny');
[H,T,R]=hough(BW);
imshowfHJl/XData'X'YData^R/lnitialMagnification'/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(:,l));
plotfx^yjs'/color'jwhite'); lines=houghlines(BW,T/R z P/FillGap',5/MinLength'/7); figurejmshowfrotO^hold on
max len=0;
for k=l:length(lines)
xy=[li nes(k).poi ntl;li nes(k).poi nt2]; plot(xy(:,2),xy(:,2),'LineWidth',2,'ColorVgreen'); plot(xy(l/l),xy(l/2);x,;LineWidth,z2/,Color';yellow,);
plot(xy( 2/l)/xy(2,2)/x,/'UneWidth,,2/,Color,/,red,);
len 二no rm(li nes(k).poi ntl・li nes(k).poi nt2);
讦(len> maxje n)
max」en 二len;
xy」on g=xy;
end
end
plot(xyJong(:,l)/xy_long(:/2)JLineWidth,/2/Color l/,cyan,);
四.
clear
I = imread('taj1 small3・jpgj;
I = rgb2gray(l);
%l = i m rotate (1,33, *crop*); % figure
% imshow(rotl);
figure imshow(l);
BW = edge(l/canny f);
figure
imshow(BW);
[H,T,R] = hough(BW);
figure
imshow(H,[],'XData・,T,'YData',R,…
,lnitialMagnification,,,fit,); xlabelCXtheta*), ylabel(f\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(l),hold on
max_len = 0;
for k = 1 :length(lines)
xy = [Iines(k).point1; Iines(k).point2];
plot(xy(:,1),xy(:,2),•LineWidth*,2,•Color*,•green1);
% 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,J2;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=imreadC circui t. tif') ;%旋转图像并寻找边缘
rotl=imrotatc(l, 33,' crop');
subplot (221), figl=imshow(rotl);
BW=edge(rotI,' canny,) ;%使用canny方法检测目标边界subplot(222), imshow(BW); %执行Hough变换并显示Hough矩阵
[H, T, R]=hough (BW);
subplot (223), imshow(H, [],' XData,, T,' YData,, R,' InitialMagnif ication,,' fit,);
xlabel (' \theta,), ylabel (' \rho,);
axis on,axis normal, hoid on;
%在Hough矩阵中寻找前5个大丁• Hough矩阵中最大值0. 3倍的峰值P=houghpeaks (H, 5,' threshold', ceil (0. 3*max(H(:)))) ;%hough 变换的极值点
x=T(P(:,2));y=R(P(:,l));%由行、列索引转换成实际坐标
plot (x, y,' s',' color*,' white*) ;%在Hough矩阵图像中标出峰值位置
%找岀并绘制直线
lines二houghlines (BW, T, R, P,' F订IGap', 5,' MinLength,, 7) ;%合并距离小于5 的线段,丢弃所有长度小于7的直线段
subplot(224), imshow(rotl), hold on
maxien=0;
for k = 1: lcngth(lincs)%依次标出各条直线段
xy二[lines(k)・ pointl;lines(k)・ point2];
plot (xy(:, 1), xy(:, 2),' x',' LineWidth,, 2,' Color ,' green,);
%绘制线段端点
plot (xy(l, 1), xy(1, 2),' x',' LincWidth', 2,' Color,,' yellow');
plot (xy (2, 1), xy (2, 2),' x',' LineWidth,, 2,' Color,,' red,);
%确定最长的线段
len二norm(lines(k). pointl-lines(k). point2);
if(len>max len)
max_len=len; xy_Iong二xy;
end
end
%高亮显示最长线段
plot (xy_Iong(:, 1), xy_Iong(:, 2),' LineWidth,, 2, ' Color',' cyan');
其中1、Tines二houghlines (BW, T, R, P,' F订IGap', 5,,MinLength,, 7) ;%合并距离小于5的线段,丢弃所有长度小于7的直线段”是什么意思?有什么依据?2、for k = 1: length (lines)%依次标出各条直线段;k 是什么,length (lines) 是什么,这句话是什么意思?上面那句“max_len二0;”是什么意思?为什么是0? 初学菜鸟,急求高人解释,感激不尽!!!
回答你第一个问题:有些时候,同一条线段由于某些原因(比如光照、噪音等)变成了不连续的两条较短的线段,所以要进项合并,至于多少长度的才合并成同一条直线,是依据不同的图像而言的,并没有统一的结论。

而有些线段可能是噪声,所以小于7的舍去,这个也么有标准,需要根据不同的图像而定。

第二个问题:k是所标出来直线段的个数,length (lines)是直线段的长度。

max二0是初始化,防止系统给它随机分配一个值,因为最后要比较的。

l=imread('houghtestl.bmp');
%imshow(l);
BW=edge(l/canny');
%figure,imshow(BW)
[H,theta,rho]=hough(BW);
figure/imshow(imadjust(mat2gray(H))/[]/,XData,/theta/'YData'/rho/lnitialMagnification,/W);
P 二houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x=theta(P(:z2));
y=rho(P(:,l));
%plot(x,y/s7color'z,black');
lines=houghlines(BW,theta,rho,P,'FillGap'/5,'MinLength,,7);
%figurejmshow(l),hold on
max」en=0;
for k=l:length(lines)
xy=[lines(k).poi ntl;lines(k).poi nt2];
plot(xy(:,l),xy(:,2),,LineWidth,,2,'Color,,,greeiY);
plot(xy(l,l),xy(l,2),'xTLineWidth',2,'Color‘,'yellow');
plot(xy(2z l),xy(2,2)/x7LineWidth'z2/Color7red,);
len 二n orm(lines(k).pointl・li nes(k).poi nt2);
讦(len> maxje n)
maxje n=len;
xy」on g=xy;
end
end
plot(xy_long(:/l);xy_long(:/2)/LineWidth,/2/Color7cyan,);
你用我的代码试试
hough变换的直线检测
close all;
clear all;
clc;
%%
I = imread(,8_traffic.bmp','bmp,);
BW= edge(l;sober);
[H,T,R] = hough(BW); imshow(H,[];XData',T;YData,,R,...
'InitialMagnification'/fif); xlabelC\theta*), ylabelf\rho'); axis on, axis normal, hold on;
P = houghpeaks(H, 10,'threshold*,ceil(0.3*max(H(:))));
x = T(P(:,2));y = R(P(:,1));
figure ⑴;
plot(x,y,'s','color1,'white1);
% Find lines and plot them
lines = houghlines(BW,T,R,P,,FillGap',20,,MinLength,,40);
figure(2);
[g,t]=edge(l,'sober,[],'both*);
imshow(l);
hold on;
% imshow(g);
maxje n = 0;
for k = 1 :length(lines)
xy = [lines(k).point1; lines(k).point2]; plot(xy(:,1),xy(:,2)/LineWidth',2,,Color7green,);
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),Y/LineWidth^/Color*,'yellow*); plot(xy(2,1),xy(2,2);
x,,,LineWidth,J2;Color,;red,);
% Determine the endpoints of the Iongest line segment len = norm(lines(k).point1 - Iines(k).point2);
if (len > maxjen)
max Jen = len;
xy_long = xy;
end
end % highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth*,2,'Color','cyan');
我运行没问题呀•你的图像是不是有问题,我用的是灰度图像,8位的.。

相关文档
最新文档