图像分割之霍夫变换
概念
霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进算法。最基本的霍夫变换是从黑白图像中检测直线(线段)。
霍夫变换最简单的是检测直线。我们知道,直线的方程表示可以由斜率k和截距b表示(这种表示方法,称为斜截式,也就是高中的时候学习到的一种常用形式),如下所示:
y=kx+b
用参数空间表示则为(b,k),即,我们可以用斜率和截距就能表示一条直线。但这种形式会产生一个问题,那就是当我们的直线斜率k为无限大的时候(即垂线),这会使得该直线无法使用斜截式来进行表示,此处我们需要使用到另一种直线的表现形式:黑塞法线式(或者简称为法线式):
r=xcosθ+ysinθ
其中r是原点到直线上最近点的距离(其他人可能把这记录为ρ,下面也可以把r看成参数ρ,两者含义相同),θ是x轴与连接原点和最近点直线之间的夹角。如下图所示。
我们可以将图像的每一条直线与一对参数(r,θ)相关联。由参数(r,θ)构成的平面有时被称为霍夫空间,用于表示二维直线所构成的集合。
经过Hough变换后,我们笛卡尔坐标系中的一个点可以映射到Hough空间中去,如下图所示。
提取飞机跑道的主跑道,并将其连接强调,效果如下:
原始图像:
处理后:
代码如下:
inf = imread('Fig1034(a)(marion_airport).tif');
imshow(inf)
f = edge(inf , 'canny', [0.05 0.15] , 2);
subplot(221),imshow(f)
[H, theta , rho] = hough(f);
subplot(222),imshow(H, [], 'XData', theta, 'YData', rho,'InitialMagnification','fit')
axis on , axis normal
xlabel('\theta'),ylabel('\rho')
peaks_1 = houghpeaks(H ,4);
hold on
plot(theta(peaks_1(:,2)), rho(peaks_1(:,1)) , ...
'linestyle', 'none', 'marker','s', 'color', 'w');
[H, theta , rho] = hough(f,'Theta',1:0.1:3);
peaks_1 = houghpeaks(H ,4);
lines = houghlines(f , theta ,rho , peaks_1,'FillGap',55,'MinLength',1);
maxLength = 0;
subplot(223),hold on
for k=1:length(lines)
if(k==1)
xy = [lines(k).point1 ; lines(k).point2];
plot(xy(:,1) , -xy(:,2), 'LineWidth', 1 , 'Color', [.0 .0 .0])
axis([0 564 -502 0]);
end
if(k==6)
xy = [lines(k).point1 ; lines(k).point2];
plot(xy(:,1) , -xy(:,2), 'LineWidth', 1 , 'Color', [.0 .0 .0])
axis([0 564 -502 0]);
end
end
subplot(224),imshow(inf),hold on
for k=1:length(lines)
if(k==1)
xy = [lines(k).point1 ; lines(k).point2];
plot(xy(:,1) , xy(:,2), 'LineWidth', 1 , 'Color', [.0 .0 .0])
end
if(k==6)
xy = [lines(k).point1 ; lines(k).point2];
plot(xy(:,1) , xy(:,2), 'LineWidth', 1 , 'Color', [.0 .0 .0])
end
end