1 博客内容
近期看matlab(simulink)应用,关注到Github小伙伴分享的车道线识别用例,这里复盘供了解思路。作者:ysshah95,Git链接: https://github.com/ysshah95/Lane-Detection-using-MATLAB.git.
2 车道线识别过程
- 图片信息读取
frame=imread('pic_01.jpg')
- 高斯滤波
frame = imgaussfilt3(frame);
3. 提取车道线
channel1MinYellow = 130;
channel1MaxYellow = 255;
channel2MinYellow = 130;
channel2MaxYellow = 255;
channel3MinYellow = 0;
channel3MaxYellow = 130;
%-----------Create mask based on chosen histogram thresholds-----------
Yellow=((frame(:,:,1)>=channel1MinYellow)|(frame(:,:,1)<=channel1MaxYellow))& ...
(frame(:,:,2)>=channel2MinYellow)&(frame(:,:,2)<=channel2MaxYellow)&...
(frame(:,:,3)>=channel3MinYellow)&(frame(:,:,3)<=channel3MaxYellow);
白色:rgb(255,255,255) 黄色: rgb(255,255,0)
4. ROI感兴趣区域( region of interest)
c = [0 4 10 10 ]
r = [10 0 0 10]
BW = roipoly(frameOrigin,c,r);
5.车道线生成流程
6.车道线探测实例
3 代码脚本
%------ 名称: LaneDetect.m
%------ 作者: ysshah95
%------ 网址: https://github.com/ysshah95/Lane-Detection-using-MATLAB.git
%------ 更新: Morven_Xie
%------ 版本: 1.0
%------ 时间: 2023/3/9 23:49
%------ 功能: 用于提取图片车道线(黄色)
%------ 简介: 简易思路提取车道线供学习参考
%------ Email: morven_xie@163.com
%------------------打开图片文件--------------------------------------------
frame=imread('pic_01.jpg')
%------------------高斯滤波,线性平滑--------------------------------------
frame = imgaussfilt3(frame);
%------------------设置R/G/B留存范围---------------------------------------
channel1MinY = 130;
channel1MaxY = 255;
channel2MinY = 130;
channel2MaxY = 255;
channel3MinY = 0;
channel3MaxY = 130;
%-----------提取图片指定色彩范围的像素区域---------------------------------
Yellow=((frame(:,:,1)>=channel1MinY)|(frame(:,:,1)<=channel1MaxY))& ...
(frame(:,:,2)>=channel2MinY)&(frame(:,:,2)<=channel2MaxY)&...
(frame(:,:,3)>=channel3MinY)&(frame(:,:,3)<=channel3MaxY);
figure('Name','指定区域提取-黄色车道线'), imshow(Yellow);
%-----------像素区域的边缘检测---------------------------------------------
frameY = edge(Yellow, 'canny', 0.2);
figure('Name','黄色车道线边缘检测结果'), imshow(frameY);
%-----------删除少于30个像素的所有连通分量---------------------------------
frameY = bwareaopen(frameY,30);
figure('Name','黄色车道线边缘检测去除噪点结果'), imshow(frameY);
%-----------ROI感兴趣区域(region of interest)提取-------------------------
r =[200 400 1100 1300]
c = [750 300 300 750]
roiY = roipoly(frameY, r, c);
[R , C] = size(roiY);
for i = 1:R
for j = 1:C
if roiY(i,j) == 1
frame_roiY(i,j) = frameY(i,j);
else
frame_roiY(i,j) = 0;
end
end
end
figure('Name','黄色车道线ROi区域提取'), imshow(frame_roiY);
%-----------霍夫变换获得连续直线点-----------------------------------------
[H_Y,theta_Y,rho_Y] = hough(frame_roiY);
P_Y = houghpeaks(H_Y,2,'threshold',2);
lines_Y = houghlines(frame_roiY,theta_Y,rho_Y,P_Y,'FillGap', ...
3000,'MinLength',20);
figure('Name','图片中获取霍夫直线'), imshow(frame), hold on
max_len = 0;
for k = 1:length(lines_Y)
%-----------去除非预期范围夹角直线-----------------------------------------
if(lines_Y(k).theta > 30 && lines_Y(k).theta < 150)
xy = [lines_Y(k).point1; lines_Y(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');
end
end
4 扩展应用
有些可研究地方:
- 相机坐标系和车体坐标系互换;
- 霍夫变换起点识别+滑动窗口+车道线拟合。
有趣应用: