问题描述
如何使这些线在目标点连接?该图像是骨架化过程的结果.
我正在尝试使用分水岭变换将每条线分割为一个区域.
注意间隙是如何闭合的,同时保持不同的水平线
以及被侵蚀的图像:
要去除由扩张/侵蚀产生的伪影,我建议再次提取骨架.
如果进一步对侵蚀图像应用骨架形态学操作,您可以得到以下结果:
一旦您将曲线连接起来,您就不需要使用分水岭分割,而是使用连接的组件来标记每条曲线.
How can I make these lines connect at the target points? The image is a result of a skeletonization process.
I'm trying to segment each line as a region using Watershed Transform.
MikeE's answer is quite good: using dilation and erosion morphological operations can help a lot in this context.
I want to suggest a little improvement, taking advantage of the specific structure of the image at hand. Instead of using dilation/erosion with a general kernel, I suggest using a horizontal kernel that will connect the endpoints of the horizontal lines, but will not connect adjacent lines to one another.
Here's a sketch of code (assuming the input image is stored in bw
numpy 2D array):
import cv2, numpy as np
kernel = np.ones((1,20), np.uint8) # note this is a horizontal kernel
d_im = cv2.dilate(bw, kernel, iterations=1)
e_im = cv2.erode(d_im, kernel, iterations=1)
What you get is the dilated image:
Note how the gaps are closed, while maintaining the distinct horizontal lines
And the eroded image:
To remove artifacts created by dilate/erode, I suggest to extract the skeleton again.
If you further apply skeleton morphological operation to the eroded image you can get this result:
Once you have the curves connected you do not need to use watershed segmentation, but rather use connected components to label each curve.
这篇关于如何使用 Python/Opencv 连接二进制图像中的断线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!