打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Matlab实现——霍夫变换直线检测(2)

在上一篇博客中发现经过霍夫变换检测出的直线有可能因为车辆挡住路沿等原因断开,形成线段,这样就不好了,因为检测道路是要找直线焦点。

Thus it is necessary to combine 相同斜率的直线 and connect them.

本代码提供了matlab下求取经过霍夫变换的直线斜率,并将其联合,代码见下方,实验结果见文末。


  1. % 入口图像为 BW,出口图像为f  
  2. %optimize from main_optimize, merely select 2 lines, one has positive  
  3. %slope,the other has negative slope  
  4. clear all,close all  
  5. BW=imread('D:\Images\NEW\img4b9faef664e03.jpg');  
  6. figure,imshow(BW);  
  7.   
  8. BW=rgb2gray(BW);  
  9. %thresh=[0.01,0.17];  
  10. thresh=[0.01,0.10];  
  11. sigma=2;%定义高斯参数  
  12. f = edge(double(BW),'canny',thresh,sigma);  
  13. figure,subplot(121);  
  14. imshow(f,[]);  
  15. title('canny Edge Detect Result');  
  16.   
  17. [H, theta, rho]= hough(f, 0.1);%cos(theta)*x+sin(theta)*y=rho  
  18. %imshow(theta,rho,H,[],'notruesize'),axis on,axis normal  
  19. %xlabel('\theta'),ylabel('rho');  
  20.   
  21. [r,c]=houghpeaks(H,10);  
  22. hold on  
  23.   
  24.   
  25. lines=houghlines(f,theta,rho,r,c);  
  26.   
  27. subplot(122);  
  28. imshow(f,[]),title('Hough Transform Detect Result'),hold on  
  29. nlind=0;%new line index  
  30. st=1;  
  31. %%%%%%%%%求斜率%%%%%%%%%%%%  
  32. for k=1:length(lines)  
  33.     %xy=[lines(k).point1;lines(k).point2];  
  34.     xielv(k)=(lines(k).point2(1)-lines(k).point1(1))/(lines(k).point2(2)-lines(k).point1(2)+0.0001)  
  35. end  
  36.   
  37. %%%%%%%%%将相同斜率的直线连起来%%%%%%%%%%%%  
  38. k=1;  
  39. while(k<=length(lines))  
  40.     if(k~=length(lines))  
  41.         k=k+1;  
  42.     end  
  43.     while(abs(xielv(k)-xielv(k-1))<0.0001)  
  44.         k=k+1;  
  45.         if(k>length(lines))  
  46.             break;  
  47.         end  
  48.     end  
  49.   
  50.     if(abs(xielv(k-1))<0.05||abs(xielv(k-1))>=10)%eliminate horizontal and vertical lines,防治水平线和楼房  
  51.         st=k;  
  52.         if(k~=length(lines))  
  53.             continue;  
  54.         end  
  55.     end  
  56.       
  57.     if(st==length(lines)&&k==st)  
  58.         if(abs(xielv(k))>0.05&&abs(xielv(k))<10)  
  59.             nlind=nlind+1;  
  60.             newlines(nlind)=lines(st);  
  61.             newlines(nlind).point2=lines(k).point2;  
  62.             newxy=[newlines(nlind).point1;newlines(nlind).point2];  
  63.             plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);  
  64.         end  
  65.         break;  
  66.     end  
  67.   
  68.     %end=k-1,start=st; draw line  
  69.     nlind=nlind+1;  
  70.     newlines(nlind)=lines(st);  
  71.     newlines(nlind).point2=lines(k-1).point2;  
  72.     newxy=[newlines(nlind).point1;newlines(nlind).point2];  
  73.     plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);  
  74.   
  75.     st=k;  
  76. end  
  77.   
  78. fprintf('%d lines are detected in sum.\n',nlind);  



实验结果:

原图:


未优化的霍夫变换:


优化后:


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
OpenCV函数cvHoughLines2进行Hough变换
!!霍夫变换原理
opencv简单的霍夫线变换应用,得到物体角度
Hough变换-理解篇
矩阵计算-MATLAB-doolittle分解
高中物理实验:实验数据常用的处理方法汇总
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服