connect lines after Hough transform

10 views (last 30 days)
Sami
Sami on 26 Aug 2017
Edited: Sami on 29 Aug 2017
I am working on a image (first image included here) where I need to automatically draw lines by connecting the objects oriented in particular direction (desired image is the one with red lines). I tried to address this using Hough transform. This helped too some extent, but the lines are disconnected (second figure). This might be due to distance between objects in a row. I need to connect these several small lines appearing in a row (second figure). Can someone help me to solve this problem? I am fairly new to matlab, so I would appreciate if someone can share the excerpt of codes to address this.
Also, one thing we can notice in the bottom of the second image is the multiple lines that are not parallel. Since these are unnecessary lines, I want to ignore those areas while drawing the lines. As in large portion of the image, one row runs parallel to the another (i.e., they are y distance apart from each other), can we use this relation to get rid of those nonparallel lines while drawing the lines? If one can, how can we implement this?
I look forward to your comments. Thanks.

Answers (1)

Image Analyst
Image Analyst on 26 Aug 2017
You can burn the lines into the image (see attached demo program) and then either skeletonize the image or remove spurs (both can be done with bwmorph()).
  2 Comments
Sami
Sami on 28 Aug 2017
Thanks Image Analyst for your response. I tried the sample code you provided with very minor tweaks (just changed the file name). It runs, but it is not doing what I am looking for. May be I need to modify it significantly.
Below is the code I am working with.
% read and show the image
BW_Img = imread(OImage);
figure, imshow(BW_Img);
% apply canny edge detectors to the image to detect edges prior to the
% application of Hough transform
BWEd=edge(BW_Img, 'canny');
figure, imshow(BWEd);
%Create the Hough transform using the binary image.
%Source: https://www.mathworks.com/help/images/ref/houghlines.html
[H,T,R] = hough(BWEd);
imshow(H,[],'XData',T,'YData',R,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
%Find peaks in the Hough transform of the image.
% need to change number e.g., 15 to increase the lines
P = houghpeaks(H,20,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
%Find lines and plot them.
lines = houghlines(BW_Img,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(BW_Img), 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
This code just does the part of the tasks I am planning to accomplish. I now need to fill the gaps in between the lines. I think I need to connect each endpoint to the closest other endpoint that it is not already connected, and try to smooth the lines afterwards (I want them to be straight lines if they are irregular). I don't know how to do it. Can you please show a path forward here? It might be a easy fix for you. Thanks.
Sami
Sami on 29 Aug 2017
Edited: Sami on 29 Aug 2017
Image Analyst, I was reading through the another post about filling gaps in between lines to make a continuous lines where you made this suggestion "Skeletonize the white blobs. Then find the endpoints with bwmorph(,'endpoints'). Connect each endpoint to the closest other endpoint that it is not already connected to by using imline.createmask(). I think this might help me address my problem.
Can you please help me by providing a demo on this? As you suggested, I think it might need to be implemented inside the "for loop" before displaying the lines in my program. I have no idea where/how to do this. So, your demo would be of great help. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!