use*_*221 4 filtering numpy scipy python-2.7 ndimage
我正在尝试使用 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)
Run Code Online (Sandbox Code Playgroud)
我从 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)
Run Code Online (Sandbox Code Playgroud)
你有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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2918 次 |
| 最近记录: |