2021年4月1日星期四

Forward-fill NaN values of an image array on python

I would now like to forward-fill the nan values in array rr_gray. So I require to replace each nan value with the nearest valid value. Thus I want to fill in missing values with the nearest neighbor, not just any neighbor.

Currently rr_gray is a numpy array of this sort:

[[ nan  nan  nan ...  nan  nan  nan]   [ nan  nan  nan ...  nan  nan  nan]   [ nan  nan  nan ...  nan  nan  nan]   ...   [ 13.  32.  41. ... 128.  89.  79.]   [ 30.  17.  11. ... 118.  78.  72.]   [ 24.  33.  27. ... 116.  81.  69.]]  

Till now I attempted to create a mask that finds the nan values in the array and then replace those values accordingly however it does not seem to be working. Here is what I have done.

import cv2  import imutils  import numpy as np    rr_imgs_all = np.zeros((1,112,112))  for rraw in range(len(raw_train[0:1])):       #LOAD IMAGES      rr_image = cv2.imread(raw_train[rraw])      rr_gray = cv2.cvtColor(rr_image, cv2.COLOR_BGR2GRAY)      rr_gray = imutils.resize(rr_gray, width=112, height=112)      rr_gray = np.where(rr_gray==0, np.nan, rr_gray)       print(rr_gray)        mask = np.isnan(rr_gray)      idx = np.where(~mask,np.arange(mask.shape[1]),0)      np.maximum.accumulate(idx,axis=1, out=idx)      rr_gray[mask] = rr_gray[np.nonzero(mask)[0], idx[mask]]        print(rr_gray)  

Target Output

https://stackoverflow.com/questions/66913047/forward-fill-nan-values-of-an-image-array-on-python April 02, 2021 at 08:21AM

没有评论:

发表评论