Finish lines in the image
Show older comments
Dear all,
I have an image like the one attached. I have it in binary format.

How do I complete the white lines so the first lines crosses x=0 point, and the remaining lines are finished until they cross each other?
This script should be flexible enough so the number of lines may change (more gaps) and they don't necessarily are lines. They may also be curves.
Here is another example where there are more gaps in data.

Thank you for your help!
5 Comments
KALYAN ACHARJYA
on 17 Sep 2019
Are you want to link those white lines?
Mateusz Matysiak
on 18 Sep 2019
Mateusz Matysiak
on 18 Sep 2019
Matt J
on 20 Sep 2019
Mateusz Matysiak's comment moved here:
The lines should look like below. Initially I was looking into creating multiple linear fit lines. Then, I create the complete lines based on the fits and cross points between the fits.
The picture below shows how it should look like. I finished the lines by hand. The script should do the same.

This script should be flexible enough so the number of lines may change (more gaps) and they don't necessarily are lines. They may also be curves.
You mentioned here that the pieces might be curved (not lines) and so naive line-fitting wouldn't work. Please show an example of this more complicated case and how you want the curves extended.
Accepted Answer
More Answers (2)
Matt J
on 17 Sep 2019
Perhaps as follows,
gapsize=36;
A=imcrop(rgb2gray(imread('image.png')));
B=(imdilate(A,strel('sphere',ceil(gapsize/2)))>0);
C=imdilate(bwskel(B),strel('sphere',1))|A;
imshow(C)

12 Comments
KALYAN ACHARJYA
on 17 Sep 2019
+1 Nice one sir
Stelios Fanourakis
on 17 Sep 2019
I use exactly the same example but it stucks on running and I needed to pause it. Do you know if this take a lot of time to compute?
Stelios Fanourakis
on 17 Sep 2019
I'm not using the imcrop line. I use those lines
x = imread('1pp.bmp');
>> gapsize=36;
B=(imdilate(x,strel('sphere',ceil(gapsize/2)))>0);
C=imdilate(bwskel(B),strel('sphere',1))|x;
imshow(C)
But it stucks and still I have to shut it down
Stelios Fanourakis
on 17 Sep 2019
It just stopped, providing a very nice result. However, the big gap in my curve still exist, although smaller gaps were filled and connected. The bigger the gapsize I will assign, the bigger gaps will be connected?
Matt J
on 17 Sep 2019
Yes.
Stelios Fanourakis
on 17 Sep 2019
Yes it's perfect. Thanks a ton Matt. It is exactly what I needed. Although it takes more than 2 minutes in my case and I don't know the reason it does take so long.
Image Analyst
on 20 Sep 2019
Or use traditional edge linking baed on linking closest endpoints, which should be even faster.
Stelios Fanourakis
on 27 Sep 2019
Can you please give an example Image Analyst?
Stelios Fanourakis
on 27 Sep 2019
Matt how your code can be used in my image linking the first white pixel lines from the bottom of the image or the last white pixel lines (lines that look like dots) as you see the image from top to bottom.
I attach the image
Matt J
on 27 Sep 2019
I don't understand how the linking is supposed to work. I suspect, in any case, that it will require a very different method, since it is a very different kind of image. A new thread may be a good idea.
Image Analyst
on 18 Sep 2019
1 vote
You might look to the "edge linking" link to the right for alternative methods. The method I like, which may be faster than dilation followed by skeletonization is to call bwmorph(bw, 'endpoints') and then use imline() to draw a line connecting endpoints closer than some distance that you specify.
1 Comment
Mateusz Matysiak
on 21 Sep 2019
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
