1.Hough Transform 的算法思想
在直角坐标系和极坐标系中,点、线是对偶关系。
即直角坐标系中的点是极坐标系中的线,直角坐标系中的线是极坐标系中的点。反之也成立。
如下图所示,想要检测图像中的直线,可以转化为检测极坐标系中的点
2.Hough空间的表示
如下图所示,图像中直线的表示,由斜率和截距表示,而极坐标中用
r,θ 就是一对Hough空间的变量表示。
旋转的
如下图,直角坐标系中的多个点,对应于
直角坐标系的三点共线,对应于
r -θ 空间的多线共点。
因此,我们可以通过检测
接下来,就是要考虑 将
3.Hough变换代码分析
以下是使用Matlab进行直线检测的代码。
Hough Transform
首先预处理,转为二值图像:
I = imread('road.jpg');
I = rgb2gray(I);
BW = edge(I,'canny');
然后进行霍夫变换:
[H,T,R] = hough(BW,'RhoResolution',0.5,'Theta',-90:0.5:89.5);
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel(' heta'), ylabel('
ho');
axis on, axis normal, hold on;
检测hough域极值点
P = houghpeaks(H,50,'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
lines = houghlines(BW,T,R,P,'FillGap',7,'MinLength',100);
figure, imshow(I), 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');
实验结果
最终车道直线检测结果:
[注] 所有的代码可以在此处免费下载:http://download.csdn.net/detail/ws_20100/9492054