文档库 最新最全的文档下载
当前位置:文档库 › Hough变换直线检测MatLab代码

Hough变换直线检测MatLab代码

Hough变换直线检测MatLab代码

一.

function Img_hough = hough_s(Img, bw)

%该函数实现hough变换提取直线的功能。

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

%选择进行Hough变换的图像行

%Img为原图像;bw为边缘图像

%%

[H,W,D]=size(Img);

Img_hough = Img;

if D==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语言中的指针,可动态的改变大小。

for i=1:md

for j=1:ma

coordrhotheta{i,j}=[];

end

end

%产生空网格。

ymin = 5;

ymax = M - 4;

for i=ymin:ymax

for j=1:N

if bw(i,j) == 1

for k=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);

hold on

num = 8;

for i=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 = [];

for r = top : down

for c = 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); % convert to intensity

BW = edge(I,'canny'); % extract edges

[H,T,R] = hough(BW,'RhoResolution',0.5,'ThetaResolution',0.5); % display the original image subplot(2,1,1);

imshow(RGB);

title('gantrycrane.png');

% display the hough matrix

subplot(2,1,2);

imshow(imadjust(mat2gray(H)),'XData',T,'YData',R,... 'InitialMagnification','fit');

title('Hough transform of gantrycrane.png');

xlabel('\theta'), ylabel('\rho');

axis on, axis normal, hold on;

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');

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');

lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7); figure,imshow(rotI),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(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');

axis on,axis normal,hold on;

%在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矩阵图像中标出峰值位置

%找出并绘制直线

lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);%合并距离小于5的线段,丢弃所有长度小于7的直线段

subplot(224),imshow(rotI),hold on

max_len=0;

for k = 1:length(lines)%依次标出各条直线段

xy=[lines(k).point1;lines(k).point2];

plot(xy(:,1),xy(:,2),'x','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');

其中1、“lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);%合并距离小于5的线段,丢弃所有长度小于7的直线段”是什么意思?有什么依据?2、for k = 1:length(lines)%依次标出各条直线段;k是什么,length(lines)是什么,这句话是什么意思?上面那句“max_len=0;”是什么意思?为什么是0?初学菜鸟,急求高人解释,感激不尽!!!

回答你第一个问题:有些时候,同一条线段由于某些原因(比如光照、噪音等)变成了不连续的两条较短的线段,所以要进项合并,至于多少长度的才合并成同一条直线,是依据不同的图像而言的,并没有统一的结论。而有些线段可能是噪声,所以小于7的舍去,这个也么有标准,需要根据不同的图像而定。

第二个问题:k是所标出来直线段的个数,length(lines)是直线段的长度。max=0是初始化,防止系统给它随机分配一个值,因为最后要比较的。

I=imread('houghtest1.bmp');

%imshow(I);

BW=edge(I,'canny');

%figure,imshow(BW)

[H,theta,rho]=hough(BW);

figure,imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,'InitialMagnif ication','fit');

P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));

x=theta(P(:,2));

y=rho(P(:,1));

%plot(x,y,'s','color','black');

lines=houghlines(BW,theta,rho,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(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');

你用我的代码试试

hough变换的直线检测

close all;

clear all;

clc;

%%

I = imread('8_traffic.bmp','bmp');

BW= edge(I,'sobel');

[H,T,R] = hough(BW);

imshow(H,[],'XData',T,'YData',R,...

'InitialMagnification','fit');

xlabel('\theta'), ylabel('\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(1);

plot(x,y,'s','color','white');

% Find lines and plot them

lines = houghlines(BW,T,R,P,'FillGap',20,'MinLength',40); figure(2);

[g, t]=edge(I,'sobel',[],'both');

imshow(I);

hold on;

% imshow(g);

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','cyan'); 我运行没问题呀.你的图像是不是有问题,我用的是灰度图像,8位的.

教育之通病是教用脑的人不用手,不教用手的人用脑,所以一无所能。教育革命的对策是手脑联盟,结果是手与脑的力量都可以大到不可思议。

教育之通病是教用脑的人不用手,不教用手的人用脑,所以一无所能。教育革命的对策是手脑联盟,结果是手与脑的力量都可以大到不可思议。

相关文档
相关文档 最新文档