难以在Numpy的第三轴上求和

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

我正在开发一个程序,它截取屏幕并将像素保存在具有3轴(X,Y和RGB值本身)的Numpy数组中,并且无法正确地对最后一个轴求和。

我已经搜索了关于这个主题的信息,虽然我尝试了几个例如“Axis = 2”但我没有取得任何进展。我想远离循环。因为虽然他们工作但我觉得好像首先击败了总和的目的。

#Imports
import numpy as np
from PIL import ImageGrab

#Define Hight and Width of screen
height = 1080
width = 1920


#Capture screen with by taking individual RGB values in an array
screen = np.array(ImageGrab.grab(bbox=(0,0,width,height)))

red = np.sum(screen[[0][0]])
green = np.sum(screen[[1][0]])
blue = np.sum(screen[[2][0]])

print(red,blue,green)

我希望得到结果,红色绿色和蓝色变量分别显示屏幕上所有像素总和的值,但我现在所有这些变量都得到“1468800”。任何帮助表示赞赏,谢谢。

python numpy python-imaging-library rgb numpy-ndarray
1个回答
0
投票

如果我正确理解问题,只需设置axis=2即可。这是一个工作示例:

# sample RGB image to work with
In [24]: from skimage import data
In [25]: astronaut = data.astronaut()
In [26]: astronaut.shape
Out[26]: (512, 512, 3)

# sum the RGB values (R+G+B)
In [30]: astronaut_summed = np.sum(astronaut, axis=2)
In [31]: astronaut_summed.shape
Out[31]: (512, 512)

附:由于我在* nix,我无法检查PIL.ImageGrab的工作,因为它仅适用于MacOS和Windows。

© www.soinside.com 2019 - 2024. All rights reserved.