How to delete the branches?

111.png
I have a matrix which just consist by 0 and 1, like the figure above (1 is black and 0 is white). It's kind of river, but there is some noise like the red arrows above.
Is there some ways to identify these small segment lines and delete these small segment lines?
Calculating the length (pixel) of these small segment lines is not necessary, but I am interested to know it.
Any suggestion will be appreciated.

 Accepted Answer

rui gao
rui gao on 23 Sep 2019
Maybe I can delete these segment lines in ArcGIS as the small segment lines can be identified as short length.

4 Comments

Is it solution using MATLAB?
No. I have tried to use a small window to filter it, but it takes a long time, and I am not sure the whether the result can achieve my goal or not.
Initially, I thought it could be solved by MATLAB. But now, I reckon that ArcGIS can address it efficiently although there is some converting process which take some time.
I am working on this now. I will let you know if you are interested in this.
See if bwmorph can manage it
I1 = bwmorph(I,'spur',inf)
WeChat Image_20190923190308.png
Oops, for this kind of situation, the left red line should be remained, but it was deleted.
Maybe, we should add something ???

Sign in to comment.

More Answers (1)

darova
darova on 24 Sep 2019
I have an idea:
  • Find pairs of branch/end points smaller than desired length
  • Select circle area between points. Find biggest area in this circle (if there are) and remove it
  • Place modificated circle area into original image
1Untitled.png
See the attached script

12 Comments

Thanks for your suggestions. I will try it later, and let you know the result of mine.
I am sorry for the delayed response.
What you showed me is a really amazing work for me. Thanks.
How about the situation I attached here, "test_denoise.jpg"? I test it with the code you provided, and because of the complicated situations, this code cannot achieve the goal I want.
Additionally, I draw several lines in Paint (ShouldBe.png) to show what the final result looks like. (It's a kind of river network)
I am not familiar with this field, and I am trying to test some thoughts that you inspired me. Any further advice would be appreciated.
Please upload testnoise.jpg. There is something wrong with image
Sorry for that. This should work.
What about this simple way?
clc,clear
I0 = imread('test_denoise.png');
I1 = imdilate(I0,ones(30)); % make lines thick
I2 = bwmorph(I1,'thin',100); % thin lines
I00 = 255*cat(3,I0,I0*0,I0*0);
I11 = 255*cat(3,I1*0,I1*0,I1);
I22 = 255*cat(3,I2*0,I2,I2*0);
I = I00 + I11 + I22;
imshow(I)
imwrite(I,'test_denoise11.png')
The code produces:
test_denoise6_fin.png
RED - original image
BLUE - image after imdilate()
CYAN - final image
Hi Darova,
Thanks for your reply, and it is an option. Compared with your result, the similar thing I did in Python is not acceptable.
But I was wondering whether we can do something focusing on the branch points. I attached the figure to show the main channel based on the result you got. For other thin red lines, we can reckon that they are noise. My untenable thought is:
  1. find all the branch/end points and delete a part of lines like you suggested before.
  2. find pairs of branch/end points again, and delete the line which is smaller than a threshold. Finally, we can get a skeleton, but not connected.
  3. set a threshold and connect those segment lines.
In step 2, we may delete a part of the main channel that I showed in "example.png", but most (all) part of small segment lines can be deleted.
After step 3, we may get a network like I showed in "example.png" (red arrows)
Oops~
Now, I thought these 3 steps below should work:
  1. find pairs of branch points, and delete the line between these two branch points if the distance is smaller than a threshold;
  2. using a loop to delete the line whose length is smaller than another threshold until it cannot find any satisfying endpoints;
  3. connect endpoints (I suppose I have finished this part).
Keep going ...
Maybe separate lines and remove smallest. Add or remove some lines manually
Fill small gaps between lines using imdilate() and bwmorph()('thin' operation
gif_animation.gif denoise_final.png
"example_3.png" is what I got now.
I am still working on gaining a better result.
Hi Darova, could you please send me the code for this processing?
  • Manual processing may not be accepted, because I need a tons of similar processing.
  • Some flaws are acceptable even there are two endpoints which should be connected
Sure, see attached script
Just pick some points to add lines (press Enter to finish)
Thank you so much.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 23 Sep 2019

Commented:

on 4 Oct 2019

Community Treasure Hunt

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

Start Hunting!