Is there A 1D interpolation (along one axis) of an image using two images (2D arrays) as inputs?

2k views Asked by At

I have two images representing x and y values. The images are full of 'holes' (the 'holes' are the same in both images).

I want to interpolate (linear interpolation is fine though higher level interpolation is preferable) along ONE of the axis in order to 'fill' the holes.

Say the axis of choice is 0, that is, I want to interpolate across each column. All I have found with numpy is interpolation when x is the same (e.g. numpy.interpolate.interp1d). In this case, however, each x is different (i.e. the holes or empty cells are different in each row).

Is there any numpy/scipy technique I can use? Could a 1D convolution work?(though kernels are fixed)

1

There are 1 answers

3
AudioBubble On BEST ANSWER

You still can use interp1d:

import numpy as np
from scipy import interpolate
A = np.array([[1,np.NaN,np.NaN,2],[0,np.NaN,1,2]])
#array([[  1.,  nan,  nan,   2.],
#       [  0.,  nan,   1.,   2.]])

for row in A:
    mask = np.isnan(row)
    x, y = np.where(~mask)[0], row[~mask]
    f = interpolate.interp1d(x, y, kind='linear',)
    row[mask] = f(np.where(mask)[0])
#array([[ 1.        ,  1.33333333,  1.66666667,  2.        ],
#       [ 0.        ,  0.5       ,  1.        ,  2.        ]])