Scipy Binary Closing - Edge Pixels失去价值

Bri*_*ian 5 python numpy image image-processing scipy

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

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

有没有办法避免这种影响?

fra*_*xel 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
Run Code Online (Sandbox Code Playgroud)

另请注意,我必须增加切片的大小,以便孔始终具有边框。


tom*_*m10 1

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