如何在保持原始值的同时模糊3D点阵列? (蟒蛇) [英] How to blur 3D array of points, while maintaining their original values? (Python)

查看:199
本文介绍了如何在保持原始值的同时模糊3D点阵列? (蟒蛇)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个稀疏的三维数组值。我试图把每个点变成一个模糊的球,通过应用一个高斯滤波器的阵列。



我想要点(x,y,z)的原始值保持不变。我只是想在这个点附近创建衰减值...但是应用高斯滤波器也会改变原来的(x,y,z)值。

我目前这样做:
$ b $ p $ dataCube = scipy.ndimage.filters.gaussian_filter(dataCube,3,truncate = 8)



有没有办法让我规范这个,或者做一些事情,让我的原始值仍然在这个新的dataCube?我不一定要使用高斯过滤器,如果这不是最好的方法。

解决方案

您可以使用与以1为中心值的内核进行卷积,并且宽度小于数据点之间的间距。

1-d示例:

  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)

赋予

 数组([0.,0,0,0,0.5,2.5 ...,2.5,0,0,0,0,0])

注意 fftconvolve 可能很多更快的大型阵列。您还必须指定在数组边界处会发生什么。



更新: 3-d示例

  import numpy as np 
from scipy import signal

#先构建平滑内核
sigma = 1.0 #内核的宽度
x = np.arange(-3,4,1)#坐标数组 - 确保它们包含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样本数据
data = np.zeros((11,11,11))
data [5,5,5] = 5.
filtered = signal.convolve(data,kernel,mode =相同)

#检查输出
打印过滤[:,5,5]



给出

pre code $ 0.055554498 0. 0.]


I have a sparse 3D array of values. I am trying to turn each "point" into a fuzzy "sphere", by applying a Gaussian filter to the array.

I would like the original value at the point (x,y,z) to remain the same. I just want to create falloff values around this point... But applying the Gaussian filter changes the original (x,y,z) value as well.

I am currently doing this:

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

Is there a way for me to normalize this, or do something so that my original values are still in this new dataCube? I am not necessarily tied to using a Gaussian filter, if that is not the best approach.

解决方案

You can do this using a convolution with a kernel that has 1 as its central value, and a width smaller than the spacing between your data points.

1-d example:

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")

gives

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

Note that fftconvolve might be much faster for large arrays. You also have to specify what should happen at the boundaries of your array.

Update: 3-d example

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]

gives

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

这篇关于如何在保持原始值的同时模糊3D点阵列? (蟒蛇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆