如何获取边界框区域内热图的平均值

问题描述 投票:1回答:1

我正在尝试计算图像边界框(汽车上的紫色遮罩)区域的热图的平均值。这两个对象都是numpy数组,但我很难找到一个合适的函数来计算掩码内的平均热图值。

enter image description here

我附加了一个最小的工作示例,它复制了我正在尝试实现的内容:

import numpy as np
mask = np.zeros((10,10))
mask[3:-3, 3:-3] = 1 # white square in black background
im = mask + np.random.randn(10,10) # random image
masked = np.ma.masked_where(mask == 0, mask)
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(im, 'BuGn')
plt.imshow(masked, 'jet',  alpha=0.9)
plt.show()

我想在蓝色方块(im)内计算masked的平均值。

enter image description here

有人有什么建议吗?

python numpy
1个回答
1
投票

我想你正在寻找这个:

#!/usr/bin/env python3

import numpy as np

# Make mask of first 4 diagonal elements
h, w = 6, 10
mask = np.zeros((h,w),dtype=np.uint8)
mask[0][0] = 1
mask[1][1] = 1
mask[2][2] = 1
mask[3][3] = 1

# Make image of random small numbers under 10 that Mark is capable of adding up
# Go for predictable randomness because Mark is not a physicist
np.random.seed(42)
im = np.random.randint(1,10,(h,w),dtype=np.uint8)

# Calculate mean of numbers where mask>0
mean = im[mask>0].mean()

以下是面具的外观:

array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 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, 0, 0]], dtype=uint8)

以下是图像的外观:

enter image description here

这是平均值:

In [33]: mean                                                                                       
Out[33]: 5.75

校验:

(7 + 5 + 3 + 8) /4 = 23/4 = 5.75
© www.soinside.com 2019 - 2024. All rights reserved.