opencv - 填充形状边缘中的间隙

标签 opencv image-processing

有没有一种算法可以很好地填充示例图像上的孔洞?膨胀效果不佳,因为在我最终设法连接这些曲线的末端之前,曲线变得非常粗。我想避免加粗线条。感谢您的帮助。

enter image description here

是的,图像中可以有任何字母或形状有这样的孔。

最佳答案

另一种更简单的方法可能会更好地转化为 OpenCV,因为它使用卷积而不是顺序 Perl/C 代码。

基本上将所有黑色像素的值设置为 10,将所有白色像素的值设置为 0,然后使用以下 3x3 内核对图像进行卷积:

1  1  1
1  10 1
1  1  1

现在,内核中间的黑色像素将给出 100 (10x10),邻域中的任何其他黑色像素将给出 10 (10x1)。因此,如果我们想要具有中心黑色像素且只有一个相邻黑色像素的点,则其值为 110 (100+10)。因此,让我们用红色为所有值为 110 的像素着色。这给出了这个命令:

convert EsmKh.png -colorspace gray -fill gray\(10\) -opaque black -fill gray\(0\) -opaque white -morphology convolve '3x3: 1,1,1 1,10,1 1,1,1' -fill red -opaque gray\(110\) out.png

生成的图像(您可能需要放大间隙才能看到红色):

enter image description here

如果您想要红色像素列表,请将输出文件名替换为 txt: 并像这样搜索:

convert EsmKh.png -colorspace gray -fill rgb\(10,10,10\) -opaque black -fill rgb\(0,0,0\) -opaque white -morphology convolve '3x3: 1,1,1 1,10,1 1,1,1' txt: | grep "110,110,110"

给出:

86,55: (110,110,110)  #6E6E6E  grey43
459,55: (110,110,110)  #6E6E6E  grey43
83,56: (110,110,110)  #6E6E6E  grey43
507,59: (110,110,110)  #6E6E6E  grey43
451,64: (110,110,110)  #6E6E6E  grey43
82,65: (110,110,110)  #6E6E6E  grey43
134,68: (110,110,110)  #6E6E6E  grey43
519,75: (110,110,110)  #6E6E6E  grey43
245,81: (110,110,110)  #6E6E6E  grey43
80,83: (110,110,110)  #6E6E6E  grey43
246,83: (110,110,110)  #6E6E6E  grey43
269,84: (110,110,110)  #6E6E6E  grey43
288,85: (110,110,110)  #6E6E6E  grey43
315,87: (110,110,110)  #6E6E6E  grey43
325,87: (110,110,110)  #6E6E6E  grey43
422,104: (110,110,110)  #6E6E6E  grey43
131,116: (110,110,110)  #6E6E6E  grey43
524,116: (110,110,110)  #6E6E6E  grey43
514,117: (110,110,110)  #6E6E6E  grey43
122,118: (110,110,110)  #6E6E6E  grey43
245,122: (110,110,110)  #6E6E6E  grey43
76,125: (110,110,110)  #6E6E6E  grey43
456,128: (110,110,110)  #6E6E6E  grey43
447,129: (110,110,110)  #6E6E6E  grey43
245,131: (110,110,110)  #6E6E6E  grey43
355,135: (110,110,110)  #6E6E6E  grey43
80,146: (110,110,110)  #6E6E6E  grey43
139,151: (110,110,110)  #6E6E6E  grey43
80,156: (110,110,110)  #6E6E6E  grey43
354,157: (110,110,110)  #6E6E6E  grey43
144,160: (110,110,110)  #6E6E6E  grey43
245,173: (110,110,110)  #6E6E6E  grey43
246,183: (110,110,110)  #6E6E6E  grey43
76,191: (110,110,110)  #6E6E6E  grey43
82,197: (110,110,110)  #6E6E6E  grey43
126,200: (110,110,110)  #6E6E6E  grey43
117,201: (110,110,110)  #6E6E6E  grey43
245,204: (110,110,110)  #6E6E6E  grey43
248,206: (110,110,110)  #6E6E6E  grey43
297,209: (110,110,110)  #6E6E6E  grey43
309,210: (110,110,110)  #6E6E6E  grey43

现在您可以处理红点列表,并为每个红点找到最近的其他红点并用一条直线将它们连接起来 - 或者如果您感觉非常热衷,可以进行一些曲线拟合。当然,可能需要做一些改进,您可能希望设置一个最大长度的间隙填充线。

关于opencv - 填充形状边缘中的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26586123/

相关文章:

c++ - cv::add 在 openCV 中不起作用

matlab - 如何在彩色图像上使用 histeq 函数?

image-processing - 有没有办法强制 PMULHRSW 将 0x8000 视为 1.0 而不是 -1.0?

image-processing - 使用 TensorFlow 训练时图像大小重要吗?

ios - 图像过滤

c# - 如何在C#的Open CV中使USB摄像机工作?

python - 使用python获取图像中形状的轮廓(x,y)坐标

c++ - OpenCV 2.3 Mat 访问单 channel 矩阵元素

python - 从 np.array 保存到 JPG 时,cv2.imwrite 改变颜色

image - 部分图片无法在 IE7 或 IE8 中显示