Search code examples
pythonimagenumpyscipyinterpolation

Interpolate Image for given indices python


I've an image of about 8000x9000 size as a numpy matrix. I also have a list of indices in a numpy 2xn matrix. These indices are fractional as well as may be out of image size. I need to interpolate the image and find the values for the given indices. If the indices fall outside, I need to return numpy.nan for them. Currently I'm doing it in for loop as below

def interpolate_image(image: numpy.ndarray, indices: numpy.ndarray) -> numpy.ndarray:
    """

    :param image:
    :param indices: 2xN matrix. 1st row is dim1 (rows) indices, 2nd row is dim2 (cols) indices
    :return:
    """
    # Todo: Vectorize this
    M, N = image.shape
    num_indices = indices.shape[1]
    interpolated_image = numpy.zeros((1, num_indices))
    for i in range(num_indices):
        x, y = indices[:, i]
        if (x < 0 or x > M - 1) or (y < 0 or y > N - 1):
            interpolated_image[0, i] = numpy.nan
        else:
            # Todo: Do Bilinear Interpolation. For now nearest neighbor is implemented
            interpolated_image[0, i] = image[int(round(x)), int(round(y))]
    return interpolated_image

But the for loop is taking huge amount of time (as expected). How can I vectorize this? I found scipy.interpolate.interp2d, but I'm not able to use it. Can someone explain how to use this or any other method is also fine. I also found this, but again it is not according to my requirements. Given x and y indices, these generated interpolated matrices. I don't want that. For the given indices, I just want the interpolated values i.e. I need a vector output. Not a matrix.

I tried like this, but as said above, it gives a matrix output

f = interpolate.interp2d(numpy.arange(image.shape[0]), numpy.arange(image.shape[1]), image, kind='linear')
interp_image_vect = f(indices[:,0], indices[:,1])
RuntimeError: Cannot produce output of size 73156608x73156608 (size too large)

For now, I've implemented nearest-neighbor interpolation. scipy interp2d doesn't have nearest neighbor. It would be good if the library function as nearest neighbor (so I can compare). If not, then also fine.


Solution

  • It looks like scipy.interpolate.RectBivariateSpline will do the trick:

    from scipy.interpolate import RectBivariateSpline
    image = # as given
    indices = # as given
    
    spline = RectBivariateSpline(numpy.arange(M), numpy.arange(N), image)
    
    interpolated = spline(indices[0], indices[1], grid=False)
    

    This gets you the interpolated values, but it doesn't give you nan where you need it. You can get that with where:

    nans = numpy.zeros(interpolated.shape) + numpy.nan
    x_in_bounds = (0 <= indices[0]) & (indices[0] < M)
    y_in_bounds = (0 <= indices[1]) & (indices[1] < N)
    bounded = numpy.where(x_in_bounds & y_in_bounds, interpolated, nans)
    

    I tested this with a 2624x2624 image and 100,000 points in indices and all told it took under a second.