Fill nan with nearest neighbor in numpy array
Asked Answered
L

1

6

I got this 2D numpy array with missing values. Is there a simple (and reasonably fast) way of filling the nan values with the closest (preferably euclidean distance, but manhattan is ok too) non-nan value? I couldn't find such a function in numpy or scipy…

Lorylose answered 30/6, 2021 at 15:34 Comment(5)
Not that this is not a duplicate of #9538043, that question's title is just misleadingLorylose
would filling the point with the average of the surrounding pixels be sufficient?Poleax
And if there are multiple different values the same distance away?Hatchet
@Poleax the surrounding pixels may be NaN too. But for my application, mean would be alright tooLorylose
@Scott Hunter then any of these is fineLorylose
J
14

Use scipy.interpolate.NearestNDInterpolator.

E.g.:

from scipy.interpolate import NearestNDInterpolator
data = ... # shape (w, h)
mask = np.where(~np.isnan(data))
interp = NearestNDInterpolator(np.transpose(mask), data[mask])
filled_data = interp(*np.indices(data.shape))

Showing it in action (with black as the mask here, image_defect is from from here):

data = image_defect
mask = np.where(~(data == 0))
interp = NearestNDInterpolator(np.transpose(mask), data[mask])
image_result = interp(*np.indices(data.shape))

Then, using the plotting code from scipy: enter image description here

Jarrodjarrow answered 30/6, 2021 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.