我正在学习ndimage,但我不知道generic_filter()函数是如何工作的。文档中提到user function将应用于用户定义的封装外形,但不知何故我做不到。下面是一个例子:
>>> import numpy as np
>>> from scipy import ndimage
>>> im = np.ones((20, 20)) * np.arange(20)
>>> footprint = np.array([[0,0,1],
... [0,0,0],
... [1,0,0]])
...
>>> def test(x):
... return x * 0.5
...
>>> res = ndimage.generic_filter(im, test, footprint=footprint)
Traceback (most recent call last):
File "<Engine input>", line 1, in <module>
File "C:\Python27\lib\site-packages\scipy\ndimage\filters.py", line 1142, in generic_filter
cval, origins, extra_arguments, extra_keywords)
TypeError: only length-1 arrays can be converted to Python scalars我期望传递给test()函数的True footprint值是每个数组样本的x footprint相邻元素,所以在本例中,数组的形状为(2,),但我得到了上面的错误。
我做错了什么?
如何告诉泛型过滤器在指定的邻接点上应用简单的值计算?
发布于 2012-12-28 03:18:24
传递给ndimage.generic_filter的函数必须将数组映射到标量。该数组将是一维的,包含由footprint“选择”的im中的值。
对于res中的每个位置,函数返回的值是分配给该位置的值。这就是函数需要返回一个标量的原因。
举个例子,
def test(x):
return (x*0.5).sum()会起作用的。
https://stackoverflow.com/questions/14059529
复制相似问题