why hough transform detects more lines while there is only one line?

%% Hough Transform
I4 = imbinarize(I4);
BW2 = imcomplement(I4);
[H,theta,rho] = hough(BW2,'RhoResolution',1.8);
figure(2);
imshow(imadjust(rescale(H)),'XData',theta,...
'YData',rho,...
'InitialMagnification','fit');
axis on
axis normal
hold on
colormap(gca,hot)
P = houghpeaks(H,40,'threshold',ceil(0.05*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
lines = houghlines(BW2,theta,rho,P,'FillGap',3,'MinLength',36);
figure(1);subplot(2,2,4);
imshow(I4),
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');
end

 Accepted Answer

Matt J
Matt J on 2 Jun 2021
Edited: Matt J on 2 Jun 2021
Because your line is actually not a line, but rather a tube with non-zero thickness, there are multiple thinner lines that will fit inside it. The number that are found depend on the boundaries of your rho and theta bins.
You can use clusterdata or uniquetol to group the redundant line tip detections into single points.

More Answers (0)

Asked:

on 1 Jun 2021

Edited:

on 2 Jun 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!