python – 在scipy.ndimage.filters.generic_filter中获取当前元
发布时间:2020-12-16 21:39:26 所属栏目:Python 来源:网络整理
导读:是否有可能在scipy.ndimage.filters.generic_filter的过滤函数中获
|
是否有可能在scipy.ndimage.filters.generic_filter的过滤函数中获取“当前元素”?
例如,如果A [0]总是包含当前元素(似乎不是这种情况),则以下内容可能会找到局部最大值 def local_max_f(A) : return A[0] == A.max() img = np.random.rand(100).reshape(10,10) ndimage.generic_filter( img,local_max_f,size=3 ) 解决方法
当前元素应该是大小中心的元素(或者示例中的A [1]),但是这不能依赖于输入数组的边缘,并且取决于处理数组边界的模式.
docs instead provide a neat example(适用于您的情况)用于确定过滤器中的当前位置,使用类来维持迭代之间的某些状态.这总是先遍历最后一个维度;所有你应该做的就是用你的过滤函数实际需要做的事情来替换结果行,在你的情况下,这将类似于result = self._array [self.coordinates] == buffer.max(). a = arange(12).reshape(3,4)
class fnc_class:
def __init__(self,_array):
# store the shape:
self.shape = _array.shape
self._array = _array
# initialize the coordinates:
self.coordinates = [0] * len(self.shape)
def filter(self,buffer):
result = self._array[tuple(self.coordinates)] == buffer.max()
print self.coordinates
# calculate the next coordinates:
axes = range(len(self.shape))
axes.reverse()
for jj in axes:
if self.coordinates[jj] < self.shape[jj] - 1:
self.coordinates[jj] += 1
break
else:
self.coordinates[jj] = 0
return result
fnc = fnc_class(a)
generic_filter(a,fnc.filter,footprint = [[1,0],[0,1]])
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
