如何在 python 中平滑具有不同高斯函数的二维数组的元素?
How can I smooth elements of a two-dimensional array with differing gaussian functions in python?
如何平滑数组的 x[1,3] 和 x[3,2] 元素,
x = np.array([[0,0,0,0,0],[0,0,0,1,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0]])
有两个宽度分别为1和2的二维高斯函数?本质上,我需要一个函数,允许我使用不同宽度的高斯平滑单个 "point like" 数组元素,这样我就得到一个具有平滑变化值的数组。
我对你提出的问题和你发表的评论有点困惑。在我看来,您想使用 scipy.ndimage.filters.gaussian_filter
但我不明白您的意思:
[...] gaussian functions with different sigma values to each pixel. [...]
事实上,由于您使用二维数组 x
,高斯滤波器将有 2 个参数。规则是:每个维度一个 sigma 值,而不是每个像素一个 sigma 值。
这是一个简短的例子:
import matplotlib.pyplot as pl
import numpy as np
import scipy as sp
import scipy.ndimage
n = 200 # widht/height of the array
m = 1000 # number of points
sigma_y = 3.0
sigma_x = 2.0
# Create input array
x = np.zeros((n, n))
i = np.random.choice(range(0, n * n), size=m)
x[i / n, i % n] = 1.0
# Plot input array
pl.imshow(x, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.savefig("array.png")
# Apply gaussian filter
sigma = [sigma_y, sigma_x]
y = sp.ndimage.filters.gaussian_filter(x, sigma, mode='constant')
# Display filtered array
pl.imshow(y, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.title("$\sigma_x = " + str(sigma_x) + "\quad \sigma_y = " + str(sigma_y) + "$")
pl.savefig("smooth_array_" + str(sigma_x) + "_" + str(sigma_y) + ".png")
这是初始数组:
以下是 sigma_x
和 sigma_y
的不同值的一些结果:
这样可以正确考虑 scipy.ndimage.filters.gaussian_filter
的第二个参数的影响。
但是,根据前面的引述,您可能对每个像素的不同权重分配更感兴趣。在这种情况下,scipy.ndimage.filters.convolve 就是您要查找的函数。下面是对应的例子:
import matplotlib.pyplot as pl
import numpy as np
import scipy as sp
import scipy.ndimage
# Arbitrary weights
weights = np.array([[0, 0, 1, 0, 0],
[0, 2, 4, 2, 0],
[1, 4, 8, 4, 1],
[0, 2, 4, 2, 0],
[0, 0, 1, 0, 0]],
dtype=np.float)
weights = weights / np.sum(weights[:])
y = sp.ndimage.filters.convolve(x, weights, mode='constant')
# Display filtered array
pl.imshow(y, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.savefig("smooth_array.png")
而对应的结果:
希望对您有所帮助。
如何平滑数组的 x[1,3] 和 x[3,2] 元素,
x = np.array([[0,0,0,0,0],[0,0,0,1,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0]])
有两个宽度分别为1和2的二维高斯函数?本质上,我需要一个函数,允许我使用不同宽度的高斯平滑单个 "point like" 数组元素,这样我就得到一个具有平滑变化值的数组。
我对你提出的问题和你发表的评论有点困惑。在我看来,您想使用 scipy.ndimage.filters.gaussian_filter
但我不明白您的意思:
[...] gaussian functions with different sigma values to each pixel. [...]
事实上,由于您使用二维数组 x
,高斯滤波器将有 2 个参数。规则是:每个维度一个 sigma 值,而不是每个像素一个 sigma 值。
这是一个简短的例子:
import matplotlib.pyplot as pl
import numpy as np
import scipy as sp
import scipy.ndimage
n = 200 # widht/height of the array
m = 1000 # number of points
sigma_y = 3.0
sigma_x = 2.0
# Create input array
x = np.zeros((n, n))
i = np.random.choice(range(0, n * n), size=m)
x[i / n, i % n] = 1.0
# Plot input array
pl.imshow(x, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.savefig("array.png")
# Apply gaussian filter
sigma = [sigma_y, sigma_x]
y = sp.ndimage.filters.gaussian_filter(x, sigma, mode='constant')
# Display filtered array
pl.imshow(y, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.title("$\sigma_x = " + str(sigma_x) + "\quad \sigma_y = " + str(sigma_y) + "$")
pl.savefig("smooth_array_" + str(sigma_x) + "_" + str(sigma_y) + ".png")
这是初始数组:
以下是 sigma_x
和 sigma_y
的不同值的一些结果:
这样可以正确考虑 scipy.ndimage.filters.gaussian_filter
的第二个参数的影响。
但是,根据前面的引述,您可能对每个像素的不同权重分配更感兴趣。在这种情况下,scipy.ndimage.filters.convolve 就是您要查找的函数。下面是对应的例子:
import matplotlib.pyplot as pl
import numpy as np
import scipy as sp
import scipy.ndimage
# Arbitrary weights
weights = np.array([[0, 0, 1, 0, 0],
[0, 2, 4, 2, 0],
[1, 4, 8, 4, 1],
[0, 2, 4, 2, 0],
[0, 0, 1, 0, 0]],
dtype=np.float)
weights = weights / np.sum(weights[:])
y = sp.ndimage.filters.convolve(x, weights, mode='constant')
# Display filtered array
pl.imshow(y, cmap='Blues', interpolation='nearest')
pl.xlabel("$x$")
pl.ylabel("$y$")
pl.savefig("smooth_array.png")
而对应的结果:
希望对您有所帮助。