【发布时间】:2021-04-23 00:13:28
【问题描述】:
给定一个点 (0.8, 0.6) 和一个强度 (3),可以将单个点双线性“反向插值”到具有整数索引 (0,0) -> (1,1) 的 2x2 网格上。
点和网格的方向是第一个条目向下增加,第二个条目向右增加。
在这样的网格上,上面坐标的权重变成:
0.08 | 0.12
----------------
0.32 | 0.48
我们可以乘以与坐标相关的强度,得到一个 2x2 网格双线性加权强度:
0.24 | 0.36
----------------
0.96 | 1.44
并且可以这样绘制:
对于几个点,可以将它们加权到同一个数组上(完整代码如下):
points = np.array([(0.8, 0.6), (2.2, 2.6),(5, 1), (3, 4.2), (8.5, 8.2)])
intens = np.array([3, 3, 1, 1, 2])
image, weight = bilinear(points, intens)
对于我的工作,我需要 weights 和 intensity*weights 作为输出数组。我需要在非常大(数百万)的坐标上执行上述操作,其中坐标的值从 0.0 到 4095.0。我在下面编写了一个 numpy 例程,虽然它对于 100_000 个点相当快(1.25 秒),但我希望它更快,因为我需要在我拥有的 10_000_000 个数据点上多次调用它。
我考虑过对 numpy 代码进行矢量化而不是 for 循环,但后来我为每个点生成了一个 4096x4096 的大部分空数组,然后我将对其求和。那将需要 1000 TB 的内存。
我也尝试过在 cupy 中简单的实现,但是由于我使用了 for 循环,它变得太慢了。
在我的代码中,我为每个点生成一个 2x2 加权数组,将数组乘以强度,然后通过切片将它们添加到主数组中。有没有更好的办法?
import numpy as np
def bilinear(points, intensity):
"""Bilinear weighting of points onto a grid.
Extent of grid given by min and max of points in each dimension
points should have shape (N, 2)
intensity should have shape (N,)
"""
floor = np.floor(points)
ceil = floor + 1
floored_indices = np.array(floor, dtype=int)
low0, low1 = floored_indices.min(0)
high0, high1 = floored_indices.max(0)
floored_indices = floored_indices - (low0, low1)
shape = (high0 - low0 + 2, high1-low1 + 2)
weights_arr = np.zeros(shape, dtype=float)
int_arr = np.zeros(shape, dtype=float)
upper_diff = ceil - points
lower_diff = points - floor
w1 = np.prod((upper_diff), axis=1)
w2 = upper_diff[:,0]*lower_diff[:,1]
w3 = lower_diff[:,0]*upper_diff[:,1]
w4 = np.prod((lower_diff), axis=1)
for i, index in enumerate(floored_indices):
s = np.s_[index[0]:index[0]+2, index[1]:index[1]+2]
weights = np.array([[w1[i], w2[i]], [w3[i], w4[i]]])
weights_arr[s] += weights
int_arr[s] += intensity[i]*weights
return int_arr, weights_arr
rng = np.random.default_rng()
N_points = 10_000 # use 10_000 so it is quick
image_shape = (256, 256) # Use 256 so it isn't so big
points = rng.random((N_points, 2)) * image_shape
intensity = rng.random(N_points)
image, weight = bilinear(points, intensity)
为了测试代码,我还提供了以下绘图代码 - 仅用于少量 (~10) 点,否则散点将覆盖整个图像。
import matplotlib.pyplot as plt
floor = np.floor(points) - 0.5
lower, left = floor.min(0)
upper, right = (floor).max(0) + 2
extent = (left, right, upper, lower)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(6,3))
ax1.scatter(*points[:,::-1].T, c='red')
im1 = ax1.imshow(weight, clim=(image.min(), image.max()), extent=extent)
ax1.set(title='Weight', xlim=(left - 1, right + 1), ylim = (upper + 1, lower - 1))
colorbar(im1)
ax2.scatter(*points[:,::-1].T , c='red')
im2 = ax2.imshow(image, extent=extent)
ax2.set(title='Weight x Intensity', xlim=(left - 1, right + 1), ylim = (upper + 1, lower - 1))
colorbar(im2)
plt.tight_layout()
plt.show()
# If labeling the first point
# ax1.text(*points[0].T, f"({points[0,0]}, {points[0,1]})", va='bottom', ha='center', color='red')
# ax2.text(*points[0].T, f"({points[0,0]}, {points[0,1]}, {intens[0]})", va='bottom', ha='center', color='red')
【问题讨论】: