如何模糊点的 3D 阵列,同时保持其原始值? (Python)

How to blur 3D array of points, while maintaining their original values? (Python)

我有一个稀疏的 3D 值数组。我试图通过对数组应用高斯滤波器将每个 "point" 变成模糊 "sphere"。

我希望 (x,y,z) 点的原始值保持不变。我只想围绕这一点创建衰减值...但是应用高斯滤波器也会更改原始 (x,y,z) 值。

我目前正在这样做:

dataCube = scipy.ndimage.filters.gaussian_filter(dataCube, 3, truncate=8)

有没有办法让我对其进行规范化,或者做些什么让我的原始值仍然在这个新的 dataCube 中?如果这不是最好的方法,我不一定要使用高斯滤波器。

您可以使用以 1 为中心值且宽度小于数据点间距的内核的卷积来执行此操作。

一维示例:

import numpy as np
import scipy.signal
data = np.array([0,0,0,0,0,5,0,0,0,0,0])
kernel = np.array([0.5,1,0.5])
scipy.signal.convolve(data, kernel, mode="same")

给予

array([ 0. ,  0. ,  0. ,  0. ,  2.5,  5. ,  2.5,  0. ,  0. ,  0. ,  0. ])

请注意,对于大型数组,fftconvolve 可能要快得多。您还必须指定在数组的边界处应该发生什么。

更新: 3-d 示例

import numpy as np
from scipy import signal

# first build the smoothing kernel
sigma = 1.0     # width of kernel
x = np.arange(-3,4,1)   # coordinate arrays -- make sure they contain 0!
y = np.arange(-3,4,1)
z = np.arange(-3,4,1)
xx, yy, zz = np.meshgrid(x,y,z)
kernel = np.exp(-(xx**2 + yy**2 + zz**2)/(2*sigma**2))

# apply to sample data
data = np.zeros((11,11,11))
data[5,5,5] = 5.
filtered = signal.convolve(data, kernel, mode="same")

# check output
print filtered[:,5,5]

给予

[ 0.          0.          0.05554498  0.67667642  3.0326533   5.          3.0326533
  0.67667642  0.05554498  0.          0.        ]