6

我正在尝试填补二进制图像中的漏洞。图像相当大,所以我把它分成块进行处理。

当我使用这些scipy.ndimage.morphology.binary_fill_holes功能时,它会填充图像中较大的孔。所以我尝试使用scipy.ndimage.morphology.binary_closing,它给出了填充图像中小孔的预期结果。但是,当我将块重新组合在一起以创建整个图像时,我最终会得到接缝线,因为该binary_closing函数会从每个块的边界像素中删除任何值。

有什么办法可以避免这种影响?

4

2 回答 2

6

是的。

  1. ndimage.label使用(首先反转图像,孔=黑色)标记您的图像。
  2. 找到孔对象切片ndimage.find_objects
  3. 根据您的大小标准过滤对象切片列表
  4. 反转您的图像并binary_fill_holes在符合您标准的切片上执行。

应该这样做,而无需将图像切碎。例如:

输入图像:

在此处输入图像描述

输出图像(中等大小的孔消失了):

在此处输入图像描述

这是代码(设置不等式以删除中等大小的斑点):

import scipy
from scipy import ndimage
import numpy as np

im = scipy.misc.imread('cheese.png',flatten=1)
invert_im = np.where(im == 0, 1, 0)
label_im, num = ndimage.label(invert_im)
holes = ndimage.find_objects(label_im)
small_holes = [hole for hole in holes if 500 < im[hole].size < 1000]
for hole in small_holes:
    a,b,c,d =  (max(hole[0].start-1,0),
                min(hole[0].stop+1,im.shape[0]-1),
                max(hole[1].start-1,0),
                min(hole[1].stop+1,im.shape[1]-1))
    im[a:b,c:d] = scipy.ndimage.morphology.binary_fill_holes(im[a:b,c:d]).astype(int)*255

另请注意,我必须增加切片的大小,以便孔周围有边界。

于 2013-01-17T18:59:30.550 回答
1

涉及来自相邻像素的信息的操作,例如closing边缘总是会遇到问题。在您的情况下,这很容易解决:只需处理比平铺略大的子图像,并在拼接时保留好的部分。

于 2013-01-17T18:59:04.270 回答