ndimage.interpolation

Module Contents

Functions

_extend_mode_to_code(mode)
spline_filter1d(input,order=3,axis=None,output=None) Calculates a one-dimensional spline filter along the given axis.
spline_filter(input,order=3,output=None) Multi-dimensional spline filter.
geometric_transform(input,mapping,output_shape=None,output=None,order=3,mode=”constant”,cval=0.0,prefilter=True,extra_arguments=tuple,extra_keywords=dict) Apply an arbitrary geometric transform.
map_coordinates(input,coordinates,output=None,order=3,mode=”constant”,cval=0.0,prefilter=True) Map the input array to new coordinates by interpolation.
affine_transform(input,matrix,offset=0.0,output_shape=None,output=None,order=3,mode=”constant”,cval=0.0,prefilter=True) Apply an affine transformation.
shift(input,shift,output=None,order=3,mode=”constant”,cval=0.0,prefilter=True) Shift an array.
zoom(input,zoom,output=None,order=3,mode=”constant”,cval=0.0,prefilter=True) Zoom an array.
_minmax(coor,minc,maxc)
rotate(input,angle,axes=tuple,reshape=True,output=None,order=3,mode=”constant”,cval=0.0,prefilter=True) Rotate an array.
_extend_mode_to_code(mode)
spline_filter1d(input, order=3, axis=None, output=None)

Calculates a one-dimensional spline filter along the given axis.

The lines of the array along the given axis are filtered by a spline filter. The order of the spline must be >= 2 and <= 5.

input : array_like
The input array.
order : int, optional
The order of the spline, default is 3.
axis : int, optional
The axis along which the spline filter is applied. Default is the last axis.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array. Default is numpy.float64.
spline_filter1d : ndarray or None
The filtered input. If output is given as a parameter, None is returned.
spline_filter(input, order=3, output=None)

Multi-dimensional spline filter.

For more details, see spline_filter1d.

spline_filter1d

The multi-dimensional filter is implemented as a sequence of one-dimensional spline filters. The intermediate arrays are stored in the same data type as the output. Therefore, for output types with a limited precision, the results may be imprecise because intermediate results may be stored with insufficient precision.

geometric_transform(input, mapping, output_shape=None, output=None, order=3, mode="constant", cval=0.0, prefilter=True, extra_arguments=tuple, extra_keywords=dict)

Apply an arbitrary geometric transform.

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.

input : array_like
The 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, optional
Shape tuple.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array.
order : int, optional
The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode : str, optional
Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.
cval : scalar, optional
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0
prefilter : bool, optional
The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.
extra_arguments : tuple, optional
Extra arguments passed to mapping.
extra_keywords : dict, optional
Extra keywords passed to mapping.
return_value : ndarray or None
The filtered input. If output is given as a parameter, None is returned.

map_coordinates, affine_transform, spline_filter1d

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.

>>> from scipy import ndimage
>>> a = np.arange(12.).reshape((4, 3))
>>> def shift_func(output_coords):
...     return (output_coords[0] - 0.5, output_coords[1] - 0.5)
...
>>> ndimage.geometric_transform(a, shift_func)
array([[ 0.   ,  0.   ,  0.   ],
       [ 0.   ,  1.362,  2.738],
       [ 0.   ,  4.812,  6.187],
       [ 0.   ,  8.263,  9.637]])
map_coordinates(input, coordinates, output=None, order=3, mode="constant", cval=0.0, prefilter=True)

Map the input array to new coordinates by interpolation.

The array of coordinates 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.

The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found.

input : ndarray
The input array.
coordinates : array_like
The coordinates at which input is evaluated.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array.
order : int, optional
The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode : str, optional
Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.
cval : scalar, optional
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0
prefilter : bool, optional
The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.
map_coordinates : ndarray
The result of transforming the input. The shape of the output is derived from that of coordinates by dropping the first axis.

spline_filter, geometric_transform, scipy.interpolate

>>> from scipy import ndimage
>>> a = np.arange(12.).reshape((4, 3))
>>> a
array([[  0.,   1.,   2.],
       [  3.,   4.,   5.],
       [  6.,   7.,   8.],
       [  9.,  10.,  11.]])
>>> ndimage.map_coordinates(a, [[0.5, 2], [0.5, 1]], order=1)
array([ 2.,  7.])

Above, the interpolated value of a[0.5, 0.5] gives output[0], while a[2, 1] is output[1].

>>> inds = np.array([[0.5, 2], [0.5, 4]])
>>> ndimage.map_coordinates(a, inds, order=1, cval=-33.3)
array([  2. , -33.3])
>>> ndimage.map_coordinates(a, inds, order=1, mode='nearest')
array([ 2.,  8.])
>>> ndimage.map_coordinates(a, inds, order=1, cval=0, output=bool)
array([ True, False], dtype=bool)
affine_transform(input, matrix, offset=0.0, output_shape=None, output=None, order=3, mode="constant", cval=0.0, prefilter=True)

Apply an affine transformation.

Given an output image pixel index vector o, the pixel value is determined from the input image at position np.dot(matrix, o) + offset.

input : ndarray
The input array.
matrix : ndarray

The inverse coordinate transformation matrix, mapping output coordinates to input coordinates. If ndim is the number of dimensions of input, the given matrix must have one of the following shapes:

  • (ndim, ndim): the linear transformation matrix for each output coordinate.
  • (ndim,): assume that the 2D transformation matrix is diagonal, with the diagonal specified by the given value. A more efficient algorithm is then used that exploits the separability of the problem.
  • (ndim + 1, ndim + 1): assume that the transformation is specified using homogeneous coordinates [1]. In this case, any value passed to offset is ignored.
  • (ndim, ndim + 1): as above, but the bottom row of a homogeneous transformation matrix is always [0, 0, ..., 1], and may be omitted.
offset : float or sequence, optional
The offset into the array where the transform is applied. If a float, offset is the same for each axis. If a sequence, offset should contain one value for each axis.
output_shape : tuple of ints, optional
Shape tuple.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array.
order : int, optional
The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode : str, optional
Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.
cval : scalar, optional
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0
prefilter : bool, optional
The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.
affine_transform : ndarray or None
The transformed input. If output is given as a parameter, None is returned.

The given matrix and offset are used to find for each point in the output the corresponding coordinates in the input by an affine transformation. The value of the input at those coordinates is determined by spline interpolation of the requested order. Points outside the boundaries of the input are filled according to the given mode.

Changed in version 0.18.0: Previously, the exact interpretation of the affine transformation depended on whether the matrix was supplied as a one-dimensional or two-dimensional array. If a one-dimensional array was supplied to the matrix parameter, the output pixel value at index o was determined from the input image at position matrix * (o + offset).

[1]https://en.wikipedia.org/wiki/Homogeneous_coordinates
shift(input, shift, output=None, order=3, mode="constant", cval=0.0, prefilter=True)

Shift an array.

The array is shifted using spline interpolation of the requested order. Points outside the boundaries of the input are filled according to the given mode.

input : ndarray
The input array.
shift : float or sequence
The shift along the axes. If a float, shift is the same for each axis. If a sequence, shift should contain one value for each axis.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array.
order : int, optional
The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode : str, optional
Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.
cval : scalar, optional
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0
prefilter : bool, optional
The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.
shift : ndarray or None
The shifted input. If output is given as a parameter, None is returned.
zoom(input, zoom, output=None, order=3, mode="constant", cval=0.0, prefilter=True)

Zoom an array.

The array is zoomed using spline interpolation of the requested order.

input : ndarray
The input array.
zoom : float or sequence
The zoom factor along the axes. If a float, zoom is the same for each axis. If a sequence, zoom should contain one value for each axis.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array.
order : int, optional
The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode : str, optional
Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.
cval : scalar, optional
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0
prefilter : bool, optional
The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.
zoom : ndarray or None
The zoomed input. If output is given as a parameter, None is returned.
_minmax(coor, minc, maxc)
rotate(input, angle, axes=tuple, reshape=True, output=None, order=3, mode="constant", cval=0.0, prefilter=True)

Rotate an array.

The array is rotated in the plane defined by the two axes given by the axes parameter using spline interpolation of the requested order.

input : ndarray
The input array.
angle : float
The rotation angle in degrees.
axes : tuple of 2 ints, optional
The two axes that define the plane of rotation. Default is the first two axes.
reshape : bool, optional
If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.
output : ndarray or dtype, optional
The array in which to place the output, or the dtype of the returned array.
order : int, optional
The order of the spline interpolation, default is 3. The order has to be in the range 0-5.
mode : str, optional
Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.
cval : scalar, optional
Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0
prefilter : bool, optional
The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.
rotate : ndarray or None
The rotated input. If output is given as a parameter, None is returned.