geometric_transform#
- scipy.ndimage.geometric_transform(input, mapping, output_shape=None, output=None, order=3, mode='constant', cval=0.0, prefilter=True, extra_arguments=(), extra_keywords=None)[源代码]#
应用任意几何变换。
给定映射函数用于为输出中的每个点查找输入中的相应坐标。输入在这些坐标处的值由所需阶数的样条插值确定。
- 参数:
- input类数组
输入数组。
- mapping{可调用对象, scipy.LowLevelCallable}
一个可调用对象,接受一个长度等于输出数组秩的元组,并返回相应的输入坐标作为长度等于输入数组秩的元组。
- output_shape整数元组,可选
形状元组。
- output数组或数据类型,可选
用于放置输出的数组,或返回数组的数据类型。默认情况下,将创建一个与输入具有相同数据类型的数组。
- order整数,可选
样条插值的阶数,默认为3。阶数必须在0-5范围内。
- mode{‘reflect’, ‘grid-mirror’, ‘constant’, ‘grid-constant’, ‘nearest’, ‘mirror’, ‘grid-wrap’, ‘wrap’},可选
参数 mode 决定输入数组如何在其边界之外扩展。默认为 'constant'。每个有效值的行为如下(参见 边界模式 的额外图表和详细信息)
- ‘reflect’ (d c b a | a b c d | d c b a)
通过最后一个像素的边缘反射来扩展输入。此模式有时也称为半样本对称。
- ‘grid-mirror’
这是 'reflect' 的同义词。
- ‘constant’ (k k k k | a b c d | k k k k)
通过用由 cval 参数定义的相同常量值填充边界之外的所有值来扩展输入。在输入边界之外不执行插值。
- ‘grid-constant’ (k k k k | a b c d | k k k k)
通过用由 cval 参数定义的相同常量值填充边界之外的所有值来扩展输入。对于输入范围之外的样本也会进行插值。
- ‘nearest’ (a a a a | a b c d | d d d d)
通过复制最后一个像素来扩展输入。
- ‘mirror’ (d c b | a b c d | c b a)
通过最后一个像素的中心反射来扩展输入。此模式有时也称为全样本对称。
- ‘grid-wrap’ (a b c d | a b c d | a b c d)
通过环绕到对边来扩展输入。
- ‘wrap’ (d b c d | a b c d | b c a b)
通过环绕到对边来扩展输入,但方式是使最后一个点和初始点精确重叠。在这种情况下,重叠点将选择哪个样本未明确定义。
- cval标量,可选
如果 mode 为 'constant',用于填充输入超出边界的值。默认为0.0。
- prefilter布尔值,可选
确定在插值之前是否使用
spline_filter
对输入数组进行预过滤。默认为 True,如果order > 1
,这将创建一个临时的 float64 过滤值数组。如果将其设置为 False,当order > 1
时,输出会略微模糊,除非输入已经过预过滤,即它是对原始输入调用spline_filter
的结果。- extra_arguments元组,可选
传递给 mapping 的额外参数。
- extra_keywords字典,可选
传递给 mapping 的额外关键字。
- 返回:
- outputndarray
过滤后的输入。
注意事项
此函数还接受具有以下签名之一并封装在
scipy.LowLevelCallable
中的低级回调函数int mapping(npy_intp *output_coordinates, double *input_coordinates, int output_rank, int input_rank, void *user_data) int mapping(intptr_t *output_coordinates, double *input_coordinates, int output_rank, int input_rank, void *user_data)
调用函数遍历输出数组的元素,并在每个元素处调用回调函数。当前输出元素的坐标通过
output_coordinates
传递。回调函数必须在input_coordinates
中返回输入需要插值的坐标。输入和输出数组的秩分别由input_rank
和output_rank
给出。user_data
是提供给scipy.LowLevelCallable
的数据指针。回调函数必须返回一个整数错误状态,如果出现问题则为零,否则为一。如果发生错误,您通常应在返回之前设置带有信息性消息的 Python 错误状态,否则调用函数将设置默认错误消息。
此外,还接受一些其他低级函数指针规范,但这些仅用于向后兼容,不应在新代码中使用。
对于复数值 input,此函数独立地转换实部和虚部。
1.6.0 版新增: 增加了复数值支持。
示例
>>> import numpy as np >>> from scipy.ndimage import geometric_transform >>> a = np.arange(12.).reshape((4, 3)) >>> def shift_func(output_coords): ... return (output_coords[0] - 0.5, output_coords[1] - 0.5) ... >>> geometric_transform(a, shift_func) array([[ 0. , 0. , 0. ], [ 0. , 1.362, 2.738], [ 0. , 4.812, 6.187], [ 0. , 8.263, 9.637]])
>>> b = [1, 2, 3, 4, 5] >>> def shift_func(output_coords): ... return (output_coords[0] - 3,) ... >>> geometric_transform(b, shift_func, mode='constant') array([0, 0, 0, 1, 2]) >>> geometric_transform(b, shift_func, mode='nearest') array([1, 1, 1, 1, 2]) >>> geometric_transform(b, shift_func, mode='reflect') array([3, 2, 1, 1, 2]) >>> geometric_transform(b, shift_func, mode='wrap') array([2, 3, 4, 1, 2])