间隙填充轮廓/线 [英] Gap Filling Contours / Lines
问题描述
我有以下图像:
,我想填充其轮廓(即,我想在这张图片中填充线条).
and I would like to fill in its contours (i.e. I would like to gap fill the lines in this image).
我尝试了形态学上的闭合,但是使用大小为3x3
的矩形内核进行10
迭代并不能填满整个边界.我还尝试了1
迭代的21x21
内核,也没有运气.
I have tried a morphological closing, but using a rectangular kernel of size 3x3
with 10
iterations does not fill in the entire border. I have also tried a 21x21
kernel with 1
iteration and also not had luck.
更新:
我已经在OpenCV(Python)中使用以下方法进行了尝试:
I have tried this in OpenCV (Python) using:
cv2.morphologyEx(img, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (21,21)))
和
cv2.morphologyEx(img, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)), iterations=10)
和 scikit图像:
closing(img, square(21))
我的最终目标是在不扭曲所覆盖区域的情况下获得整个图像的填充版本.
My end goal is to a have a filled version of that entire image without distorting the area covered.
推荐答案
在以下代码段中,我计算了逆像的距离图.我将其设置为阈值以获得当前对象的大轮廓,然后对其进行骨架化以获取中心线.这可能已经足以满足您的目的.但是为了使其与给定的线宽一致,我对骨架进行了扩张,然后将其添加到原始骨架中,从而缩小了所有间隙.我还删除了剩下的一个接触边界的物体.
In the following snippet I calculate the distance map of the inverse image. I threshold it to obtain a large outline of the current object, which I then skeletonize to get the central line. This may already be enough for your purposes. But to make it consistent with the line thickness given, I dilate the skeleton and add it to the original, thereby closing any gaps. I also remove the one remaining object touching the boundary.
from skimage import io, morphology, img_as_bool, segmentation
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
image = img_as_bool(io.imread('/tmp/gaps.png'))
out = ndi.distance_transform_edt(~image)
out = out < 0.05 * out.max()
out = morphology.skeletonize(out)
out = morphology.binary_dilation(out, morphology.selem.disk(1))
out = segmentation.clear_border(out)
out = out | image
plt.imshow(out, cmap='gray')
plt.imsave('/tmp/gaps_filled.png', out, cmap='gray')
plt.show()
这篇关于间隙填充轮廓/线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!