-1

如果可能的话,我试图弄清楚如何在没有“if”条件的情况下使“z”轴对 2D 和 3D 输入可选。因此,如果输入是 2D,它将忽略 z 轴。如果输入是 3D,则需要 z 值。任何帮助或建议将不胜感激。

def grey_closing(matrix, x=2, y=2, z=1):
    matrix = ndimage.grey_opening(matrix, structure=np.ones((x,y,z)))
    return matrix

test_result_aug_2 = grey_closing(test_result_aug_2)

这是我得到的错误:

RuntimeError                              Traceback (most recent call last)
<ipython-input-249-3772fbf0cb7c> in <module>()
----> 1 test_result_aug_2 = grey_closing(test_result_aug_2)
      2 test_result2 = grey_closing(test_result2)
      3 plot_comparison(test_result2, test_result_aug_2)

<ipython-input-241-2b0072643ca3> in grey_closing(matrix, x, y, z)
     35     return matrix
     36 def grey_closing(matrix, x=2, y=2, z=1):
---> 37     matrix = ndimage.grey_closing(matrix, structure=np.ones((x,y,z)))
     38     return matrix

c:\python36\lib\site-packages\scipy\ndimage\morphology.py in grey_closing(input, size, footprint, structure, output, mode, cval, origin)
   1520         warnings.warn("ignoring size because footprint is set", UserWarning, stacklevel=2)
   1521     tmp = grey_dilation(input, size, footprint, structure, None, mode,
-> 1522                         cval, origin)
   1523     return grey_erosion(tmp, size, footprint, structure, output, mode,
   1524                         cval, origin)

c:\python36\lib\site-packages\scipy\ndimage\morphology.py in grey_dilation(input, size, footprint, structure, output, mode, cval, origin)
   1356 
   1357     return filters._min_or_max_filter(input, size, footprint, structure,
-> 1358                                       output, mode, cval, origin, 0)
   1359 
   1360 

c:\python36\lib\site-packages\scipy\ndimage\filters.py in _min_or_max_filter(input, size, footprint, structure, output, mode, cval, origin, minimum)
   1001         fshape = [ii for ii in footprint.shape if ii > 0]
   1002         if len(fshape) != input.ndim:
-> 1003             raise RuntimeError('footprint array has incorrect shape.')
   1004         for origin, lenf in zip(origins, fshape):
   1005             if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):

RuntimeError: footprint array has incorrect shape.
4

1 回答 1

2

也许:

structure_shape = (x, y, z)[:matrix.ndim]
structure = np.ones(structure_shape)

如果是 2Dz则忽略somatrix

于 2018-08-28T08:37:40.863 回答