Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resampling of 2D numpy array

I have a 2D array of size (3,2) and i have to re sample this by using nearest neighbor, linear and bi cubic method of interpolation so that the size become (4,3).

I am using Python, numpy and scipy for this.

How can I achieve resampling of the input array?

like image 703
Joel Avatar asked Oct 21 '25 12:10

Joel


1 Answers

There is a good tutorial on re-sampling using convolution here.

For integer factor up-scaling:

import numpy
import scipy
from scipy import ndimage, signal

# Scale factor
factor = 2

# Input image
a = numpy.arange(16).reshape((4,4))

# Empty image enlarged by scale factor
b = numpy.zeros((a.shape[0]*factor, a.shape[0]*factor))

# Fill the new array with the original values
b[::factor,::factor] = a

# Define the convolution kernel
kernel_1d = scipy.signal.boxcar(factor)
kernel_2d = numpy.outer(kernel_1d, kernel_1d)

# Apply the kernel by convolution, seperately in each axis
c = scipy.signal.convolve(b, kernel_2d, mode="valid")

Note that the factor can be different for each axis, and that you can also apply the convolution sequentially, on each axis. The kernels for bi-linear and bi-cubic are also shown in the link, with the bilinear interpolation making use of a triangular signal (scipy.signal.triang) and bi-cubic being a piece wise function.

You should also mind which portion of the interpolated image is valid; along the edges there is not sufficient support for the kernel.

Bi-cubic interpolation is the best option of the three, as far as satellite imagery goes.

like image 77
Benjamin Avatar answered Oct 23 '25 02:10

Benjamin