有人知道如何使用霍夫变换在二进制图像中检测最强的直线吗?
A = zeros(7,7);
A([6 10 18 24 36 38 41]) = 1;
使用(rho; theta)格式,其中theta从-45°到90°以45°为步长。我该如何在MATLAB中显示累加器数组呢?
请提供任何帮助或提示。
谢谢!
A = zeros(7,7);
A([6 10 18 24 36 38 41]) = 1;
使用(rho; theta)格式,其中theta从-45°到90°以45°为步长。我该如何在MATLAB中显示累加器数组呢?
请提供任何帮助或提示。
谢谢!
%# 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
每个像素点(x,y)都映射到一组通过它的直线(rho,theta)。
实际上,极坐标参数的离散化很重要。如果过于精细,则会有不足够的点重叠。如果过于粗糙,则每个箱可能对应多条线。
伪代码如下:
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