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)
版权声明:本文标题:python中generate函数_python scipy ndimage.generate_binary_structure用法及代码示例 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1725840454a1044960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论