I am working on a project in which I have to complete the broken lines in the image below and then calculate the area. Is there any way to complete the broken lines in the image below using python?
Asked
Active
Viewed 1,241 times
0
-
Did you tried something? – Youness Saadna Aug 15 '20 at 08:50
-
I tried to obtain the skeleton of the image and then join the lines but it does not work so well and also discards the thickness of the lines – a.kamil Aug 15 '20 at 09:16
-
Add some code that you use to your post. – Youness Saadna Aug 15 '20 at 09:17
-
1This shoukd give you an idea... https://stackoverflow.com/a/63414618/2836621 – Mark Setchell Aug 15 '20 at 11:33
-
1could you please also upload the expected result? I did not get it completely in which lines should be merged and which not. – Prefect Aug 16 '20 at 09:52
-
To connect broken lines/edges based on contour extremities have a look at this https://stackoverflow.com/questions/71811385/connecting-disjointed-lines-or-edges-in-binary-images/71816288#71816288 – Jeru Luke Apr 23 '22 at 15:31
1 Answers
3
Do you mean something like this?
This can be done with simple morphology; however, it is subject to two conditions:
- You only have horizontal and vertical lines
- The gaps you want to fill are strictly smaller than gaps you want to preserve (empty space in the image)
import matplotlib.pyplot as plt
import numpy as numpy
from skimage import io
from skimage import morphology
img = io.imread("test.png", as_gray=True)
# fill horizontal gaps
selem_horizontal = morphology.rectangle(1,50)
img_filtered = morphology.closing(img, selem_horizontal)
# fill vertical gaps
selem_vertical = morphology.rectangle(80,1)
img_filtered = morphology.closing(img_filtered, selem_vertical)
plt.imshow(img_filtered, cmap="gray")
plt.gca().axis("off")
plt.show()

FirefoxMetzger
- 2,880
- 1
- 18
- 32