1

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)

marcos
  • 134
  • 15

1 Answers1

1

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.        ]])
  • This is useful however, this solution requires going row by row or column by column. I have and image of 3600x1000 (it may get bigger). I was hoping to find something can be used on the image as a whole? – marcos Jun 14 '15 at 11:20
  • This is the only solution that I could find, sorry. –  Jun 14 '15 at 13:22
  • 1
    At the end it was a combination of your solution and the one offered by @ali_m (see above) that did the trick! – marcos Jun 15 '15 at 14:24