Search code examples
pythonpython-3.ximage-processingscipyndimage

Set interpolation method in scipy.ndimage.map_coordinates to nearest and bilinear


I want to set the interpolation method of scipy.ndimage.map_coordinates to bilinear and nearest(2 different for different examples).

There is nothing mentioned in the documentation about how to change the interpolation method.There is the following statement written in it which I am unable to comprehend.

order : int, optional The order of the spline interpolation, default is 3. The order has to be in the range 0-5.

Does this implies that in scipy.ndimage.map_coordinates only the spline interpolation is allowed? What I am missing and how to change the interpolation. P.s-I need it to change for elastic deformation task


Solution

  • You can get what you want with:

    • order=0 for nearest interpolation
    • order=1 for linear interpolation

    Example

    a = np.arange(12.).reshape((4, 3))
    inds = np.array([[0], [0.7]]) 
    ndimage.map_coordinates(a, inds, order=0) # returns [1.0]
    ndimage.map_coordinates(a, inds, order=1) # returns [0.7]
    

    Why

    Interpolation with a zero-order spline is nearest neighbor interpolation, and interpolation with a first-order spline is linear interpolation. From this paper:

    ...splines of degree 0 (piecewise constant) and splines of degree 1 (piecewise linear) ...