Scipy二进制闭合边缘像素丢失值

2024-09-30 05:16:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在二值图像中填充漏洞。图像相当大,所以我把它分成几块进行处理。在

当我使用scipy.ndimage.morphology.binary_fill_holes函数时,它会填充图像中更大的洞。因此,我尝试使用scipy.ndimage.morphology.binary_closing,它给出了填充图像中小孔的理想结果。但是,当我把这些块放回一起时,为了创建整个图像,我最终得到的是缝合线,因为binary_closing函数会从每个块的边界像素中删除任何值。在

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


Tags: 函数图像scipyfill边界holesbinaryclosing
2条回答

是的。在

  1. 使用ndimage.label标记图像(首先反转图像,holes=black)。在
  2. 使用ndimage.find_objects查找孔对象切片
  3. 根据大小条件筛选对象切片列表
  4. 反转图像并对符合条件的切片执行binary_fill_holes。在

这样就可以了,而不需要将图像切碎。例如:

输入图像:

{1美元^

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

enter image description here

下面是代码(设置不等式以删除中等大小的blob):

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

还需要注意的是,我必须增加切片的大小,这样孔就可以在所有的方向上都有边界。在

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

相关问题 更多 >

    热门问题