4

我正在尝试使用 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:需要浮点数”,但我不知道它指的是什么参数,它看起来与我见过的其他示例基本相同。

4

1 回答 1

5

这对我有用。代码有几个小问题:

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)
于 2014-06-25T17:03:21.293 回答