Hough lines not reaching the edge of an image

5 views (last 30 days)
I am tryiing to detect all the lines in the following image:
Using the following code I am able to detect almost all lines:
%Find the line detected
I = imread('img3.png');
J = histeq(I);
BW = edge(J,'canny');
[H,T,R] = hough(BW);
P = houghpeaks(H,30,'Threshold',0.7*max(H(:)),'nhoodsize',[5 5]);
lines = houghlines(BW,T,R,P );
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(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
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
This gives the following image
However as you can see the lines do not go all the way to the edge of the image. I have tried shrinking the nhood size but this meant double detection of some lines and no extension of lines to the edge.
Also is it possible to detect the very top and bottom lines? I understand that these lines won't have as many votes due to them being shorter, I've modified the threshold but end up with spurious diagonal lines and still no detection of the top and bottom lines.
Thanks in advance

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 5 Jan 2024
Hi Joe,
I understand that you have applied hough lines detection on an image and were able to detect almost all lines. Further you want to know how to extend these lines and also to detect the remaning lines which were missed.
For the initial concern of extending lines, you can determine where each line intersects with the edges of the image. These intersection points can then be utilized to redraw the extended lines. Here's the MATLAB code that illustrates this process:
I = imread('image.png');
J = histeq(I);
BW = edge(J,'canny');
[H,T,R] = hough(BW);
P = houghpeaks(H,30,'Threshold',0.7*max(H(:)),'nhoodsize',[5 5]);
lines = houghlines(BW,T,R,P);
figure, imshow(I, []), hold on, max_len = 0;
image_height = size(I, 1);
image_width = size(I, 2);
for k = 1:length(lines)
% Calculate the slope (m) and y-intercept (b) of the line
x1 = lines(k).point1(1);
y1 = lines(k).point1(2);
x2 = lines(k).point2(1);
y2 = lines(k).point2(2);
m = (y2 - y1) / (x2 - x1);
b = y1 - m * x1;
% Find intersection with the left border (x=1)
y_left = m * 1 + b;
left_point = [1, y_left];
% Find intersection with the right border (x=image_width)
y_right = m * image_width + b;
right_point = [image_width, y_right];
% Find intersection with the top border (y=1)
x_top = (1 - b) / m;
top_point = [x_top, 1];
% Find intersection with the bottom border (y=image_height)
x_bottom = (image_height - b) / m;
bottom_point = [x_bottom, image_height];
% Check which points are actually within the borders
points = [left_point; right_point; top_point; bottom_point];
points = points(points(:,1) >= 1 & points(:,1) <= image_width & ...
points(:,2) >= 1 & points(:,2) <= image_height, :);
% Sort points by x-coordinate (or y-coordinate if the line is closer to vertical)
if abs(m) > 1
points = sortrows(points, 2); % Sort by y if the line is steep
else
points = sortrows(points, 1); % Sort by x otherwise
end
% Take the two end points for the line segment
point1 = points(1, :);
point2 = points(end, :);
% Plot the extended line
plot([point1(1), point2(1)], [point1(2), point2(2)], 'LineWidth',2,'Color','green');
end
Result
Regarding the secondary part of the query, to detect the remaining lines or refine the detections, you can impose constraints on the angles of detection, filter out lines based on their orientation, and exclude similar lines by considering the proximity between them.
Hope this answers your query.
Regards,
Vinayak Luha

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!