4

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?

4
  • What do you mean exactly by resampling? Do you have a link to an explaination of what that means? Commented Dec 21, 2016 at 13:48
  • actually, i need to convert a low resolution image to high resolution but using numpy. Commented Dec 21, 2016 at 13:57
  • I don't think that is possible unless you have a better resolution original image. Once you have taken an image, there is no sub-pixel information in that image... Commented Dec 21, 2016 at 13:59
  • 1
    I have one Panchromatic Image( High Resolution) and one multispectral image(low resolution). and i have to change this low resolution to High Resolution. Commented Dec 21, 2016 at 14:03

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a simpler solution for this https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html.

Nearest neighbor interpolation is order=0, bilinear interpolation is order=1, and bicubic is order=3 (default).

import numpy as np
import scipy.ndimage

x = np.arange(6).reshape(3,2).astype(float)
z = (4/3, 3/2)
print('Original array:\n{0}\n\n'.format(x))
methods=['nearest-neighbor', 'bilinear', 'biquadratic', 'bicubic']
for o in range(4):
    print('Resampled with {0} interpolation:\n {1}\n\n'.
    format(methods[o],  scipy.ndimage.zoom(x, z, order=o)))

This results to:

Original array:
[[0. 1.]
 [2. 3.]
 [4. 5.]]


Resampled with nearest-neighbor interpolation:
 [[0. 1. 1.]
 [2. 3. 3.]
 [2. 3. 3.]
 [4. 5. 5.]]


Resampled with bilinear interpolation:
 [[0.         0.5        1.        ]
 [1.33333333 1.83333333 2.33333333]
 [2.66666667 3.16666667 3.66666667]
 [4.         4.5        5.        ]]


Resampled with biquadratic interpolation:
 [[1.04083409e-16 5.00000000e-01 1.00000000e+00]
 [1.11111111e+00 1.61111111e+00 2.11111111e+00]
 [2.88888889e+00 3.38888889e+00 3.88888889e+00]
 [4.00000000e+00 4.50000000e+00 5.00000000e+00]]


Resampled with bicubic interpolation:
 [[5.55111512e-16 5.00000000e-01 1.00000000e+00]
 [1.03703704e+00 1.53703704e+00 2.03703704e+00]
 [2.96296296e+00 3.46296296e+00 3.96296296e+00]
 [4.00000000e+00 4.50000000e+00 5.00000000e+00]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.