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...
-
Not that this is not a duplicate of stackoverflow.com/questions/9537543/…, that question's title is just misleadingtbrugere– tbrugere2021年06月30日 15:35:02 +00:00Commented Jun 30, 2021 at 15:35
-
would filling the point with the average of the surrounding pixels be sufficient?Conic– Conic2021年06月30日 15:37:32 +00:00Commented Jun 30, 2021 at 15:37
-
And if there are multiple different values the same distance away?Scott Hunter– Scott Hunter2021年06月30日 15:37:52 +00:00Commented Jun 30, 2021 at 15:37
-
@Conic the surrounding pixels may be NaN too. But for my application, mean would be alright tootbrugere– tbrugere2021年06月30日 15:37:58 +00:00Commented Jun 30, 2021 at 15:37
-
@Scott Hunter then any of these is finetbrugere– tbrugere2021年06月30日 15:38:22 +00:00Commented Jun 30, 2021 at 15:38
1 Answer 1
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
answered Jun 30, 2021 at 15:38
orlp
119k39 gold badges226 silver badges325 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py