# Interpolate the value from a discreted image with grid_sample function

I want to interpolate the value from a discreted image. Speciafically, I have a grid, a mask and an input. Only the points whose mask is 1 of the input will contribute the interpolation. Is there any way to achieve this with grid_sample function?

Any one can help me? Thanks a lot!

How would the output values be calculated if all neighbors of the output pixel locations have a 0 in their mask?

Hi, for the interpolation, the nearest points whose mask is one will be taken into account. The point with mask 0 will be ignored.

Hi, for the interpolation, the nearest points whose mask is one will be taken into account. The point with mask 0 will be ignored. Is there any way to achieve this by grid sample?

I don’t understand the use case completely.
An interpolation would use neighboring values to calculate the value at the new output location using a defined method, such as linear interpolation etc.
E.g. for a simple linear interpolation of a 1D singal, the output location at coordinate `[0.2]`, would get the new value as `input * 0.8 + input * 0.2`. How would this value be calculated, if the mask has a zero value for this coordinate?

Thank you for the reply. For my case, suggest the mask is [1, 0, 1, 0], for the output location at [1.2], the output value will be input(1.8/2)+input(0.2/2). Which means, I will find the nearest points whose masks are not zero for the linear interpolation.

This would mean that `input` would be multiplied by zero, as its the mask value there?
In that case you could multiply the input tensor with the mask and perform a “standard” interpolation.

I’m afraid not. Take the input [2,10,3,4] as an example. The output in my case should be 20.9+30.1=2.1. But if I multiply 10 with zero and perform a “standard” interpolation, the output will be 20.8+00.2=1.6.

sorry the formulas are `2*0.9+3*0.1=2.1` and`2*0.8+0*0.2=1.6`

Ah OK, so in the previous fomula `input` would be used, no?
I don’t know, how this would work out of the box and would need to run some code first.

No. The source code of the grid_sample will choose the nearest point of the target position as `(x,y)` and use `(x,y), (x+1, y), (x, y+1) and (x+1, y+1)` for the interpolation process.