【发布时间】:2018-10-03 10:51:37
【问题描述】:
简单的任务..我想用高斯平滑一些向量..这只是一个测试用例,稍后我想将它应用到图像上。
import numpy as np
import scipy.stats
import scipy.ndimage
m = 7 # size of the 'signal'
n = 7 # size of the filter
sgm = 2 # dev for standard distr
weight_conv = np.zeros(2*m*n).reshape(2*n, m) # Weights for the convolution
input_signal = np.array(range(m)) # input signal..
x1 = np.linspace(-4*sgm, 4*sgm, n) # x-values for the normal-dstr
input_filter = scipy.stats.norm.pdf(x1, loc=0, scale=sgm)
# create my own weight matrix
for i in range(weight_conv.shape[1]):
weight_conv[i:(len(input_filter)+i), i] = input_filter
# My own way of calculating the convolution
np.sum(weight_conv * input_signal, axis=1)
# Convolution provided by numpy
np.convolve(input_signal, input_filter)
# Apply the scipy gaussian filter...
scipy.ndimage.filters.gaussian_filter(input_signal, sigma=sgm)
scipy.ndimage.filters.gaussian_filter1d(input_signal, sigma=sgm)
现在我的想法是这些都应该是相似的。我的方法确实产生与 numpy 卷积相似的输出,但 scipy 方法不同......
scipy.ndimage.filters.gaussian_filter(input_signal, sigma=sgm)
array([1, 1, 2, 3, 3, 4, 4])
现在肯定是 scipy 正在做一些不同的事情。但是什么?我不知道。我检查了源代码,似乎他们只是在使用带有高斯核的卷积(这也是我正在做的事情)。但答案并没有加起来......
有人有不同的想法吗?
【问题讨论】: