您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 在scipy.ndimage.filters.generic_filter中获取当前元素

5b51 2022/1/14 8:20:28 python 字数 2326 阅读 505 来源 www.jb51.cc/python

是否有可能在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.

概述

例如,如果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 )

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]])

总结

以上是编程之家为你收集整理的python – 在scipy.ndimage.filters.generic_filter中获取当前元素全部内容,希望文章能够帮你解决python – 在scipy.ndimage.filters.generic_filter中获取当前元素所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶