# Skimage Sample From Non-integer Locations In Image?

## 18 June 2018 - 1 answer

We can sample from integer positions in an image because images are constructed as 2d arrays, and we just take whatever data is sitting in the array location.

With non-integer positions, like say between two pixels, this is not so straightforward. However, it's such a common problem that (for ex.) GPUs have this functionality baked into the hardware so long as you're satisfied with linear interpolation. I can't find any functionality for this in skimage, but it seems so fundamental to image processing that I feel like I must be missing something.

I would expect something like:

``````sample(img, (64.5, 120.37), interpolation='linear')
``````

Scipy has interp2d that can be used successfully for image interpolation.

Let's start with a sample image (random grayscale to keep it simple, colormap comes from matplotlib which I'm using for plotting):

``````np.random.seed(42)
np.random.randint(255, (10, 10))
`````` Now we can initialize our interpolator

``````from scipy.interpolate import interp2d
x = np.arange(10)
y = np.arange(10)
f = interp2d(x, y, img, kind="cubic")
``````

and evaluate it on a new grid

``````xdense = np.linspace(0, 9, 100)
ydense = np.linspace(0, 9, 100)
newimg = f(xdense, ydense)
`````` And you can also use it to sample arbitrary points

``````f(0.192321, 5.99927371)
``````

Gives

``````array([99.04826046])
``````

With `skimage` you could maybe obtain something similar rescaling and resampling, but this method looks a lot more convenient to me.