Hough transform incorrectly identifying vertical line segments
6 views (last 30 days)
Show older comments
Hello everyone,
I am trying to measure the lengths of several sequences of vertical line segments. Please see the attached image, "sample_image.jpg," for these sequences. I thought to implement the Hough transform to measure these line segments. The code included at the bottom of this comment contains the implementation. I changed it slightly from the MATLAB reference page regarding the Hough transform, most notably in the way a binary image is generated via edge detection. The fact that these are all 1-pixel lines simplifies the process. However, as seen in the below image, the implementation makes mistakes. The Hough transform incorrectly places lines in midair, and incorrectly assigns multiple lines to just one line.
I am not very experienced with implementing the Hough transform, and am unsure where I'm going wrong. Any assistance would be greatly appreciated!
The code:
fname='sample_image.jpg';
I=imread(fname);
% extremely simple edge finder -- edges are only one pixel wide so all pixels
% with values less than max unit8 value (white) are edges
BW=I<max(I(:));
% Hough transform
[H,theta,rho] = hough(BW);
figure
imshow(imadjust(rescale(H)),[],...
'XData',theta,...
'YData',rho,...
'InitialMagnification','fit');
xlabel('\theta (degrees)')
ylabel('\rho')
axis on
axis normal
hold on
colormap(gca,hot)
%keep max# of peaks overly large to keep script applicable to future images where number of lines is unknown a priori
P = houghpeaks(H,1e6,'threshold',ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
plot(x,y,'s','color','black');
lines = houghlines(BW,theta,rho,P,'FillGap',2,'MinLength',7); %aggressive fillgap exclusion
figure, imshow(I), 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');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
0 Comments
Answers (2)
Constantino Carlos Reyes-Aldasoro
on 17 May 2022
Have you tried inverting the intensities of the image? As it is, white is the main element and that will be difficult to obtain the lines, say that your image i
newimage = max(sample_image(:))- sample_image;
and then apply the hough transform
Constantino Carlos Reyes-Aldasoro
on 17 May 2022
Ok, 2 things, you are first comparing and then subtracting, which may be counteracting, you could calculate hough directly like this:
[H,T,R] = hough(255-sample_image);
Now, where I think the problem lies is the following line
P = houghpeaks(H,1e6,'threshold',ceil(0.3*max(H(:))));
Second parameter is the number of peaks to be found, you should give a number that is in the range of what you want to see, if there are 8 lines, then 10-20 could be good, you are using 1e6, so actually asking to find a million peaks! Try changing that number and see what happens.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!