【问题标题】:How to completely fill holes to an image using the skimage?如何使用 skimage 完全填充图像的孔?
【发布时间】:2017-10-12 10:29:20
【问题描述】:

我正在使用 skimage 填充二进制输入图像(左侧)的孔。这是我的代码

from skimage import ndimage
img_fill_holes=ndimage.binary_fill_holes(binary).astype(int)

但是,上述代码的结果并不能完全填补二进制图像的漏洞。这意味着输出仍然是圆圈内的孔。我怎样才能完全填满圆圈内的所有洞?

【问题讨论】:

    标签: python-2.7 image-processing scikit-image


    【解决方案1】:

    您可能尝试在 RGB 图像上应用 binary_fill_holes(3D 矩阵,请使用 binary.shape[2] == 3 进行检查)。

    尝试:

    img_fill_holes = ndimage.binary_fill_holes(binary[:,:,0]).astype(int)
    

    或任何其他“rgb2gray”方法,您应该得到预期的输出

    【讨论】:

    • 我有一个数组是 (301, 512,512) ,我可以对我的数组执行类似的操作吗?
    • 我刚试过这个,skimage 中似乎没有 ndimage 子包。我尝试导入skimage.ndimage,它说没有这样的包。但是我确实在 SciPy 中找到了它:docs.scipy.org/doc/scipy-0.16.0/reference/generated/… 并且输入和输出是相同的。
    • 这只会切片 3d 数组的第一行,请解释如何处理整个 3d nparry
    【解决方案2】:

    如果您有 3D 数组,只需将 structure 更改为 3d 数组。

    例如:

    import SimpleITK as sitk
    from scipy import ndimage
    brain_mask_raw = sitk.ReadImage('/PATH/TO/brain_mask.nii.gz')
    brain_mask_array = sitk.GetArrayFromImage(brain_mask_raw)
    brain_mask_array_filled = ndimage.binary_fill_holes(brain_mask_array, structure=np.ones((3,3,3))).astype(int)
    brain_mask_filled = sitk.GetImageFromArray(brain_mask_array_filled.astype(brain_mask_array.dtype)).CopyInformation(brain_mask_raw)
    sitk.WriteImage(brain_mask_filled, '/RESULT/PATH/brain_mask_filled.nii.gz')
    

    参考:scipy doc for binary fill holes

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2018-11-26
      • 2013-05-20
      相关资源
      最近更新 更多