0

I've a numpy array of shape (960, 2652), I want to change its size to (1000, 1600) using linear/cubic interpolation.

>>> print(arr.shape)
(960, 2652)

I've check this and this answer, which recommends to use scipy.interpolate.interp2d, but what should I provide as x and y?

from scipy import interpolate
f = interpolate.interp2d(x, y, arr, kind='cubic')
1
  • 2
    X and y can be range objects as coordinates Commented Feb 25, 2020 at 11:14

3 Answers 3

4

The datapoints on the old domain, i.e.

import numpy as np
from scipy import interpolate
from scipy import misc
import matplotlib.pyplot as plt

arr = misc.face(gray=True)

x = np.linspace(0, 1, arr.shape[0])
y = np.linspace(0, 1, arr.shape[1])
f = interpolate.interp2d(y, x, arr, kind='cubic')

x2 = np.linspace(0, 1, 1000)
y2 = np.linspace(0, 1, 1600)
arr2 = f(y2, x2)

arr.shape # (768, 1024)
arr2.shape # (1000, 1600)

plt.figure()
plt.imshow(arr)
plt.figure()
plt.imshow(arr2)
Sign up to request clarification or add additional context in comments.

Comments

2

skimage.transform.resize is a very convenient way to do this:

import numpy as np
from skimage.transform import resize
import matplotlib.pyplot as plt
from scipy import misc
    
arr = misc.face(gray=True)

dim1, dim2 = 1000, 1600
    
arr2= resize(arr,(dim1,dim2),order=3)  #order = 3 for cubic spline
    
print(arr2.shape) 

plt.figure()
plt.imshow(arr)
plt.figure()
plt.imshow(arr2)

Comments

0

for new version of spicy (>1.10)

import numpy as np
import spipy as sp

def inter_image(image, size_out):    
    x_size = image.shape[0]
    y_size = image.shape[1]

    x = np.linspace(0, 1, x_size)
    y = np.linspace(0, 1, y_size)
    f = sp.interpolate.RegularGridInterpolator((y, x), image)

    x = np.linspace(0, 1, size_out[0])
    y = np.linspace(0, 1, size_out[1])
    xg, yg = np.meshgrid(x, y)

    return  f((yg, xg))

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.