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…
Fill nan with nearest neighbor in numpy array
Not that this is not a duplicate of #9538043, that question's title is just misleading –
Lorylose
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 too –
Lorylose
@Scott Hunter then any of these is fine –
Lorylose
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))
© 2022 - 2024 — McMap. All rights reserved.