0

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

Beginner
  • 721
  • 11
  • 27
  • 2
    There's nothing implied about it. The function description states explicitly `"The value of the input at those coordinates is determined by spline interpolation of the requested order."` There may be other interpolators that use the method you want, but this isn't one of them. – hpaulj Sep 03 '19 at 21:01

1 Answers1

4

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) ...

bogovicj
  • 363
  • 2
  • 9
  • Is the result of bilinear same as linear? – Beginner Sep 04 '19 at 00:43
  • 1
    Yes. If your input is 2d, the order=1 case will be the same as bilinear interpolation. – bogovicj Sep 04 '19 at 00:51
  • Is there any effect of setting the argument `mode` in it, like does different values alter the effect? – Beginner Sep 05 '19 at 12:37
  • Also, I am getting "Fatal Python Error: Cannot recover from stack overflow` when I use order = 1 – Beginner Sep 05 '19 at 15:09
  • yes `mode` will change things - but only for indexes outside the "boundaries" of the image. re: the error - I don't know, that might be data dependent - try to come up with a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and consider making a question about it. – bogovicj Sep 05 '19 at 18:20
  • 1
    oops, you [did make a question about it](https://stackoverflow.com/questions/57810275/getting-fatal-python-error-cannot-recover-from-stack-overflow-while-applying-bi) – bogovicj Sep 05 '19 at 18:30