如果元素是元组,则 numpy 查找均值

numpy find mean if element is tuple

我有 3 通道 cv2 框架,众所周知是 ndarray。所以数组的形状是 (640, 480, 3)。我想计算整个帧的每个颜色通道的平均值。 numpy 中是否有方便的单字符串方式来做到这一点?

您可以使用 numpy 的内置均值函数并指定要平均的轴:

import numpy as np

img_arr = np.random.randn(640, 480, 3)
print(img_arr.shape)
# (640, 480, 3)

means = np.mean(img_arr, axis=(0, 1))
print(means.shape)
# (3,)