Hough变换提取车道线
- 图像前处理
clc;clear;close all;
Image=rgb2gray(imread('车道线.png'));
imshow(Image);
figure(2),imhist(Image);
Image = imbinarize(Image,0.90);
figure(3),imshow(Image);
bw=edge(Image,'log');
figure(4),imshow(bw);title("边缘检测后的二值图");
- 绘制Hough曲线并提取交叉点
[h,t,r]=hough(bw,'RhoResolution',1,'Theta',-90:0.1:89.5);
figure(5),imshow(imadjust(mat2gray(h)),'XData',t,'YData',r);
xlabel('\theta'),ylabel('\rho');
axis on,axis normal,hold on;%绘制hough曲线
P=houghpeaks(h,10,"NHoodSize",[31 31],"Threshold",25);
x=t(P(:,2));
y=r(P(:,1));%提取点
plot(x,y,'o','color','r');
- 提取直线并绘制
lines=houghlines(bw,t,r,P,'FillGap',100,'Minlength',30);
figure(6),imshow(Image);
hold on;
max_len=0;
for i=1:length(lines)
xy=[lines(i).point1;lines(i).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','g');
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','y');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','r');
end
Hough提取车道线并不是一种好的方法,个人认为只具有学习意义。
事实上道路总是靠下,摄像头上部分的区域是无效内容,太靠左太靠右也是无效内容。所以可以对图像进行裁剪,例如裁剪为三角形区域内有效,或者图像矩形的下半部分有效。