bundles / scipy 1.17.1 / scipy / ndimage / _interpolation / geometric_transform
function
scipy.ndimage._interpolation:geometric_transform
Signature
def geometric_transform ( input , mapping , output_shape = None , output = None , order = 3 , mode = constant , cval = 0.0 , prefilter = True , extra_arguments = () , extra_keywords = None ) Summary
Apply an arbitrary geometric transform.
Extended Summary
The given mapping function is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order.
Parameters
input: array_likeThe input array.
mapping: {callable, scipy.LowLevelCallable}A callable object that accepts a tuple of length equal to the output array rank, and returns the corresponding input coordinates as a tuple of length equal to the input array rank.
output_shape: tuple of ints, optionalShape tuple.
output: array or dtype, optionalThe array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created.
order: int, optionalThe order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode: {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}, optionalThe
modeparameter determines how the input array is extended beyond its boundaries. Default is 'constant'. Behavior for each valid value is as follows (see additional plots and details onboundary modes <ndimage-interpolation-modes>):'reflect' (
d c b a | a b c d | d c b a)The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.
'grid-mirror'
This is a synonym for 'reflect'.
'constant' (
k k k k | a b c d | k k k k)The input is extended by filling all values beyond the edge with the same constant value, defined by the
cvalparameter. No interpolation is performed beyond the edges of the input.'grid-constant' (
k k k k | a b c d | k k k k)The input is extended by filling all values beyond the edge with the same constant value, defined by the
cvalparameter. Interpolation occurs for samples outside the input's extent as well.'nearest' (
a a a a | a b c d | d d d d)The input is extended by replicating the last pixel.
'mirror' (
d c b | a b c d | c b a)The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.
'grid-wrap' (
a b c d | a b c d | a b c d)The input is extended by wrapping around to the opposite edge.
'wrap' (
d b c d | a b c d | b c a b)The input is extended by wrapping around to the opposite edge, but in a way such that the last point and initial point exactly overlap. In this case it is not well defined which sample will be chosen at the point of overlap.
cval: scalar, optionalValue to fill past edges of input if
modeis 'constant'. Default is 0.0.prefilter: bool, optionalDetermines if the input array is prefiltered with
spline_filterbefore interpolation. The default is True, which will create a temporaryfloat64array of filtered values iforder > 1. If setting this to False, the output will be slightly blurred iforder > 1, unless the input is prefiltered, i.e. it is the result of callingspline_filteron the original input.extra_arguments: tuple, optionalExtra arguments passed to
mapping.extra_keywords: dict, optionalExtra keywords passed to
mapping.
Returns
output: ndarrayThe filtered input.
Notes
This function also accepts low-level callback functions with one the following signatures and wrapped in 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)
The calling function iterates over the elements of the output array, calling the callback function at each element. The coordinates of the current output element are passed through output_coordinates. The callback function must return the coordinates at which the input must be interpolated in input_coordinates. The rank of the input and output arrays are given by input_rank and output_rank respectively. user_data is the data pointer provided to scipy.LowLevelCallable as-is.
The callback function must return an integer error status that is zero if something went wrong and one otherwise. If an error occurs, you should normally set the Python error status with an informative message before returning, otherwise a default error message is set by the calling function.
In addition, some other low-level function pointer specifications are accepted, but these are for backward compatibility only and should not be used in new code.
For complex-valued input, this function transforms the real and imaginary components independently.
Array API Standard Support
geometric_transform has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
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)
✗b = [1, 2, 3, 4, 5] def shift_func(output_coords): return (output_coords[0] - 3,) geometric_transform(b, shift_func, mode='constant') geometric_transform(b, shift_func, mode='nearest') geometric_transform(b, shift_func, mode='reflect') geometric_transform(b, shift_func, mode='wrap')✓
See also
- affine_transform
- map_coordinates
- spline_filter1d
Aliases
-
scipy.ndimage.geometric_transform