多数过滤器 Numpy 数组

Majority filter Numpy array

我有一个由 0、1 和 NaN 组成的 numpy ndarray。我想在该数组上使用多数过滤器,这意味着我想设置一个内核 window (例如,3X3 单元格),它将遍历数组并将中心单元格的值更改为在其邻居中出现次数最多的值。这个过滤器应该承受两个约束,它应该忽略 NaN,如果中心单元格的值为 1,那么它应该保持为 1。

这里是我正在寻找的一个小例子: 输入数组:

array([[ 1.,  1.,  1.,  0.,  0.],
       [ 1.,  1., nan,  1.,  1.],
       [nan,  1.,  1.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  1.]])

应用多数过滤器 输出数组:

array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1., nan,  1.,  1.],
       [nan,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  1.,  1.]])

我正在查看 scipy filters but could not find anything adequate. I thought to build a generic convolved filter,但我不确定如何将其用于多数目的。感觉这个应该是quit basic filter,但是我好像找不到。

试试下面的代码:

请注意,由于 numpy.argmax 的行为,当多个索引具有相同的最大值时,结果与您的结果略有不同(您可能想编写自己的 argmax 函数... x=np.argwhere(x==np.max(x))[:,0] 给出所有索引而不是仅第一个)

import numpy as np

def block_fn(x,center_val):

    unique_elements, counts_elements = np.unique(x.ravel(), return_counts=True)

    if np.isnan(center_val):
        return np.nan
    elif center_val == 1:
        return 1.0
    else:
        return unique_elements[np.argmax(counts_elements)]



def majority_filter(x,block_size = (3,3)):

    #Odd block sizes only  ( ? )
    assert(block_size[0]%2 != 0 and block_size[1]%2 !=0)

    yy =int((block_size[0]-1)/2)
    xx =int((block_size[1]-1)/2)


    output= np.zeros_like(x)
    for i in range(0,x.shape[0]):
        miny,maxy = max(0,i-yy),min(x.shape[0]-1,i+yy)

        for j in range(0,x.shape[1]):
            minx,maxx = max(0,j-xx),min(x.shape[1]-1,j+xx)

            #Extract block to take majority filter over
            block=x[miny:maxy+1,minx:maxx+1]

            output[i,j] = block_fn(block,center_val=x[i,j])


    return output


inp=np.array([[ 1.,  1.,  1.,  0.,  0.],
       [ 1.,  1., np.nan,  1.,  1.],
       [np.nan,  1.,  1.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  1.]])


print(majority_filter(inp))

这是一个基于 convolution 的向量化想法。考虑到这些限制,我们似乎只需要编辑 0s 个地方。对于每次滑动 window,获取 1 的计数,然后获取非 NaN 的计数,这决定了决定 1 是否占多数的阈值。如果是,就将那些也是0的地方设置为1。

实现看起来像这样 -

from scipy.signal import convolve2d

def fill0s(a):
    # Mask of NaNs
    nan_mask = np.isnan(a)

    # Convolution kernel
    k = np.ones((3,3),dtype=int)

    # Get count of 1s for each kernel window
    ones_count = convolve2d(np.where(nan_mask,0,a),k,'same')

    # Get count of elements per window and hence non NaNs count
    n_elem = convolve2d(np.ones(a.shape,dtype=int),k,'same')
    nonNaNs_count = n_elem - convolve2d(nan_mask,k,'same')

    # Compare 1s count against half of nonNaNs_count for the first mask.
    # This tells us if 1s are majority among non-NaNs population.
    # Second mask would be of 0s in a. Use Combined mask to set 1s.
    final_mask = (ones_count >= nonNaNs_count/2.0) & (a==0)
    return np.where(final_mask,1,a)

注意,由于我们是用那种1s的内核进行均匀过滤,所以我们也可以使用uniform_filter

样本运行-

In [232]: a
Out[232]: 
array([[ 1.,  1.,  1.,  0.,  0.],
       [ 1.,  1., nan,  1.,  1.],
       [nan,  1.,  1.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  1.]])

In [233]: fill0s(a)
Out[233]: 
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1., nan,  1.,  1.],
       [nan,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  1.,  1.]])