0

I have a 32x32 numpy array representing an image in which 50% values, which amount to 512 pixels, are NaN's. I want to use the griddata function from scipy.interpolate to fill in these missing values so that I can reconstruct the image.

However, I'm having a hard time understanding the griddata function and how exactly to pass my image array to it. The arguments of the function are listed in the documentation but I cannot understand what these arguments mean in the context of my data.

What I understand so far is that the xi argument indicates the indices in my image array where I want the interpolated values, which I presume would be all the locations where the NaN's are. The values argument would be my image array but the shape mentioned in the documentation is (n,) so do I have to flatten the array? And I'm really not sure what the points argument stands for.

The image array looks something like this:

array([[[ nan,  79.,  nan, ...,  nan,  nan,  44.],
        [ nan,  84.,  45., ...,  48.,  84.,  44.],
        [ nan,  nan,  56., ...,  42.,  66.,  34.],
        ...,
        [126.,  nan,  nan, ...,  70.,  nan, 133.],
        [135., 137.,  nan, ...,  nan,  nan,  nan],
        [142.,  nan,  nan, ...,  nan,  nan, 151.]]])

Any suggestions would be welcome. Also, is there a better way to interpolate the missing pixel values? Thank you.

chomprrr
  • 406
  • 4
  • 15

1 Answers1

1

To answer your question about how griddata works: in order to perform an interpolation, you need to provide both your existing data points (location and values) and the nodes onto which you wish to interpolate. The location of your existing data points corresponds to the first keyword (points), while their values are represented by the second keyword (values). Imagine you would measure the temperature on a surface, then points would give you the locations of your measurements while values tell you the corresponding temperature you recorded. Finally, the third keyword (xi) contains the coordinates of the points where you wish to interpolate your existing data. Typically, this would be a structured grid which has to be contained inside the spatial extent of your existing data. This means that, for each node in the grid, there exist data points which can define a polygon inside which the grid point is contained.

Now, for how to actually interpolate your data, this answer should help you.

Patol75
  • 4,342
  • 1
  • 17
  • 28
  • Thank you for the answer and the link! – chomprrr May 06 '19 at 17:22
  • This doesn't work in all cases. There will be no interpolation if your requested to be interpolated points are outside of the convex hull of your original points. For example if you have a whole row without values at the edge or a corner that doesn't contain any values. – eavsteen May 18 '20 at 13:06
  • "Typically, this would be a structured grid which has to be contained inside the spatial extent of your existing data. This means that, for each node in the grid, there exist data points which can define a polygon inside which the grid point is contained." I think this is roughly what I wrote, in simpler terms? – Patol75 May 18 '20 at 23:36