1. 程式人生 > >Matlab實現——霍夫變換直線檢測

Matlab實現——霍夫變換直線檢測

霍夫變換實現直線檢測——MATLAB

網上好多不能用,就重新寫了一個:

% 入口影象為 BW,出口影象為f
clc,close
BW=imread('D:\picture\9dafa605d53eea243812bb29.jpg');
BW=rgb2gray(BW);
thresh=[0.01,0.17];
sigma=2;%定義高斯引數
f = edge(double(BW),'canny',thresh,sigma);
figure(1),imshow(f,[]);title('canny 邊緣檢測');
[H, theta, rho]= hough(f, 0.5);
%imshow(theta,rho,H,[],'notruesize'),
axis on,
axis normal
%xlabel('\theta'),ylabel('rho');
[r,c]=houghpeaks(H,5);
hold on
lines=houghlines(f,theta,rho,r,c);
figure,imshow(f,[]),title('Hough Transform Detect Result'),
hold on
for k=1:length(lines) 
    xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]);
end


Matlab2012版本的核心函式有所改變,houghpeaks, houghlines等函式有所改變,感謝cuicd2011的提醒,R2012a版本的直線檢測稍有變化:

clc,close  
BW=imread('D:\picture\9dafa605d53eea243812bb29.jpg');
  
BW=rgb2gray(BW);  
thresh=[0.01,0.17];  
sigma=2;%定義高斯引數  
f = edge(double(BW),'canny',thresh,sigma);  
figure(1),imshow(f,[]);  
title('canny 邊緣檢測');  
  
[H, theta, rho]= hough(f,'RhoResolution', 0.5);  
%imshow(theta,rho,H,[],'notruesize'),axis on,axis normal  
%xlabel('\theta'),ylabel('rho');  
  
peak=houghpeaks(H,5);  
hold on  
  
lines=houghlines(f,theta,rho,peak);  
figure,imshow(f,[]),title('Hough Transform Detect Result'),hold on  
for k=1:length(lines)  
    xy=[lines(k).point1;lines(k).point2];  
    plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]);  
end  


實驗結果: