分享

Matlab实现——霍夫变换直线检测(1)

 zqian84 2014-04-04

霍夫变换实现直线检测——MATLAB

网上好多不能用,就重新写了一个:


  1. % 入口图像为 BW,出口图像为f  
  2. clc,close  
  3. BW=imread('D:\picture\9dafa605d53eea243812bb29.jpg');  
  4. BW=rgb2gray(BW);  
  5. thresh=[0.01,0.17];  
  6. sigma=2;%定义高斯参数  
  7. f = edge(double(BW),'canny',thresh,sigma);  
  8. figure(1),imshow(f,[]);title('canny 边缘检测');  
  9. [H, theta, rho]= hough(f, 0.5);  
  10. %imshow(theta,rho,H,[],'notruesize'),  
  11. axis on,  
  12. axis normal  
  13. %xlabel('\theta'),ylabel('rho');  
  14. [r,c]=houghpeaks(H,5);  
  15. hold on  
  16. lines=houghlines(f,theta,rho,r,c);  
  17. figure,imshow(f,[]),title('Hough Transform Detect Result'),  
  18. hold on  
  19. for k=1:length(lines)   
  20.     xy=[lines(k).point1;lines(k).point2];  
  21. plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]);  
  22. end  



Matlab2012版本的内核函数有所改变,houghpeaks, houghlines等函数有所改变,感谢cuicd2011的提醒,R2012a版本的直线检测稍有变化:

  1. clc,close    
  2. BW=imread('D:\picture\9dafa605d53eea243812bb29.jpg');  
  3.     
  4. BW=rgb2gray(BW);    
  5. thresh=[0.01,0.17];    
  6. sigma=2;%定义高斯参数    
  7. f = edge(double(BW),'canny',thresh,sigma);    
  8. figure(1),imshow(f,[]);    
  9. title('canny 边缘检测');    
  10.     
  11. [H, theta, rho]= hough(f,'RhoResolution', 0.5);    
  12. %imshow(theta,rho,H,[],'notruesize'),axis on,axis normal    
  13. %xlabel('\theta'),ylabel('rho');    
  14.     
  15. peak=houghpeaks(H,5);    
  16. hold on    
  17.     
  18. lines=houghlines(f,theta,rho,peak);    
  19. figure,imshow(f,[]),title('Hough Transform Detect Result'),hold on    
  20. for k=1:length(lines)    
  21.     xy=[lines(k).point1;lines(k).point2];    
  22.     plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]);    
  23. end    



实验结果:




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多