3
votes

Does anyone know how to use the Hough transform to detect the strongest lines in the binary image:

A = zeros(7,7);
A([6 10 18 24 36 38 41]) = 1;

Using the (rho; theta) format with theta in steps of 45° from -45° to 90°. And how do I show the accumulator array in MATLAB as well.

Any help or hints please?

Thank you!

2

2 Answers

5
votes

If you have access to the Image Processing Toolbox, you can use the functions HOUGH, HOUGHPEAKS, and HOUGHLINES:

%# 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

houghlines

1
votes

Each pixel (x,y) maps to a set of lines (rho,theta) that run through it.

  1. Build an accumulator matrix indexed by (rho theta).
  2. For each point (x,y) that is on, generate all the quantized (rho, theta) values that correspond to (x,y) and increment the corresponding point in the accumulator.
  3. Finding the strongest lines corresponds to finding peaks in the accumulator.

In practice, the descritization of the polar parameters is important to get right. Too fine and not enough points will overlap. Too coarse and each bin could correspond to multiple lines.

in pseudo code with liberties:

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