MATLAB中的霍夫变换

3
有人知道如何使用霍夫变换在二进制图像中检测最强的直线吗?
A = zeros(7,7);
A([6 10 18 24 36 38 41]) = 1;

使用(rho; theta)格式,其中theta从-45°到90°以45°为步长。我该如何在MATLAB中显示累加器数组呢?

请提供任何帮助或提示。

谢谢!

2个回答

5
如果您可以访问图像处理工具箱,您可以使用HOUGHHOUGHPEAKSHOUGHLINES函数:
%# your binary image
BW = false(7,7);
BW([6 10 18 24 36 38 41]) = true;

%# hough transform, detect peaks, then get lines segments
[H T R] = hough(BW);
P  = houghpeaks(H, 4);
lines = houghlines(BW, T, R, P, 'MinLength',2);

%# show accumulator matrix and peaks
imshow(H./max(H(:)), [], 'XData',T, 'YData',R), hold on
plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2);
xlabel('\theta'), ylabel('\rho')
axis on, axis normal
colormap(hot), colorbar

%# overlay detected lines over image
figure, imshow(BW), hold on
for k = 1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end
hold off

hough lines


1

每个像素点(x,y)都映射到一组通过它的直线(rho,theta)。

  1. 建立一个由(rho theta)索引的累加器矩阵。
  2. 对于每个点(x,y),生成与之对应的所有量化(rho,theta)值,并增加累加器中相应点的计数。
  3. 找到最强的线对应于在累加器中找到峰值。

实际上,极坐标参数的离散化很重要。如果过于精细,则会有不足够的点重叠。如果过于粗糙,则每个箱可能对应多条线。

伪代码如下:

accum = zeros(360,100);
[y,x] = find(binaryImage);
y = y - size(binaryImage,1)/2;  % use locations offset from the center of the image
x = x - size(binaryImage,2)/2;
npts = length(x);
for i = 1:npts
    for theta = 1:360  % all possible orientations
        rho = %% use trigonometry to find minimum distance between origin and theta oriented line passing through x,y here
        q_rho = %% quantize rho so that it fits neatly into the accumulator %% 
        accum(theta,rho) = accum(theta,rho) + 1;
    end
end

谢谢!你知道如何显示累加器数组吗?我可以使用 imshow(accum); 吗? - Glove
你知道如何在图像中显示最强的线条吗? - Glove
最强的线是与累加器中具有最多证据的点相对应的那条线。[best_th,best_rho] = ind2sub(size(accum),max(accum(:)); - BlessedKey
你不应该检查所有360度,只需要180度。 - Cameron Lowell Palmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接