How can I automatically identify multiple lines in an image?

Given a binary image containing angled lines, how can I automatically identify as many lines as possible? Using the function bwtraceboundaryin Matlab, I was able to identify one of them, manually providing the initial coordinates of the identified string.

Can someone specify a way to loop the matrix of them and zeros to automatically identify as many as possible?

Here is an example image:

enter image description here

% Read the image
I = imread('./synthetic.jpg');


figure(1)
BW = im2bw(I, 0.7);
imshow(BW2,[]);
c = 255; % X coordinate of a manually identified line
r = 490; % Y coordinate of a manually identified line
contour = bwtraceboundary(BW,[c r],'NE',8, 1000,'clockwise');
imshow(BW,[]);
hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',2); 

From the above code we get:

enter image description here

+4
source share
1 answer

This is a small example of how to use the Hough transform for strings in MATLAB, with some clutter in front of your images.

, , , , StackOverflow. , - :

I=rgb2gray(imread('https://i.stack.imgur.com/fTWHh.jpg'));

I = imgaussfilt(I,1);
I=I([90:370],:);
BW = edge(I,'canny');
[H,T,R] = hough(BW);
P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',3);

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

enter image description here

+3

Source: https://habr.com/ru/post/1695352/


All Articles