# Hough Transform not working to recognize a line

I have an image composed of small white dots that should stay on the same line.

I wanted to recognize this line using the Hough Transform. To do that I binarized the image, calculated the centroid of each dot and therefore I got an image that is all black except a few white pixels in the position of the centroids of the original dots.

Then I applied the hough transform algorithm (already implemented in Matlab), but it looks not capable to fully detect this line. In particular it skips the last dot in the bottom.

Do you know why this is happening? I would like to understand why the hough trasform method is not working properly and if there is any kind of improvement I can apply.

Here is my Matlab code:

BW = imbinarize(data,0.04);

s = regionprops(BW,'centroid');
centroids = cat(1, s.Centroid);
imshow(BW)
hold on
plot(centroids(:,1),centroids(:,2), 'r*')
BW(:,:)=0;
for k=1:size(centroids,1)
BW(round(centroids(k,2)),round(centroids(k,1)))=1;
end

[H,T,R] = hough(BW,'RhoResolution',0.1,'Theta',-90:1:89.9);
P  = houghpeaks(H,5,'Theta',T,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap',10000,'MinLength',1);
figure, imshow(BW), 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(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');

• I don't see a line when I see three dots. You interpret that there is a line! – Marcus Müller Jan 4 '19 at 13:00
• @MarcusMüller the dots are more than three, if you enlarge the image or binarize it's easier to tell. I know for sure that these dots should stay on the same line because I know the physics of the system that gave origin of these dots. – Alessandro Zunino Jan 4 '19 at 13:02
• Yeah, but the fact that they are on a line doesn't mean there is a line! – Marcus Müller Jan 4 '19 at 13:03
• @MarcusMüller I edited the question. You will see that the problem is not the fact that these points are disconnected, but the fact that that I use the centroids, somehow. – Alessandro Zunino Jan 4 '19 at 13:07
• That's exactly what I'm saying. – Marcus Müller Jan 4 '19 at 13:08

What you want is just to find the function

$$y(x) = mx + q$$

that fits through the centroids you've found.

You wouldn't do a Hough transform on that (since Hough transforms generally perform badly in presence of only few representatives of the full line, and are pretty computationally intense), but simply look for something like the least squared error fit of a $$y(x)$$ through the $$(x_i, y_i)$$ tuples given by the coordinates of your centroids.

• Yes, what I already did is exactly a linear fit of the coordinates of these centroids. My question was not about finding an alternative method to the hough transform, but I wanted to understand why the hough trasform method was not working, if there is any kind of improvement I can apply and why there is a difference why I apply it on the simply binarized image and when I apply it with the image with the centroids. – Alessandro Zunino Jan 4 '19 at 13:36
• Well, it doesn't perform well because it's designed to do something different than what you're trying to make it do. There really is only a hint of a line, not a line in your image. – Marcus Müller Jan 4 '19 at 14:14
• Could you please explain this better? If my understanding of the math behind the Hough trasform is correct if I have no other signal in my image except my 5 points even if the peaks in the $\rho\theta$ space will be dim they will still be very well recognizable, because there is no noise at all to cover the signal of my line. – Alessandro Zunino Jan 4 '19 at 14:32
• well, good key word: in your $(\rho, \theta)$ space, the upper four points fall onto one point – and everything is fine; however, the last centroid is offset enough from the theoretical line you're looking for that all connections between that centroid and any other centroid maps to a significantly different point in the $(\rho, \theta)$ plane. Hence, there's not enough power accumulating in these four accumulator space points individually for the Hough algorithm you're using to detect a peak. – Marcus Müller Jan 4 '19 at 14:37
• So, the problem is that the Hough Transform is not able to deal with unprecise alignment of the points and it is expected to detect only ideal lines? – Alessandro Zunino Jan 4 '19 at 14:45