计算 python 中中心像素周围区域的平均值

calculate average value of area around center pixel in python

我有一个非常大的二维数组,想过滤掉一些小特征。因此,我想计算中心像素周围区域的平均值。如果此值低于某个阈值,则在掩码中将中心像素的值设置为零。是否有现成的 python 功能?为简单起见,假设输入是一个包含整数项的 5×9 数组,并且应该在阈值为 7 的 2×2 掩码上求平均值。

尝试以下代码(阈值设置为 7,平均值设置为 2×2 区域):

Python 3.5.0 (default, Sep 27 2015, 12:06:50) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from scipy import signal
>>> a = np.array([[2,3,2,3,5,7,3,6,1],[2,3,2,7,3,1,3,6,2],[345,1345,45,2,6,1,0,1,1],[2,3,2,456,5,7,3,1,1],[2,789,2,8,5,7,3,6,1]])
>>> b = np.array([[0.25,0.25],[0.25,0.25]])
>>> c = signal.convolve2d(a,b,boundary='symm',mode='same')
>>> threshold = 7.
>>> a[c<np.ones([5,9])*threshold]=0
>>> a
array([[   0,    0,    0,    0,    0,    0,    0,    0,    0],
   [   0,    0,    0,    0,    0,    0,    0,    0,    0],
   [ 345, 1345,   45,    2,    0,    0,    0,    0,    0],
   [   2,    3,    2,  456,    5,    0,    0,    0,    0],
   [   0,  789,    2,    8,    5,    0,    0,    0,    0]])
>>>