admin管理员组

文章数量:1530842

为二进制形态学运算生成二进制结构。

参数:

rank:intnp.ndim返回的结构元素将应用到的数组的维数。

connectivity:int连通性确定输出阵列的哪些元素属于该结构,即被视为中心元素的邻居。与中心的连接距离不超过平方距离的元素被视为相邻元素。连接性的范围可以是1(没有对角线元素是邻居)到排名(所有元素是邻居)。

返回值:

output:布尔数组可用于二进制形态学运算的结构元素,其秩维和所有维等于3。

注意:

generate_binary_structure只能创建尺寸等于3(即最小尺寸)的结构化元素。对于较大的结构元素,例如对于侵蚀大物体,可以使用iterate_structure,或使用numpy函数直接创建自定义数组,例如numpy.ones。

例子:

>>> from scipy import ndimage

>>> struct = ndimage.generate_binary_structure(2, 1)

>>> struct

array([[False, True, False],

[ True, True, True],

[False, True, False]], dtype=bool)

>>> a = np.zeros((5,5))

>>> a[2, 2] = 1

>>> a

array([[ 0., 0., 0., 0., 0.],

[ 0., 0., 0., 0., 0.],

[ 0., 0., 1., 0., 0.],

[ 0., 0., 0., 0., 0.],

[ 0., 0., 0., 0., 0.]])

>>> b = ndimage.binary_dilation(a, structure=struct).astype(a.dtype)

>>> b

array([[ 0., 0., 0., 0., 0.],

[ 0., 0., 1., 0., 0.],

[ 0., 1., 1., 1., 0.],

[ 0., 0., 1., 0., 0.],

[ 0., 0., 0., 0., 0.]])

>>> ndimage.binary_dilation(b, structure=struct).astype(a.dtype)

array([[ 0., 0., 1., 0., 0.],

[ 0., 1., 1., 1., 0.],

[ 1., 1., 1., 1., 1.],

[ 0., 1., 1., 1., 0.],

[ 0., 0., 1., 0., 0.]])

>>> struct = ndimage.generate_binary_structure(2, 2)

>>> struct

array([[ True, True, True],

[ True, True, True],

[ True, True, True]], dtype=bool)

>>> struct = ndimage.generate_binary_structure(3, 1)

>>> struct # no diagonal elements

array([[[False, False, False],

[False, True, False],

[False, False, False]],

[[False, True, False],

[ True, True, True],

[False, True, False]],

[[False, False, False],

[False, True, False],

[False, False, False]]], dtype=bool)

本文标签: 示例函数代码PythonGenerate