简体   繁体   English

如何在具有稀疏数据的2D numpy数组上进行线性插值?

[英]how to do linear interpolation on a 2D numpy array having sparse data?

I have a 2D numpy array... there are some values in the image and rest is sparse. 我有一个2D numpy数组...图像中有一些值,其余部分稀疏。 For linear imterpolation, I want to take the first column of the array. 对于线性插值,我想获取数组的第一列。 See where the values are present and do the linear interpolation on the zero values but only on one interval. 查看存在值的位置,并对零值但仅对一个间隔进行线性插值。

We loop over every column of the 2D array 我们遍历二维数组的每一列

As an example, consider following as the first column 例如,考虑以下内容作为第一列

   a = [0,0,0,0,1,0,0,0,2,0,0,10,0,0,3,4,6,0,0,1,0,0]

The first four 0,0,0,0 will be the same copy of the first non_zero element in our case this is 1. 前四个0,0,0,0将与第一个non_zero元素的副本相同,在本例中为1。

The second linear interpolation interval will be 第二个线性插值间隔为

   [1,0,0,0,2]

The third and rest will be 第三和其余将是

   [2,0,0,10]
   [10,0,0,3]
   [6,0,0,1]

At the end the last element will be copied. 最后,将复制最后一个元素。

Thanks a lot 非常感谢

Try something like this: 尝试这样的事情:

import numpy as np

a = np.array([0,0,0,0,1,0,0,0,2,0,0,10,0,0,3,4,6,0,0,1,0,0])
x, = np.nonzero(a)
a_filled = np.interp(np.arange(a.size), x, a[x])

This yields: 这样产生:

array([1, 1, 1, 1, 1, 1.25, 1.5, 1.75, 2, 4.67, 7.33, 10, 7.67, 5.33, 3, 4, 6, 4.33, 2.67, 1, 1, 1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM