如何使用Python / Opencv连接二进制图像中的虚线 [英] How to connect broken lines in a binary image using Python/Opencv
问题描述
如何在目标点连接这些线?图像是骨架化过程的结果。
我正在尝试使用分水岭变换将每一行分割为一个区域。
注意间隙是如何关闭的,同时保持间隙不同的水平李nes
受侵蚀的图片:
如果您进一步将骨架形态学操作应用于侵蚀图像,您可以得到以下结果:
连接曲线后,您不需要使用分水岭分割,而是使用连通分量标记每条曲线。
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)
to remove artifacts created by dialte/erode, I suggest to extract the skeleton again
What you get is the dilated image:
Note how the gaps are closed, while maintaining the distinct horizontal lines
And the eroded image:
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连接二进制图像中的虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!