MATLAB Answers

0

Finish lines in the image

Asked by Mateusz Matysiak on 16 Sep 2019 at 19:52
Latest activity Commented on by Matt J
on 27 Sep 2019 at 15:30
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

Mateusz Matysiak on 18 Sep 2019 at 7:10
I want that there is continuous white line from x=0 point till the last x value = over the entire graph
Matt J
on 20 Sep 2019 at 20:39
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.
Matt J
on 20 Sep 2019 at 21:02
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.

Sign in to comment.

3 Answers

Matt J
Answer by Matt J
on 18 Sep 2019 at 12:00
Edited by Matt J
on 18 Sep 2019 at 12:22
 Accepted Answer

Here is another method based on interpolation, and faster than dilating with large strels.
[m,n]=size(A);
[I,J]=find(bwskel(A));
IJ=sortrows([I,J],2);
[I,J]=deal(IJ(:,1),IJ(:,2));
Jw=(J(1):J(end)).';
Iw=round(interp1(J,I,Jw));
B=imdilate( accumarray([Iw,Jw],1,[m,n]) , strel('sphere',1));
imshow(B)

  6 Comments

Matt J
on 19 Sep 2019 at 16:44
Here's a 3rd iteration of the interpolation based method (Method 2).
[m,n]=size(A);
[I,J]=find(A);
[G,J]=findgroups(J);
I=splitapply(@median, I,G);
Jw=(J(1):J(end)).';
Iw=round(interp1(J,I,Jw));
B=imdilate( accumarray([Iw,Jw],1,[m,n]) , strel('sphere',1));
When I test this on lines2.mat, I get a much better completion than my original slower method.
Method 1:
image1.png
Method 2:
image2.png
Still I struggle to get the correct results. Though I will stick to your first suggestion that works very good for me. Thanks Matt.
Matt J
on 20 Sep 2019 at 16:01
You are welcome, but please Accept-click an answer if you have found one that works for you.

Sign in to comment.


Matt J
Answer by Matt J
on 17 Sep 2019 at 20:58

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)
Capture.PNG

  12 Comments

Can you please give an example Image Analyst?
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 at 15:30
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.

Sign in to comment.


Image Analyst
Answer by Image Analyst
on 18 Sep 2019 at 0:51

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 at 20:41
Image Analyst,
Please explain the imline part. Please give an example.
Thank you

Sign in to comment.