实验四:车道线的检测

数字图像处理完整MATLAB代码在我的资源可以看到,为方便下载,下面是百度网盘资源:

链接:https://pan.baidu.com/s/17S7PZJwwvb3PFMFVxqEY5w 
提取码:HUAT

一、实验目的与要求

1、深入学习Hough变换,理解其直线检测的原理;

3、编程实现对所给含有车道线的图像二值图像检测出车道线。

二、实验预习部分

1. hough变换的直线检测原理。

霍夫变换(Hough Transform)是图像处理中的一种特征提取技术,它通过一种投票算法检测具有特定形状的物体。该过程在一个参数空间中通过计算累计结果的局部最大值得到一个符合该特定形状的集合作为霍夫变换结果。

2. 学习《Matlab图像处理教程简易教程》中的“图像分割”下的“Hough变换直线检测”这部分内容,明确在matlab中实现车道线检测的基本过程是什么?

将一个图像读入工作区中,为了使此示例更具说明性,请旋转该图像。显示图像。

使用 edge 函数找到图像中的边缘。

计算由 edge 返回的二值图像的 Hough 变换。

显示由 hough 函数返回的变换 H

使用 houghpeaks 函数,在 Hough 变换矩阵 H 中找到峰值。

在标识了峰值的变换图像上叠加一个绘图。

使用 houghlines 函数查找图像中的线条。

创建一个显示原始图像并在其上叠加线条的绘图。

三、实验内容及步骤

1.在实验三的基础上,在GUI界面增加车道线检测功能菜单项或者控件,显示新增功能如下:

2. 对实验三检测出的边缘图检测车道线

2.1 hough变换检测车道线的程序:

需要直接单独运行此代码!!!

 I1  = imread("C:\Users\A\Desktop\picture\t01bd924934ea9ef63a.jpg");

    I1=imresize(I1,[200 200]);

       I=im2gray(I1);

%     rotI = imrotate(I,33,'crop');

    BW = edge(I,'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');



    % Find lines and plot them

     q  = houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));%4代表四种斜率的所有线段

    lines = houghlines(BW,T,R,q,'FillGap',5,'MinLength',7);

% set(gca, 'XLim',[150,200],'YLim',[1,100]) ;  % X、Y轴的数据显示范围

    figure, imshow(I1),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','cyan');

2.2 hough变换检测结果;

2.3对检测的改进(包括改进算法及编程):

改进算法:

  由于直接用hough变换检测车道线会使得无关道路的线条可能会被检测出来,因此为了减少无关的线条被检测出来,我采取对图像进行掩膜处理,对需要检测的道路部分进行掩膜,其余的无关图像的部分被掩膜舍去,这样不仅减少了检测量,也使得无关的对象不被检测到。

部分代码如下:

不可直接运行,需要运行完整代码才行!!!

global My_GUI_handles;

global houghpeaks_Slope_type;

global houghpeaks_Peak_multiples;

global houghlines_Line_distance;

global houghlines_Line_length;



img_src=getimage(My_GUI_handles.axes_src);

I1=imresize(img_src,[300 300]);

I=im2gray(I1);

%rotI = imrotate(I,33,'crop');

BW = edge(I,'canny');%可以设置使用其他算子

my_mask = mask_rectangle(BW);

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



axes(My_GUI_handles.axes_dst);

imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');

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

hold on

% 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

q  = houghpeaks(H,houghpeaks_Slope_type,'threshold',ceil(houghpeaks_Peak_multiples*max(H(:))));%4代表四种斜率的所有线段*****建议变量10和0.7

lines = houghlines(my_mask,T,R,q,'FillGap',houghlines_Line_distance,'MinLength',houghlines_Line_length);%*********建议变量3和20一起

axes(My_GUI_handles.axes_dst1);

imshow(I1),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','cyan');



axes(My_GUI_handles.axes_src1);

imshow(my_mask);

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值