简体   繁体   English

使用遮罩从2D数组中获取numpy元素的平均值

[英]get mean of numpy elements from 2D array using mask

I want to get the average value of numpy elements in a 2d array around a selected point with a selected neighborhood size and shape. 我想获得具有选定邻域大小和形状的选定点周围的二维数组中的numpy元素的平均值。

I made an example to help explain what I'm trying to do. 我举了一个例子来帮助解释我要做什么。 It doesn't work with most inputs yet because of shape issues. 由于形状问题,它不适用于大多数输入。 Before I go on, is there an elegant or built in way of doing this? 在我继续之前,是否有一种优雅或内在的方式来做到这一点?

import numpy as np
from skimage.morphology import disk

def get_sub_array_about_point(array, point, size):
    i1 = point[0]-(size-1)
    i2 = point[0]+size
    j1 = point[1]-(size-1)
    j2 = point[1]+size
    return array[i1:i2, j1:j2]

def get_neighborhood_mean(array, point, size, shape):

    sub_array = get_sub_array_about_point(test_array, point, size)
    """
    array([[10.1,  1.2,  1.3],
           [ 2.1, 20.2,  2.3],
           [ 3.1,  3.2,  3.3]])
    """

    masked_sub_array = np.ma.masked_where(mask==False, sub_array)
    masked_sub_array
    """
    masked_array(
      data=[[--, 1.2, --],
            [2.1, 20.2, 2.3],
            [--, 3.2, --]],
      mask=[[ True, False,  True],
            [False, False, False],
            [ True, False,  True]],
      fill_value=1e+20)
    """

    return masked_sub_array.mean()
    """
    5.8
    """

test_array = np.array([[0. , 0.1 , 0.2 , 0.3 ],
                       [1. , 10.1 , 1.2, 1.3 ],
                       [2. , 2.1, 20.2, 2.3 ],
                       [3. , 3.1 , 3.2, 3.3 ]])

mask = disk(1)
"""
array([[0, 1, 0],
       [1, 1, 1],
       [0, 1, 0]], dtype=uint8)
"""
get_neighborhood_mean(test_array, point=(2,2), size=2, shape=mask)

An elegant way of doing this is with a 2D convolution . 做到这一点的一种优雅方法是使用2D卷积 If you normalize the elements in mask (by dividing by the sum of the elements), the convolution will give you a 2D array that's the average of the neighborhood. 如果对mask中的元素进行归一化(除以元素之和),则卷积将为您提供2D数组,该数组是邻域的平均值。

from scipy.signal import convolve2d

test_array = np.array([[0. , 0.1 , 0.2 , 0.3 ],
                       [1. , 10.1 , 1.2, 1.3 ],
                       [2. , 2.1, 20.2, 2.3 ],
                       [3. , 3.1 , 3.2, 3.3 ]])

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

# Normalize mask
mask = mask / float(np.sum(mask))

convolve2d(test_array, mask, mode='valid')
# array([[2.9, 6.6],
#        [7.5, 5.8]])

By using mode='valid' , the average values are only given for elements that don't require padding, where the mask can fit without having to extend beyond the edge of the array. 通过使用mode='valid' ,仅对不需要填充的元素提供平均值,可以在其中填充遮罩而不必扩展到数组边缘之外。 So in the above case, the output array is for the center 4 elements of test_array only. 因此,在上述情况下,输出数组仅用于test_array的中心4个元素。

If you want to get the averages of the neighborhoods for all elements, including at the edges, zero-padding can be used by setting mode='same' . 如果要获取所有元素(包括边缘)的邻域平均值,则可以通过设置mode='same'来使用零填充。

convolve2d(test_array, mask, mode='same')
# array([[0.22, 2.08, 0.36, 0.36],
#        [2.62, 2.9 , 6.6 , 1.02],
#        [1.62, 7.5 , 5.8 , 5.42],
#        [1.62, 2.28, 5.96, 1.76]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM