Nearest Neighbor Imputation

I have images which have empty values, I want to fill the empty values with nearest neighbor. What is an efficient method for this sort of imputation?

The images will already be pytorch tensors on GPU, and sending to cpu and back to gpu is not an option, so I cannot use numpy nor scikit options.

How do you define a pixel that is empty? Do those pixels have an specific value?

yes they have a specific value, could be any value you want (including nan). without loss of generality consider it to be [0,0,0]

efficient method to fix isolated holes would be to prepare a substitute image, and do conditional blending with torch.where(hole, sub, original)

how to prepare such a substitute is another matter, using a smoothed image would be the simplest approach perhaps.

creating that substitute image is essentially the problem of imputation in and of itself. I know how to place the pixel values once I have them, but generating them is the problem of imputation. I’m not sure what you mean by “using a smoothed image” because when there are empty pixels, there are no values to “smooth”

if you’re talking about bigger regions missing, the problem is not cheaply solveable with generic math operators (built-in kernels), look for noise reduction algorithm implementations. for small holes, using something like gaussian blur may suffice