【问题标题】:calculate average value of area around center pixel in python [closed]在python中计算中心像素周围区域的平均值[关闭]
【发布时间】:2015-10-21 06:41:25
【问题描述】:

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

【问题讨论】:

标签: python filter average mask


【解决方案1】:

尝试以下代码(阈值设置为 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]])
>>> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多