Scipy interp2d interpolate masked fill values

半腔热情 提交于 2020-05-15 05:53:13

问题


I want to interpolate data (120*120) in order to get output data (1200*1200).

In this way I'm using scipy.interpolate.interp2d.

Below is my input data, where 255 corresponds to fill values, I mask these values before the interpolation.

I'm using the code below:

tck = interp2d(np.linspace(0, 1200, data.shape[1]),
               np.linspace(0, 1200, data.shape[0]),
               data,
               fill_value=255)
data = tck(range(1200), range(1200))
data = np.ma.MaskedArray(data, data == 255)

I get the following result:

Fill values have been interpolated.

How can I interpolate my data without interpolate fill values ?


回答1:


I found a solution with scipy.interpolate.griddata but I'm not sure that's the best one.

I interpolate data with the nearest method parameter which returns the value at the data point closest to the point of interpolation.

points = np.meshgrid(np.linspace(0, 1200, data.shape[1]),
                     np.linspace(0, 1200, data.shape[0]))
points = zip(points[0].flatten(), points[1].flatten())
xi = np.meshgrid(np.arange(1200), np.arange(1200))
xi = zip(xi[0].flatten(), xi[1].flatten())

tck = griddata(np.array(points), data.flatten(), np.array(xi), method='nearest')
data = tck.reshape((1200, 1200))



来源:https://stackoverflow.com/questions/41298078/scipy-interp2d-interpolate-masked-fill-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!