【发布时间】:2016-01-05 22:51:54
【问题描述】:
我想使用generic filter 来计算给定窗口(或内核)内的值的平均值,以获得满足几个条件的值。我希望下面的代码能够在 3 层窗口中生成第一个数组的均值滤波器,并使用其他两个数组来屏蔽均值计算中的值。
from scipy import ndimage
import numpy as np
#some test data
tstArr = np.random.rand(3,7,7)
tstArr = tstArr*10
tstArr = np.int_(tstArr)
tstArr[1] = tstArr[1]*100
tstArr[2] = tstArr[2] *1000
#mean function
def testFun(tstData,processLayer,nLayers,kernelSize):
funData= tstData.reshape((nLayers,kernelSize,kernelSize))
meanLayer = funData[processLayer]
maskedData = meanLayer[(funData[1]>1)&(funData[2]<9000)]
returnMean = np.mean(maskedData)
return returnMean
#number of layers in the array
nLayers = np.shape(tstArr)[0]
#window size
kernelSize = 5
#create a sampling window of 5x5 elements from each array
footprnt = np.ones((nLayers,kernelSize,kernelSize),dtype = np.int)
# calculate the mean of the first layer in the array (other two are for masking)
processLayer = 0
tstOut = ndimage.generic_filter(tstArr, testFun, footprint=footprnt, extra_arguments = (processLayer,nLayers,kernelSize))
我认为这会从输入数组的第一层产生一个 7x7 的掩码平均值数组。输出是一个 3x7x7 数组,我不明白这些值代表什么。我不确定如何生成“掩码”均值过滤数组,或者如何将输出解释为给定。
【问题讨论】:
标签: python numpy multidimensional-array scipy filtering