【图像处理】Hough变换直线检测

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

##################################################

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

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

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



% 入口图像为 BW,出口图像为f
%optimize from main_optimize, merely select 2 lines, one has positive
%slope,the other has negative slope
clear all,close all
BW=imread('road.jpg');
figure,imshow(BW);

BW=rgb2gray(BW);
%thresh=[0.01,0.17];
thresh=[0.01,0.10];
sigma=2;%定义高斯参数
f = edge(double(BW),'canny',thresh,sigma);
figure,subplot(121);
imshow(f,[]);
title('canny Edge Detect Result');

[H, theta, rho]= hough(f, 'Theta',20:0.1:75);%cos(theta)*x+sin(theta)*y=rho
%imshow(theta,rho,H,[],'notruesize'),axis on,axis normal
%xlabel('\theta'),ylabel('rho');

%求极值点
Peaks=houghpeaks(H,5);
%得到线段信息
lines=houghlines(f,theta,rho,Peaks);

% [r,c]=houghpeaks(H,10);
% hold on
% lines=houghlines(f,theta,rho,r,c);

subplot(122);
imshow(f,[]),title('Hough Transform Detect Result'),hold on
nlind=0;%new line index
st=1;
%%%%%%%%%求斜率%%%%%%%%%%%%
for k=1:length(lines)
    %xy=[lines(k).point1;lines(k).point2];
    xielv(k)=(lines(k).point2(1)-lines(k).point1(1))/(lines(k).point2(2)-lines(k).point1(2)+0.0001)
end

%%%%%%%%%将相同斜率的直线连起来%%%%%%%%%%%%
k=1;
while(k<=length(lines))
    if(k~=length(lines))
        k=k+1;
    end
    while(abs(xielv(k)-xielv(k-1))<0.0001)
        k=k+1;
        if(k>length(lines))
            break;
        end
    end

    if(abs(xielv(k-1))<0.05||abs(xielv(k-1))>=10)%eliminate horizontal and vertical lines,防治水平线和楼房
        st=k;
        if(k~=length(lines))
            continue;
        end
    end
    
    if(st==length(lines)&&k==st)
        if(abs(xielv(k))>0.05&&abs(xielv(k))<10)
            nlind=nlind+1;
            newlines(nlind)=lines(st);
            newlines(nlind).point2=lines(k).point2;
            newxy=[newlines(nlind).point1;newlines(nlind).point2];
            plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);
        end
        break;
    end

    %end=k-1,start=st; draw line
    nlind=nlind+1;
    newlines(nlind)=lines(st);
    newlines(nlind).point2=lines(k-1).point2;
    newxy=[newlines(nlind).point1;newlines(nlind).point2];
    plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);

    st=k;
end

fprintf('%d lines are detected in sum.\n',nlind);




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值