如何使用 Python/Opencv 连接二进制图像中的断线 [英] How to connect broken lines in a binary image using Python/Opencv

查看:84
本文介绍了如何使用 Python/Opencv 连接二进制图像中的断线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使这些线在目标点连接?该图像是骨架化过程的结果.

我正在尝试使用分水岭变换将每条线分割为一个区域.

解决方案

注意间隙是如何闭合的,同时保持不同的水平线

以及被侵蚀的图像:

要去除由扩张/侵蚀产生的伪影,我建议再次提取骨架.
如果进一步对侵蚀图像应用骨架形态学操作,您可以得到以下结果:

一旦您将曲线连接起来,您就不需要使用分水岭分割,而是使用连接的组件来标记每条曲线.

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 连接二进制图像中的断线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆