使用内核平滑二维 Numpy 数组

Smoothing a 2-D Numpy Array with a Kernel

假设我有一个只有 0 和 1 的 (m x n) 二维 numpy 数组。我想 "smooth" 数组 运行,例如,数组上的 3x3 内核并在该内核中获取多数值。对于边缘的值,我会忽略 "missing" 值。

例如,假设数组看起来像

import numpy as np

x = np.array([[1, 0, 0, 0, 0, 0, 1, 0],
              [0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 1, 1, 1, 1, 1, 0],
              [0, 0, 1, 1, 0, 1, 1, 0],
              [0, 0, 1, 0, 1, 1, 1, 0],
              [0, 1, 1, 1, 1, 0, 1, 0],
              [0, 0, 1, 1, 1, 1, 1, 0],
              [0, 0, 0, 0, 0, 0, 0, 0]])

从左上角“1”开始,以第一个左上角元素为中心的 3 x 3 内核将缺少第一行和第一列。我想要处理的方式就是忽略它并考虑剩余的 2 x 2 矩阵:

1 0
0 0

在这种情况下,多数值为 0,因此将该元素设置为 0。对所有元素重复此操作,我想要的结果二维数组是:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0
0 0 1 1 1 1 1 0
0 0 1 1 1 1 1 0
0 0 1 1 1 1 1 0
0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0

我该如何完成?

您可以使用 skimage.filters.rank.majority to assign to each value the most occuring one within its neighborhood. The 3x3 kernel can be defined using skimage.morphology.square:

from skimage.filters.rank import majority
from skimage.morphology import square

majority(x.astype('uint8'), square(3))

array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

注意majority 需要最新稳定版 scikit-image。更多here

我最终做了这样的事情(基于 How do I use scipy.ndimage.filters.gereric_filter?):

import scipy.ndimage.filters
import scipy.stats as scs


def filter_most_common_element(a, w_k=np.ones(shape=(3, 3))):
    """
    Creating a function for scipy.ndimage.generic_filter.

    See https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.generic_filter.html for more information
    on generic filters. 

    This filter takes a kernel of np.ones() to find the most common element in the array.
    Based off of 
    """
    a = a.reshape(w_k.shape)
    a = np.multiply(a, w_k)

    # See https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.stats.mode.html
    most_common_element = scs.mode(a, axis=None)[0][0]
    return most_common_element
x = np.array([[1, 0, 0, 0, 0, 0, 1, 0],
              [0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 1, 1, 1, 1, 1, 0],
              [0, 0, 1, 1, 0, 1, 1, 0],
              [0, 0, 1, 0, 1, 1, 1, 0],
              [0, 1, 1, 1, 1, 0, 1, 0],
              [0, 0, 1, 1, 1, 1, 1, 0],
              [0, 0, 0, 0, 0, 0, 0, 0]])

out = scipy.ndimage.filters.generic_filter(x, filter_most_common_element, footprint=np.ones((3,3)),mode='constant',cval=0.0)

out
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]])