我正在尝试使用 scipy.ndimage.filters.generic_filter 来计算邻域的加权总和。社区在某些时候会发生变化,但现在 3x3 是我正在努力的方向。
到目前为止,这是我的位置:
def Func(a):
a = np.reshape((3,3))
weights = np.array([[0.5,.05,0.5],[0.5,1,0.5],[0.5,0.5,0.5]])
a = np.multiply(a,weights)
a = np.sum(a)
return a
ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)
我从 ndimage 收到一条错误消息,说“TypeError: a float is required”,但我不知道它指的是什么参数,它看起来与我见过的其他示例基本相同。
最佳答案
这对我有用。代码有几个小问题:
import scipy.ndimage.filters
import numpy as np
Array = rand( 100,100 )
def Func(a):
a = a.reshape((3,3))
weights = np.array([[0.5,.05,0.5],[0.5,1,0.5],[0.5,0.5,0.5]])
a = np.multiply(a,weights)
a = np.sum(a)
return a
out = scipy.ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)
您的
a = np.reshape( (3,3) )
不正确。那是你要的吗?[更新]
根据我们的讨论稍微清理一下:
import scipy.ndimage.filters
import numpy as np
Array = rand( 100,100 )
def Func(a):
return np.sum( a * r_[0.5,.05,0.5, 0.5,1,0.5, 0.5,0.5,0.5] )
out = scipy.ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)
关于python-2.7 - 如何使用 scipy.ndimage.filters.gereric_filter?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24413843/