SciPy 1.12 中文文档(二十一)
scipy.ndimage.standard_deviation
scipy.ndimage.standard_deviation(input, labels=None, index=None)
计算 N 维图像数组值的标准差,可选地在指定的子区域进行计算。
参数:
input类似数组
要处理的 N 维图像数据。
标签类似数组,可选
用于识别输入中子区域的标签。如果不为 None,则必须与输入具有相同的形状。
index整数或整数序列,可选
要包含在输出中的标签。如果为 None(默认),则使用所有标签非零的值。
返回:
standard_deviation浮点数或 ndarray
如果指定了标签和索引,则每个子区域的标准差值。
另请参见
label, variance, maximum, minimum, extrema
示例
>>> import numpy as np
>>> a = np.array([[1, 2, 0, 0],
... [5, 3, 0, 4],
... [0, 0, 0, 7],
... [9, 3, 0, 0]])
>>> from scipy import ndimage
>>> ndimage.standard_deviation(a)
2.7585095613392387
可以使用标签和索引指定要处理的特征:
>>> lbl, nlbl = ndimage.label(a)
>>> ndimage.standard_deviation(a, lbl, index=np.arange(1, nlbl+1))
array([ 1.479, 1.5 , 3\. ])
如果没有给出索引,则处理非零标签:
>>> ndimage.standard_deviation(a, lbl)
2.4874685927665499
scipy.ndimage.sum_labels
scipy.ndimage.sum_labels(input, labels=None, index=None)
计算数组值的总和。
参数:
input:array_like
input 中由labels 定义的区域内的值被合并。
labels:array_like,整数,可选
为数组的值分配标签。必须与input具有相同的形状。
index:array_like,可选
单个标签号或要测量的对象的标签号序列。
返回:
sum:ndarray 或标量
index 形状与labels 定义的区域内input 值的和的数组相同。如果‘index’ 为 None 或标量,则返回标量。
另请参阅:
mean,median
示例
>>> from scipy import ndimage
>>> input = [0,1,2,3]
>>> labels = [1,1,2,2]
>>> ndimage.sum_labels(input, labels, index=[1,2])
[1.0, 5.0]
>>> ndimage.sum_labels(input, labels, index=1)
1
>>> ndimage.sum_labels(input, labels)
6
scipy.ndimage.value_indices
scipy.ndimage.value_indices(arr, *, ignore_value=None)
查找给定数组中每个不同值的索引。
参数:
arr整数的 ndarray
包含整数值的数组。
ignore_valueint,可选
在搜索 arr 数组时,将忽略该值。如果未提供,则输出中将包括所有找到的值。默认为 None。
返回:
indices字典
一个 Python 字典,用于每个不同值的数组索引。字典以不同的值作为键,条目是覆盖数组中所有出现值的数组索引元组。
此字典可能占用大量内存,通常是输入数组大小的几倍。
另请参见
label,maximum,median,minimum_position,extrema,sum,mean,variance
standard_deviation,numpy.where,numpy.unique
注释
对于具有少量不同值的小数组,可以使用 numpy.unique() 找到所有可能的值,并使用 (arr == val) 定位数组中的每个值。然而,对于具有许多不同值的大数组,这可能变得非常低效,因为每次定位值都需要对整个数组进行新的搜索。使用此函数,实际上只进行了一次搜索,并保存了所有不同值的索引。
当将分类图像(例如分割或分类)与其他数据的关联图像进行匹配时,这非常有用,允许然后计算任何每类统计量。提供了对 scipy.ndimage.mean() 和 scipy.ndimage.variance() 等函数的更灵活的替代方案。
其他相关功能可在 scipy.stats.binned_statistic() 和 scikit-image 函数 skimage.measure.regionprops() 中找到,它们各有优势和劣势。
IDL 用户注意:这提供了与 IDL 的 REVERSE_INDICES 选项相当的功能(根据HISTOGRAM函数的 IDL 文档)。
新版 1.10.0 中新增功能。
示例
>>> import numpy as np
>>> from scipy import ndimage
>>> a = np.zeros((6, 6), dtype=int)
>>> a[2:4, 2:4] = 1
>>> a[4, 4] = 1
>>> a[:2, :3] = 2
>>> a[0, 5] = 3
>>> a
array([[2, 2, 2, 0, 0, 3],
[2, 2, 2, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0]])
>>> val_indices = ndimage.value_indices(a)
字典val_indices将为输入数组中的每个不同值都有一个条目。
>>> val_indices.keys()
dict_keys([0, 1, 2, 3])
每个值的条目是一个索引元组,用于定位具有该值的元素。
>>> ndx1 = val_indices[1]
>>> ndx1
(array([2, 2, 3, 3, 4]), array([2, 3, 2, 3, 4]))
这可用于对原始数组或任何具有相同形状的数组进行索引。
>>> a[ndx1]
array([1, 1, 1, 1, 1])
如果忽略了零,则结果字典将不再包含零的条目。
>>> val_indices = ndimage.value_indices(a, ignore_value=0)
>>> val_indices.keys()
dict_keys([1, 2, 3])
scipy.ndimage.variance
scipy.ndimage.variance(input, labels=None, index=None)
可选地计算 N 维图像数组值的方差,可选地在指定的子区域。
参数:
input:array_like
待处理的 Nd 图像数据。
labels:array_like,可选
定义input中子区域的标签。如果不是 None,则必须与input具有相同的形状。
index:int 或 int 序列,可选
labels要包含在输出中。如果为 None(默认),则使用所有labels非零的值。
返回:
variance:float 或 ndarray
如果指定了labels和index,则每个子区域的方差值。
另请参见
label, standard_deviation, maximum, minimum, extrema
示例
>>> import numpy as np
>>> a = np.array([[1, 2, 0, 0],
... [5, 3, 0, 4],
... [0, 0, 0, 7],
... [9, 3, 0, 0]])
>>> from scipy import ndimage
>>> ndimage.variance(a)
7.609375
可使用labels和index指定要处理的特征:
>>> lbl, nlbl = ndimage.label(a)
>>> ndimage.variance(a, lbl, index=np.arange(1, nlbl+1))
array([ 2.1875, 2.25 , 9\. ])
如果没有给出索引,则处理所有非零的labels:
>>> ndimage.variance(a, lbl)
6.1875
scipy.ndimage.watershed_ift
scipy.ndimage.watershed_ift(input, markers, structure=None, output=None)
使用图像森林变换算法从标记中应用分水岭。
参数:
input:array_like
输入。
markers:array_like
标记是每个分水岭中形成过程开始的点。负标记被视为背景标记,这些标记在其他标记之后处理。
structure:结构元素,可选
可以提供一个定义对象连接性的结构元素。如果为 None,则生成一个具有方形连接性为一的元素。
output:ndarray,可选
可以选择性地提供输出数组。与输入相同的形状。
返回:
watershed_ift:ndarray
输出。与 input 相同的形状。
参考文献:
[1]
A.X. Falcao, J. Stolfi 和 R. de Alencar Lotufo,《图像森林变换:理论、算法和应用》,模式分析与机器智能,第 26 卷,第 19-29 页,2004 年。
scipy.ndimage.binary_closing
scipy.ndimage.binary_closing(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False)
多维二进制使用给定的结构元素进行闭合。
通过结构元素对输入图像进行闭合,是通过结构元素对图像进行膨胀再腐蚀的过程。
参数:
输入array_like
待闭合的二进制 array_like。非零(True)元素形成要闭合的子集。
structurearray_like,可选
用于闭合的结构元素。非零元素被视为 True。如果没有提供结构元素,则生成一个连接度为 1 的方形元素(即只有最近的邻居与中心相连,对角线相连的元素不视为邻居)。
迭代次数int,可选
膨胀步骤的闭合,然后是腐蚀步骤,每个操作重复迭代次数(默认为 1 次)。如果迭代次数小于 1,则每个操作重复直到结果不再改变。只接受整数迭代次数。
输出ndarray,可选
与输入相同形状的数组,其中输出被放置。默认情况下,会创建一个新数组。
原点int 或 int 元组,可选
滤波器的放置,默认为 0。
掩模array_like,可选
如果给定掩模,则只有对应掩模元素处为 True 的元素在每次迭代中才会被修改。
1.1.0 版本新增。
边界值int(转换为 0 或 1),可选
输出数组中边界的值。
1.1.0 版本新增。
蛮力布尔值,可选
存储条件:如果为 False,则仅跟踪上次迭代中值发生变化的像素作为当前迭代中更新的候选;如果为 True,则所有像素都被视为候选更新,不管上一次迭代发生了什么。默认为 False。
1.1.0 版本新增。
返回:
binary_closing布尔值的 ndarray
用给定的结构元素进行闭合。
参见
注意事项
Closing [1] 是一种数学形态学操作 [2],由输入与相同结构元素的膨胀和腐蚀相继进行组成。因此,闭运算填充比结构元素小的空洞。
与 opening (binary_opening) 结合使用可以用于去除噪音。
参考资料
[1]
[zh.wikipedia.org/wiki/闭运算 _(形态学)](zh.wikipedia.org/wiki/闭运算 _(形态学))
[2]
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5,5), dtype=int)
>>> a[1:-1, 1:-1] = 1; a[2,2] = 0
>>> a
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
>>> # Closing removes small holes
>>> ndimage.binary_closing(a).astype(int)
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
>>> # Closing is the erosion of the dilation of the input
>>> ndimage.binary_dilation(a).astype(int)
array([[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0]])
>>> ndimage.binary_erosion(ndimage.binary_dilation(a)).astype(int)
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
>>> a = np.zeros((7,7), dtype=int)
>>> a[1:6, 2:5] = 1; a[1:3,3] = 0
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> # In addition to removing holes, closing can also
>>> # coarsen boundaries with fine hollows.
>>> ndimage.binary_closing(a).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
scipy.ndimage.binary_dilation
scipy.ndimage.binary_dilation(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False)
给定结构元素的多维二进制膨胀。
参数:
inputarray_like
二进制数组 _like,需进行膨胀操作。非零(True)元素形成需膨胀的子集。
structurearray_like, optional
用于膨胀的结构元素。非零元素被视为 True。如果未提供结构元素,则生成一个连通性为 1 的正方形元素。
iterationsint, optional
膨胀重复 iterations 次(默认一次)。如果 iterations 小于 1,则膨胀将重复,直到结果不再改变。只接受整数 iterations。
maskarray_like, optional
如果提供了掩码,则仅在每次迭代时修改相应掩码元素处为 True 的元素。
outputndarray, optional
与输入相同形状的数组,用于存放输出。默认情况下,将创建一个新数组。
border_valueint(强制为 0 或 1),可选
输出数组的边界值。
originint 或整数元组,可选
滤波器的放置位置,默认为 0。
brute_forceboolean, optional
内存条件:如果为 False,则仅跟踪在上一次迭代中更改值的像素作为当前迭代中待更新(膨胀)的候选像素;如果为 True,则所有像素均视为候选膨胀像素,不考虑上一次迭代中的情况。默认为 False。
返回:
binary_dilationbools 的 ndarray
使用结构元素对输入进行膨胀。
另请参见
grey_dilation, binary_erosion, binary_closing, binary_opening
注意事项
膨胀 [1] 是一种使用结构元素扩展图像中形状的数学形态学操作 [2]。通过结构元素对图像的非零点进行膨胀,膨胀的图像点由结构元素的中心所在位置决定。
参考资料
en.wikipedia.org/wiki/Dilation_%28morphology%29
[2]
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> 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.]])
>>> ndimage.binary_dilation(a)
array([[False, False, False, False, False],
[False, False, True, False, False],
[False, True, True, True, False],
[False, False, True, False, False],
[False, False, False, False, False]], dtype=bool)
>>> ndimage.binary_dilation(a).astype(a.dtype)
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.]])
>>> # 3x3 structuring element with connectivity 1, used by default
>>> struct1 = ndimage.generate_binary_structure(2, 1)
>>> struct1
array([[False, True, False],
[ True, True, True],
[False, True, False]], dtype=bool)
>>> # 3x3 structuring element with connectivity 2
>>> struct2 = ndimage.generate_binary_structure(2, 2)
>>> struct2
array([[ True, True, True],
[ True, True, True],
[ True, True, True]], dtype=bool)
>>> ndimage.binary_dilation(a, structure=struct1).astype(a.dtype)
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(a, structure=struct2).astype(a.dtype)
array([[ 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0.]])
>>> ndimage.binary_dilation(a, structure=struct1,\
... iterations=2).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.]])
scipy.ndimage.binary_erosion
scipy.ndimage.binary_erosion(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False)
给定结构元素进行的多维二值侵蚀。
二值侵蚀是一种用于图像处理的数学形态学操作。
参数:
input array_like
待侵蚀的二值图像。非零(True)元素形成待侵蚀的子集。
structure array_like,可选
用于侵蚀的结构元素。非零元素被视为 True。若未提供结构元素,则生成一个具有正方形连接性的元素。
iterations int,可选
侵蚀操作重复 iterations 次数(默认为一次)。若 iterations 小于 1,则重复侵蚀直至结果不再改变。
mask array_like,可选
若给定掩模,则只有对应掩模元素值为 True 的元素在每次迭代中才会被修改。
output ndarray,可选
形状与输入相同的数组,用以放置输出。默认情况下,创建一个新数组。
border_value int(转换为 0 或 1),可选
输出数组中边界处的值。
origin int 或 int 元组,可选
滤波器的放置,默认为 0。
brute_force 布尔值,可选
内存条件:若为 False,则仅追踪上次迭代中值已更改的像素作为当前迭代中要更新(侵蚀)的候选;若为 True,则无论上次迭代中发生了什么,所有像素都被视为侵蚀的候选。默认为 False。
返回:
binary_erosion 布尔值的 ndarray
通过结构元素对输入进行的侵蚀。
参见
注释
侵蚀 [1] 是一种数学形态学操作 [2],使用结构元素来缩小图像中的形状。图像的结构元素侵蚀是结构元素中心位于该点的叠加完全包含在图像非零元素集合中的点的轨迹。
参考文献
[1]
en.wikipedia.org/wiki/Erosion_%28morphology%29
[2]
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((7,7), dtype=int)
>>> a[1:6, 2:5] = 1
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.binary_erosion(a).astype(a.dtype)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> #Erosion removes objects smaller than the structure
>>> ndimage.binary_erosion(a, structure=np.ones((5,5))).astype(a.dtype)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
scipy.ndimage.binary_fill_holes
scipy.ndimage.binary_fill_holes(input, structure=None, output=None, origin=0)
填充二进制对象的空洞。
参数:
input类数组
N-D 二进制数组,其中含有待填充的孔洞
structure类数组,可选
用于计算的结构元素;大尺寸元素可以加快计算速度,但可能会忽略背景与细胞间隔开的孔洞。默认元素(方形连通性等于 1)产生直观结果,即输入中的所有孔洞已被填充。
outputndarray,可选
与输入相同形状的数组,其中放置了输出。默认情况下,将创建一个新数组。
origin整数,整数元组,可选
结构元素的位置。
返回:
outndarray
经过填充孔洞的初始图像 input 的变换。
另请参阅
binary_dilation, binary_propagation, label
注意事项
此函数中使用的算法是从图像的外部边界入侵 input 的形状的补集,使用二进制膨胀。孔洞未连接到边界,因此未被入侵。结果是入侵区域的补集子集。
参考文献
[1]
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5, 5), dtype=int)
>>> a[1:4, 1:4] = 1
>>> a[2,2] = 0
>>> a
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
>>> ndimage.binary_fill_holes(a).astype(int)
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
>>> # Too big structuring element
>>> ndimage.binary_fill_holes(a, structure=np.ones((5,5))).astype(int)
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
scipy.ndimage.binary_hit_or_miss
scipy.ndimage.binary_hit_or_miss(input, structure1=None, structure2=None, output=None, origin1=0, origin2=None)
多维二进制命中或错过变换。
命中或错过变换找到输入图像中给定模式的位置。
参数:
inputarray_like(转换为布尔值)
二进制图像,其中要检测到模式。
structure1array_like(转换为布尔值),可选
适合于input的前景(非零元素)的结构元素的一部分。如果未提供值,则选择 1 的方形连接结构。
structure2array_like(转换为布尔值),可选
必须完全错过前景的第二部分结构元素。如果未提供值,则取structure1的补集。
outputndarray,可选
形状与输入相同的数组,其中放置输出。默认情况下,会创建一个新数组。
origin1int 或整数元组,可选
结构元素structure1的第一部分的放置位置,默认为 0 表示中心结构。
origin2int 或整数元组,可选
结构元素structure2的第二部分的放置位置,默认为 0 表示中心结构。如果为origin1提供了值但未提供origin2的值,则origin2设为origin1。
返回:
binary_hit_or_missndarray
使用给定的结构元素(structure1、structure2)对input执行命中或错过变换。
参见
binary_erosion
参考文献
[1]
en.wikipedia.org/wiki/Hit-or-miss_transform
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((7,7), dtype=int)
>>> a[1, 1] = 1; a[2:4, 2:4] = 1; a[4:6, 4:6] = 1
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> structure1 = np.array([[1, 0, 0], [0, 1, 1], [0, 1, 1]])
>>> structure1
array([[1, 0, 0],
[0, 1, 1],
[0, 1, 1]])
>>> # Find the matches of structure1 in the array a
>>> ndimage.binary_hit_or_miss(a, structure1=structure1).astype(int)
array([[0, 0, 0, 0, 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, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> # Change the origin of the filter
>>> # origin1=1 is equivalent to origin1=(1,1) here
>>> ndimage.binary_hit_or_miss(a, structure1=structure1,\
... origin1=1).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 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, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0]])
scipy.ndimage.binary_opening
scipy.ndimage.binary_opening(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False)
给定结构元素的多维二进制开运算。
输入图像通过结构元素的开运算是图像通过结构元素的侵蚀后膨胀的过程。
参数:
输入类似数组
待开运算的二进制数组。非零(True)元素形成要开运算的子集。
结构类似数组,可选
用于开运算的结构元素。非零元素视为 True。如果未提供结构元素,则生成一个连接性等于一的方形元素(即,只有最近的邻居与中心连接,对角线连接的元素不被视为邻居)。
迭代次数整数,可选
开运算的侵蚀步骤,然后将膨胀步骤重复迭代次数次(默认为一次)。如果迭代次数小于 1,则每个操作重复,直到结果不再改变。只接受整数的迭代次数。
输出类似数组,可选
输出与输入相同形状的数组,其中输出被放置。默认情况下,创建一个新数组。
起始点整数或整数元组,可选
过滤器的放置,默认为 0。
掩码类似数组,可选
如果给定掩码,则仅修改每次迭代中对应掩码元素为 True 的元素。
新版本 1.1.0 中新增。
边界值整数(转换为 0 或 1),可选
输出数组的边界值。
新版本 1.1.0 中新增。
蛮力布尔值,可选
内存条件:如果为 False,则仅跟踪上次迭代中值发生更改的像素作为当前迭代中要更新的候选像素;如果为 True,则考虑所有像素作为候选像素,无论上次迭代中发生了什么。默认为 False。
新版本 1.1.0 中新增。
返回:
binary_opening布尔数组
通过结构元素开运算输入。
另见
笔记
Opening [1] 是数学形态学操作 [2],包括对输入使用相同结构元素的侵蚀和膨胀的连续操作。因此,opening 可以去除小于结构元素的对象。
与 closing (binary_closing) 一起,opening 可用于去噪。
参考文献
[1]
en.wikipedia.org/wiki/Opening_%28morphology%29
[2]
en.wikipedia.org/wiki/Mathematical_morphology
例子
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5,5), dtype=int)
>>> a[1:4, 1:4] = 1; a[4, 4] = 1
>>> a
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 1]])
>>> # Opening removes small objects
>>> ndimage.binary_opening(a, structure=np.ones((3,3))).astype(int)
array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]])
>>> # Opening can also smooth corners
>>> ndimage.binary_opening(a).astype(int)
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]])
>>> # Opening is the dilation of the erosion of the input
>>> ndimage.binary_erosion(a).astype(int)
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]])
>>> ndimage.binary_dilation(ndimage.binary_erosion(a)).astype(int)
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]])
scipy.ndimage.binary_propagation
scipy.ndimage.binary_propagation(input, structure=None, mask=None, output=None, border_value=0, origin=0)
使用给定结构元素的多维二进制传播。
参数:
输入array_like
用于在掩模内部传播的二进制图像。
结构array_like,可选
用于连续膨胀的结构元素。输出可能取决于结构元素,特别是如果掩模有多个连通分量。如果未提供结构元素,则生成一个方形连通性为 1 的元素。
掩模array_like,可选
定义了输入允许传播到的区域的二进制掩模。
输出ndarray,可选
与输入形状相同的数组,其中放置了输出。默认情况下,会创建一个新数组。
边界值int(转换为 0 或 1),可选
输出数组中边界的值。
起点int 或 int 元组,可选
过滤器的放置,默认为 0。
返回:
二进制传播ndarray
在掩模内部传播输入的二进制传播。
注释
此函数在功能上相当于调用二值膨胀,迭代次数小于 1:迭代膨胀,直到结果不再改变。
可以使用原始图像中的侵蚀和传播的连续序列来代替开运算,以删除小对象同时保持较大对象的轮廓不变。
参考文献
[1]
cmm.ensmp.fr/~serra/cours/pdf/en/ch6en.pdf,第 15 页.
[2]
I.T. Young, J.J. Gerbrands, 和 L.J. van Vliet,《图像处理基础》,1998 ftp://qiftp.tudelft.nl/DIPimage/docs/FIP2.3.pdf
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> input = np.zeros((8, 8), dtype=int)
>>> input[2, 2] = 1
>>> mask = np.zeros((8, 8), dtype=int)
>>> mask[1:4, 1:4] = mask[4, 4] = mask[6:8, 6:8] = 1
>>> input
array([[0, 0, 0, 0, 0, 0, 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, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
>>> mask
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1]])
>>> ndimage.binary_propagation(input, mask=mask).astype(int)
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.binary_propagation(input, mask=mask,\
... structure=np.ones((3,3))).astype(int)
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
>>> # Comparison between opening and erosion+propagation
>>> a = np.zeros((6,6), dtype=int)
>>> a[2:5, 2:5] = 1; a[0, 0] = 1; a[5, 5] = 1
>>> a
array([[1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 1]])
>>> ndimage.binary_opening(a).astype(int)
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0]])
>>> b = ndimage.binary_erosion(a)
>>> b.astype(int)
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 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, 0, 0]])
>>> ndimage.binary_propagation(b, mask=a).astype(int)
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]])
scipy.ndimage.black_tophat
scipy.ndimage.black_tophat(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
多维黑帽滤波器。
参数:
input类似数组
输入。
size整数元组,可选
用于过滤器的平坦且完整的结构元素的形状。如果提供了footprint或structure,则可选。
footprint整数数组,可选
用于黑帽滤波器的平坦结构元素的非无限元素的位置。
structure整数数组,可选
用于滤波器的结构元素。structure可以是非平坦结构元素。
output数组,可选
可以提供用于存储滤波器输出的数组。
mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’},可选
mode参数确定如何处理数组边界,当mode等于‘constant’时,cval为其值。默认为‘reflect’。
cval标量,可选
如果mode为‘constant’,则填充输入超出边缘的值。默认为 0.0。
origin标量,可选
origin参数控制滤波器的放置。默认为 0。
返回:
black_tophat ndarray
input与structure的滤波器结果。
另请参阅
white_tophat, grey_opening, grey_closing
示例
将暗峰变为亮峰并减去背景。
>>> from scipy.ndimage import generate_binary_structure, black_tophat
>>> import numpy as np
>>> square = generate_binary_structure(rank=2, connectivity=3)
>>> dark_on_gray = np.array([[7, 6, 6, 6, 7],
... [6, 5, 4, 5, 6],
... [6, 4, 0, 4, 6],
... [6, 5, 4, 5, 6],
... [7, 6, 6, 6, 7]])
>>> black_tophat(input=dark_on_gray, structure=square)
array([[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 5, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]])
scipy.ndimage.distance_transform_bf
scipy.ndimage.distance_transform_bf(input, metric='euclidean', sampling=None, return_distances=True, return_indices=False, distances=None, indices=None)
通过蛮力算法的距离变换函数。
此函数通过用前景(非零)元素的最短距离替换到背景(任何零值元素)来计算input的距离变换。
除了距离变换之外,还可以计算特征变换。在这种情况下,将返回每个前景元素最接近的背景元素的索引。
参数:
inputarray_like
输入
metric{‘euclidean’,‘taxicab’,‘chessboard’},可选
‘cityblock’和‘manhattan’也是有效的,它们映射到‘taxicab’。默认为‘euclidean’。
samplingfloat 或 float 序列,可选
仅当metric为‘euclidean’时使用此参数。沿每个维度的元素间距。如果是序列,则必须与输入等级的长度相等;如果是单个数字,则用于所有轴。如果未指定,则暗示单位的网格间距。
return_distancesbool,可选
是否计算距离变换。默认为 True。
return_indicesbool,可选
是否计算特征变换。默认为 False。
distancesndarray,可选
用于存储计算的距离变换的输出数组,而不是返回它。return_distances必须为 True。如果metric为‘euclidean’,则其类型必须为 float64,否则为 uint32,形状必须与input相同。
indicesint32 ndarray,可选
用于存储计算的特征变换的输出数组,而不是返回它。return_indicies必须为 True。其形状必须为*(input.ndim,) + input.shape*。
返回:
distancesndarray,可选
计算得出的距离变换。仅当return_distances为 True 且未提供distances时返回。它将具有与输入数组相同的形状。
indicesint32 ndarray,可选
计算得出的特征变换。它对于输入的每个维度都有一个类似形状的数组。详见distance_transform_edt文档的示例。仅在return_indices为 True 且未提供indices时返回。
另请参见
distance_transform_cdt
更快的出租车距离和棋盘距离变换
distance_transform_edt
更快的欧几里德距离变换
注意事项
此函数采用了一种缓慢的暴力算法。另请参阅函数distance_transform_cdt以获取更高效的出租车[1]和棋盘算法[2]。
参考文献
[1]
出租车距离。维基百科,2023 年。zh.wikipedia.org/wiki/%E8%A1%8C%E8%BB%8A%E8%B7%9D%E9%9B%A2
[2]
棋盘距离。维基百科,2023 年。zh.wikipedia.org/wiki/%E6%A3%8B%E7%9B%98%E8%B7%9D%E7%A6%BB
示例
导入必要的模块。
>>> import numpy as np
>>> from scipy.ndimage import distance_transform_bf
>>> import matplotlib.pyplot as plt
>>> from mpl_toolkits.axes_grid1 import ImageGrid
首先,我们创建一个玩具二进制图像。
>>> def add_circle(center_x, center_y, radius, image, fillvalue=1):
... # fill circular area with 1
... xx, yy = np.mgrid[:image.shape[0], :image.shape[1]]
... circle = (xx - center_x) ** 2 + (yy - center_y) ** 2
... circle_shape = np.sqrt(circle) < radius
... image[circle_shape] = fillvalue
... return image
>>> image = np.zeros((100, 100), dtype=np.uint8)
>>> image[35:65, 20:80] = 1
>>> image = add_circle(28, 65, 10, image)
>>> image = add_circle(37, 30, 10, image)
>>> image = add_circle(70, 45, 20, image)
>>> image = add_circle(45, 80, 10, image)
接下来,我们设置图形。
>>> fig = plt.figure(figsize=(8, 8)) # set up the figure structure
>>> grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=(0.4, 0.3),
... label_mode="1", share_all=True,
... cbar_location="right", cbar_mode="each",
... cbar_size="7%", cbar_pad="2%")
>>> for ax in grid:
... ax.axis('off') # remove axes from images
左上图是原始的二进制图像。
>>> binary_image = grid[0].imshow(image, cmap='gray')
>>> cbar_binary_image = grid.cbar_axes[0].colorbar(binary_image)
>>> cbar_binary_image.set_ticks([0, 1])
>>> grid[0].set_title("Binary image: foreground in white")
距离变换根据距离度量计算前景像素与图像背景之间的距离。在distance_transform_bf中可用的度量包括:euclidean(默认)、taxicab和chessboard。右上图包含基于euclidean度量的距离变换。
>>> distance_transform_euclidean = distance_transform_bf(image)
>>> euclidean_transform = grid[1].imshow(distance_transform_euclidean,
... cmap='gray')
>>> cbar_euclidean = grid.cbar_axes[1].colorbar(euclidean_transform)
>>> colorbar_ticks = [0, 10, 20]
>>> cbar_euclidean.set_ticks(colorbar_ticks)
>>> grid[1].set_title("Euclidean distance")
左下图包含使用taxicab度量的距离变换。
>>> distance_transform_taxicab = distance_transform_bf(image,
... metric='taxicab')
>>> taxicab_transformation = grid[2].imshow(distance_transform_taxicab,
... cmap='gray')
>>> cbar_taxicab = grid.cbar_axes[2].colorbar(taxicab_transformation)
>>> cbar_taxicab.set_ticks(colorbar_ticks)
>>> grid[2].set_title("Taxicab distance")
最后,右下图包含使用chessboard度量的距离变换。
>>> distance_transform_cb = distance_transform_bf(image,
... metric='chessboard')
>>> chessboard_transformation = grid[3].imshow(distance_transform_cb,
... cmap='gray')
>>> cbar_taxicab = grid.cbar_axes[3].colorbar(chessboard_transformation)
>>> cbar_taxicab.set_ticks(colorbar_ticks)
>>> grid[3].set_title("Chessboard distance")
>>> plt.show()
scipy.ndimage.distance_transform_cdt
scipy.ndimage.distance_transform_cdt(input, metric='chessboard', return_distances=True, return_indices=False, distances=None, indices=None)
按 chamfer 类型的转换的距离变换。
此函数通过将每个前景(非零)元素替换为其到背景(任何零值元素)的最短距离,计算input的距离变换。
除了距离变换之外,还可以计算特征变换。在这种情况下,将返回一个单独的数组,其中包含每个前景元素到最近背景元素的索引。
参数:
inputarray_like
输入。值为 0 被视为背景。
metric{‘chessboard’, ‘taxicab’} 或 array_like,可选
metric确定所执行的 chamfer 类型。如果metric等于'taxicab',则生成一个结构,使用generate_binary_structure ,其平方距离等于 1。如果metric等于'chessboard',则生成一个metric,使用generate_binary_structure ,其平方距离等于数组的维数。这些选择对应于在二维空间中‘taxicab’和‘chessboard’距离度量的常见解释。可以提供自定义的度量,形式为一个矩阵,其中每个维度长度为三。‘cityblock’和‘manhattan’也是有效的,并映射到‘taxicab’。默认值是‘chessboard’。
return_distancesbool,可选
是否计算距离变换。默认为 True。
return_indicesbool,可选
是否计算特征变换。默认为 False。
distancesint32 ndarray,可选
一个输出数组,用于存储计算得到的距离变换,而不是返回它。return_distances 必须为 True。它必须与input具有相同的形状。
indicesint32 ndarray,可选
一个输出数组,用于存储计算得到的特征变换,而不是返回它。return_indicies 必须为 True。其形状必须为*(input.ndim,) + input.shape*。
返回:
distancesint32 ndarray,可选
计算得到的距离变换。仅在return_distances为 True 且未提供distances时返回。它的形状与输入数组相同。
indicesint32 ndarray,可选
计算得到的特征变换。对于输入的每个维度,它有一个形状相同的数组。详细示例请参见 distance_transform_edt 文档。仅在return_indices为 True 且未提供indices时返回。
参见
distance_transform_edt
欧几里得距离的快速距离变换
distance_transform_bf
使用较慢的蛮力算法进行不同度量的距离变换
示例
导入必要的模块。
>>> import numpy as np
>>> from scipy.ndimage import distance_transform_cdt
>>> import matplotlib.pyplot as plt
>>> from mpl_toolkits.axes_grid1 import ImageGrid
首先,我们创建一个玩具二进制图像。
>>> def add_circle(center_x, center_y, radius, image, fillvalue=1):
... # fill circular area with 1
... xx, yy = np.mgrid[:image.shape[0], :image.shape[1]]
... circle = (xx - center_x) ** 2 + (yy - center_y) ** 2
... circle_shape = np.sqrt(circle) < radius
... image[circle_shape] = fillvalue
... return image
>>> image = np.zeros((100, 100), dtype=np.uint8)
>>> image[35:65, 20:80] = 1
>>> image = add_circle(28, 65, 10, image)
>>> image = add_circle(37, 30, 10, image)
>>> image = add_circle(70, 45, 20, image)
>>> image = add_circle(45, 80, 10, image)
接下来,我们设置图表。
>>> fig = plt.figure(figsize=(5, 15))
>>> grid = ImageGrid(fig, 111, nrows_ncols=(3, 1), axes_pad=(0.5, 0.3),
... label_mode="1", share_all=True,
... cbar_location="right", cbar_mode="each",
... cbar_size="7%", cbar_pad="2%")
>>> for ax in grid:
... ax.axis('off')
>>> top, middle, bottom = grid
>>> colorbar_ticks = [0, 10, 20]
顶部图像包含原始的二进制图像。
>>> binary_image = top.imshow(image, cmap='gray')
>>> cbar_binary_image = top.cax.colorbar(binary_image)
>>> cbar_binary_image.set_ticks([0, 1])
>>> top.set_title("Binary image: foreground in white")
中间图像包含使用曼哈顿距离度量的距离变换。
>>> distance_taxicab = distance_transform_cdt(image, metric="taxicab")
>>> taxicab_transform = middle.imshow(distance_taxicab, cmap='gray')
>>> cbar_taxicab = middle.cax.colorbar(taxicab_transform)
>>> cbar_taxicab.set_ticks(colorbar_ticks)
>>> middle.set_title("Taxicab metric")
底部图像包含使用棋盘距离度量的距离变换。
>>> distance_chessboard = distance_transform_cdt(image,
... metric="chessboard")
>>> chessboard_transform = bottom.imshow(distance_chessboard, cmap='gray')
>>> cbar_chessboard = bottom.cax.colorbar(chessboard_transform)
>>> cbar_chessboard.set_ticks(colorbar_ticks)
>>> bottom.set_title("Chessboard metric")
>>> plt.tight_layout()
>>> plt.show()
scipy.ndimage.distance_transform_edt
scipy.ndimage.distance_transform_edt(input, sampling=None, return_distances=True, return_indices=False, distances=None, indices=None)
精确的欧几里得距离变换。
此函数通过将每个前景(非零)元素替换为其到背景(任何零值元素)的最短距离,计算 input 的距离变换。
除了距离变换外,还可以计算特征变换。在这种情况下,返回每个前景元素到最接近的背景元素的索引的单独数组。
参数:
input类数组
转换的输入数据。可以是任何类型,但将转换为二进制:输入等于 True 的地方为 1,其他地方为 0。
samplingfloat 或浮点数序列,可选
每个维度上的元素间隔。如果是序列,必须与输入的秩相等;如果是单个数字,则用于所有轴。如果未指定,假定为单位网格间距。
return_distances布尔值,可选
是否计算距离变换。默认为 True。
return_indices布尔值,可选
是否计算特征变换。默认为 False。
distancesfloat64 数组,可选
一个输出数组,用于存储计算得到的距离变换,而不是返回它。return_distances 必须为 True。其形状必须与 input 相同。
indicesint32 数组,可选
一个输出数组,用于存储计算得到的特征变换,而不是返回它。return_indicies 必须为 True。其形状必须为 (input.ndim,) + input.shape。
返回:
distancesfloat64 数组,可选
计算得到的距离变换。仅在 return_distances 为 True 且未提供 distances 时返回。其形状与输入数组相同。
indicesint32 数组,可选
计算得到的特征变换。它为输入的每个维度形状的数组。参见下面的示例。仅在 return_indices 为 True 且未提供 indices 时返回。
注:
欧几里得距离变换提供欧几里得距离的值:
n
y_i = sqrt(sum (x[i]-b[i])**2)
i
其中 b[i] 是背景点(值为 0),其与输入点 x[i] 的欧几里得距离最小,n 是维度的数量。
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.array(([0,1,1,1,1],
... [0,0,1,1,1],
... [0,1,1,1,1],
... [0,1,1,1,0],
... [0,1,1,0,0]))
>>> ndimage.distance_transform_edt(a)
array([[ 0\. , 1\. , 1.4142, 2.2361, 3\. ],
[ 0\. , 0\. , 1\. , 2\. , 2\. ],
[ 0\. , 1\. , 1.4142, 1.4142, 1\. ],
[ 0\. , 1\. , 1.4142, 1\. , 0\. ],
[ 0\. , 1\. , 1\. , 0\. , 0\. ]])
当沿 x 轴采样 2 单位,沿 y 轴采样 1 单位时:
>>> ndimage.distance_transform_edt(a, sampling=[2,1])
array([[ 0\. , 1\. , 2\. , 2.8284, 3.6056],
[ 0\. , 0\. , 1\. , 2\. , 3\. ],
[ 0\. , 1\. , 2\. , 2.2361, 2\. ],
[ 0\. , 1\. , 2\. , 1\. , 0\. ],
[ 0\. , 1\. , 1\. , 0\. , 0\. ]])
还要求返回索引:
>>> edt, inds = ndimage.distance_transform_edt(a, return_indices=True)
>>> inds
array([[[0, 0, 1, 1, 3],
[1, 1, 1, 1, 3],
[2, 2, 1, 3, 3],
[3, 3, 4, 4, 3],
[4, 4, 4, 4, 4]],
[[0, 0, 1, 1, 4],
[0, 1, 1, 1, 4],
[0, 0, 1, 4, 4],
[0, 0, 3, 3, 4],
[0, 0, 3, 3, 4]]])
提供用于原位输出的数组:
>>> indices = np.zeros(((np.ndim(a),) + a.shape), dtype=np.int32)
>>> ndimage.distance_transform_edt(a, return_indices=True, indices=indices)
array([[ 0\. , 1\. , 1.4142, 2.2361, 3\. ],
[ 0\. , 0\. , 1\. , 2\. , 2\. ],
[ 0\. , 1\. , 1.4142, 1.4142, 1\. ],
[ 0\. , 1\. , 1.4142, 1\. , 0\. ],
[ 0\. , 1\. , 1\. , 0\. , 0\. ]])
>>> indices
array([[[0, 0, 1, 1, 3],
[1, 1, 1, 1, 3],
[2, 2, 1, 3, 3],
[3, 3, 4, 4, 3],
[4, 4, 4, 4, 4]],
[[0, 0, 1, 1, 4],
[0, 1, 1, 1, 4],
[0, 0, 1, 4, 4],
[0, 0, 3, 3, 4],
[0, 0, 3, 3, 4]]])
scipy.ndimage.generate_binary_structure
scipy.ndimage.generate_binary_structure(rank, connectivity)
生成用于二进制形态学操作的二进制结构。
参数:
rankint
数组的维度,即由np.ndim返回的结构元素将应用到的数组的维度。
connectivityint
connectivity决定输出数组中哪些元素属于结构,即被视为中心元素的邻居。距离中心元素不超过connectivity的平方距离的元素被视为邻居。connectivity的范围可以从 1(没有对角线元素为邻居)到rank(所有元素都是邻居)。
返回:
outputbools 数组
用于二进制形态学操作的结构元素,具有rank维度和所有维度均为 3。
另请参阅
iterate_structure,binary_dilation,binary_erosion
注意事项
generate_binary_structure只能创建维度为 3 的结构元素,即最小维度。对于更大的结构元素(例如,用于侵蚀大物体),可以使用iterate_structure或直接使用 NumPy 函数(如numpy.ones)创建自定义数组。
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> 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)
scipy.ndimage.grey_closing
scipy.ndimage.grey_closing(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
多维灰度闭合。
灰度闭合包括灰度膨胀和灰度腐蚀的连续操作。
参数:
input类似数组
需要计算灰度闭合的数组。
size整数元组
用于灰度闭合的平面和完整结构元素的形状。如果提供了 footprint 或 structure,则可选。
footprint整数数组,可选
用于灰度闭合的平面结构元素的非无限元素位置。
structure整数数组,可选
用于灰度闭合的结构元素。structure 可以是非平面结构元素。
output数组,可选
可以提供一个数组用于存储闭合操作的输出。
mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’},可选
mode 参数确定如何处理数组边界,其中 cval 是当 mode 等于 'constant' 时的值。默认为 'reflect'。
cval标量,可选
如果 mode 为 'constant',则用来填充输入边界之外的值。默认为 0.0。
origin标量,可选
origin 参数控制滤波器的放置位置。默认为 0
返回:
grey_closing ndarray
input 和 structure 进行灰度闭合的结果。
另请参阅
binary_closing, grey_dilation, grey_erosion, grey_opening
注释
使用平面结构元素进行灰度闭合的操作相当于平滑深部局部最小值,而二值闭合则填补小孔。
参考
[1]
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.arange(36).reshape((6,6))
>>> a[3,3] = 0
>>> a
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 0, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
>>> ndimage.grey_closing(a, size=(3,3))
array([[ 7, 7, 8, 9, 10, 11],
[ 7, 7, 8, 9, 10, 11],
[13, 13, 14, 15, 16, 17],
[19, 19, 20, 20, 22, 23],
[25, 25, 26, 27, 28, 29],
[31, 31, 32, 33, 34, 35]])
>>> # Note that the local minimum a[3,3] has disappeared
scipy.ndimage.grey_dilation
scipy.ndimage.grey_dilation(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
计算灰度膨胀,使用结构元素或对应于平坦结构元素的足迹。
灰度膨胀是一种数学形态学操作。对于完整且平坦的结构元素的简单情况,可以将其视为在滑动窗口上的最大过滤器。
参数:
input类似数组
用于计算灰度膨胀的数组。
size整数元组
用于灰度膨胀的平坦和完整结构元素的形状。如果提供footprint或structure,则可选。
足迹整数数组,可选
用于灰度膨胀的平坦结构元素的非无限元素位置。非零值给出中心的邻居集,用于选择最大值。
structure整数数组,可选
用于灰度膨胀的结构元素。structure可以是非平坦结构元素。
output数组,可选
可以提供一个用于存储膨胀输出的数组。
模式{‘reflect’,’constant’,’nearest’,’mirror’, ‘wrap’},可选
mode参数确定如何处理数组边界,当 mode 等于‘constant’时,cval为其值。默认为‘reflect’。
cval标量,可选
如果mode为‘constant’,则超出输入边界的值填充。默认为 0.0。
origin标量,可选
origin参数控制过滤器的放置。默认为 0。
返回:
grey_dilationndarray
input的灰度膨胀。
参见
binary_dilation, grey_erosion, grey_closing, grey_opening
generate_binary_structure, maximum_filter
注意事项
对由定义在域 E 上的结构元素 s 输入的灰度膨胀的计算如下:
(input+s)(x) = max {input(y) + s(x-y),y 在 E 中}
特别地,对于定义为 s(y) = 0 的结构元素 E,灰度膨胀计算输入图像在由 E 定义的滑动窗口内的最大值。
参考文献
[1]
en.wikipedia.org/wiki/Dilation_%28morphology%29
[2]
en.wikipedia.org/wiki/Mathematical_morphology
示例
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((7,7), dtype=int)
>>> a[2:5, 2:5] = 1
>>> a[4,4] = 2; a[2,3] = 3
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 3, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.grey_dilation(a, size=(3,3))
array([[0, 0, 0, 0, 0, 0, 0],
[0, 1, 3, 3, 3, 1, 0],
[0, 1, 3, 3, 3, 1, 0],
[0, 1, 3, 3, 3, 2, 0],
[0, 1, 1, 2, 2, 2, 0],
[0, 1, 1, 2, 2, 2, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.grey_dilation(a, footprint=np.ones((3,3)))
array([[0, 0, 0, 0, 0, 0, 0],
[0, 1, 3, 3, 3, 1, 0],
[0, 1, 3, 3, 3, 1, 0],
[0, 1, 3, 3, 3, 2, 0],
[0, 1, 1, 2, 2, 2, 0],
[0, 1, 1, 2, 2, 2, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> s = ndimage.generate_binary_structure(2,1)
>>> s
array([[False, True, False],
[ True, True, True],
[False, True, False]], dtype=bool)
>>> ndimage.grey_dilation(a, footprint=s)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 3, 1, 0, 0],
[0, 1, 3, 3, 3, 1, 0],
[0, 1, 1, 3, 2, 1, 0],
[0, 1, 1, 2, 2, 2, 0],
[0, 0, 1, 1, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.grey_dilation(a, size=(3,3), structure=np.ones((3,3)))
array([[1, 1, 1, 1, 1, 1, 1],
[1, 2, 4, 4, 4, 2, 1],
[1, 2, 4, 4, 4, 2, 1],
[1, 2, 4, 4, 4, 3, 1],
[1, 2, 2, 3, 3, 3, 1],
[1, 2, 2, 3, 3, 3, 1],
[1, 1, 1, 1, 1, 1, 1]])
scipy.ndimage.grey_erosion
scipy.ndimage.grey_erosion(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0)
计算灰度侵蚀,可以使用结构元素,或者对应于平面结构元素的足迹。
灰度侵蚀是一种数学形态学操作。对于简单情况下的全平面结构元素,可以视为滑动窗口上的最小滤波器。
参数:
input 类似数组
计算灰度侵蚀的数组。
size 整数元组
用于灰度侵蚀的平坦全结构元素的形状。如果提供了 footprint 或 structure,则可选。
footprint 数组,整数,可选
用于计算灰度侵蚀的平坦结构元素的非无限元素位置。非零值给出中心的邻域集,其中选择最小值。
structure 数组,整数,可选
用于灰度侵蚀的结构元素。structure 可以是非平坦的结构元素。
output 数组,可选
可提供用于存储侵蚀输出的数组。
mode {‘reflect’,’constant’,’nearest’,’mirror’, ‘wrap’},可选
mode 参数决定如何处理数组边界,其中 cval 是当 mode 等于 'constant' 时的值。默认为 'reflect'。
cval 标量,可选
如果 mode 是 'constant',则填充输入边界之外的值。默认为 0.0。
origin 标量,可选
origin 参数控制滤波器的放置。默认为 0。
返回:
output ndarray
输入图像的灰度侵蚀。
另见
binary_erosion, grey_dilation, grey_opening, grey_closing
generate_binary_structure, minimum_filter
注意
由结构元素 s 定义的输入图像的灰度侵蚀在域 E 上给出:
(input+s)(x) = min {input(y) - s(x-y),y∈E}
特别地,对于定义为 s(y) = 0 的结构元素,灰度侵蚀计算在 E 定义的滑动窗口内输入图像的最小值。
参考文献
[1]
en.wikipedia.org/wiki/Erosion_%28morphology%29
[2]
en.wikipedia.org/wiki/Mathematical_morphology
Examples
>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((7,7), dtype=int)
>>> a[1:6, 1:6] = 3
>>> a[4,4] = 2; a[2,3] = 1
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
[0, 3, 3, 3, 3, 3, 0],
[0, 3, 3, 1, 3, 3, 0],
[0, 3, 3, 3, 3, 3, 0],
[0, 3, 3, 3, 2, 3, 0],
[0, 3, 3, 3, 3, 3, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.grey_erosion(a, size=(3,3))
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 3, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> footprint = ndimage.generate_binary_structure(2, 1)
>>> footprint
array([[False, True, False],
[ True, True, True],
[False, True, False]], dtype=bool)
>>> # Diagonally-connected elements are not considered neighbors
>>> ndimage.grey_erosion(a, footprint=footprint)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 3, 1, 2, 0, 0],
[0, 0, 3, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])