gpt4 book ai didi

python - Scipy 二进制关闭 - 边缘像素失去值(value)

转载 作者:太空狗 更新时间:2023-10-30 01:09:18 24 4
gpt4 key购买 nike

我正在尝试填充二进制图像中的孔。图片比较大,所以我把它分成几 block 进行处理。

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

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

最佳答案

是的。

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

这应该可以做到,而不需要将图像切碎。例如:

输入图片:

enter image description here

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

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

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

关于python - Scipy 二进制关闭 - 边缘像素失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14385921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com