0

我第一次尝试使用 scipy.ndimage.generic_filter1d ,但进展不顺利。这就是我正在尝试的

import numpy as np
from scipy.ndimage import generic_filter1d

def test(x):
    return x.sum()

im_cube = np.zeros((100,100,100))
calc = generic_filter1d(im_cube, test, filter_size=5, axis=2)

但我得到这个错误:

TypeError: test() takes 1 positional argument but 2 were given

我正在使用 scipy 1.4.1 我做错了什么?

对于该功能,我也尝试了 np.mean 但后来我得到了这个:

TypeError: only integer scalar arrays can be converted to a scalar index
4

1 回答 1

1

根据文档,回调接受两个参数:对输入行的引用和对输出行的引用。它不返回输出,而是就地修改提供的缓冲区。

如果要实现滚动和过滤器,则需要手动执行此操作。例如:

def test(x, out)
    out[:] = np.lib.stride_tricks.as_strided(x, strides=x.strides * 2, shape=(5, x.size - 4)).sum(axis=0)

为了使它成为一个平均值,/ 5请在末尾添加。

的目的genetic_filter1d是格式化边缘并运行外循环。它实际上并没有为您实现滚动过滤器。您仍然需要自己实现整个内循环。

于 2020-06-13T16:32:40.090 回答