python - 如何获得由PiCam拍摄的图片的RGB值作为矩阵?

标签 python camera raspberry-pi rgb

我正在编写代码,以对Raspberry Pi cam V2拍摄的照片进行一些图像处理。我对图像的兴趣不如对图像的RGB值感兴趣。有没有办法拍照然后创建这些值的矩阵?谢谢!

我曾尝试寻找一些文档,但是它永远无法正常工作。这是我尝试过的其中之一的示例:

import time
import picamera
import numpy as np

with picamera.PiCamera() as camera:
    camera.resolution = (320, 240)
    camera.framerate = 24
    time.sleep(2)
    output = np.empty((240, 320, 3), dtype=np.uint8)
    camera.capture(output, 'rgb')


这是我每次运行此代码时遇到的错误...

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 456, 
in _init_camera
     self._camera = mo.MMALCamera()
   File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 
 2279, in __init__
     super(MMALCamera, self).__init__()
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 633, 
 in __init__
    prefix="Failed to create MMAL component %s" % self.component_type)
  File "/usr/lib/python3/dist-packages/picamera/exc.py", line 184, in 
mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to create MMAL component 
b'vc.ril.camera': Out of memory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/camerasample.py", line 5, in <module>
    with picamera.PiCamera() as camera:
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 431, 
in __init__
    self._init_camera(camera_num, stereo_mode, stereo_decimate)
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 460, 
in _init_camera
    "Camera is not enabled. Try running 'sudo raspi-config' "
picamera.exc.PiCameraError: Camera is not enabled. Try running 'sudo 
raspi-config' and ensure that the camera has been enabled.


我启用了我的相机,所以我不知道该怎么做...(我是这个家伙的新手)

我运行了Raspistill,现在出现此错误。相机已连接,并且在30分钟前即可正常工作。

mmal: Cannot read camera info, keeping the defaults for OV5647 
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' 
(1:ENOMEM)
mmal: mmal_component_create_core: could not create component 
 'vc.ril.camera' (1)
 mmal: Failed to create camera component
 mmal: main: Failed to create camera component
 mmal: Camera is not detected. Please check carefully the camera module is 
 installed correctly

最佳答案

不知道这里是什么问题。如果在ipython下运行您的确切代码,则可以在最后检查output的类型,并查看它是一个Numpy数组:

In [7]: type(output)
Out[7]: numpy.ndarray


我可以检查其形状是否正确:

In [8]: output.shape                                           
Out[8]: (240, 320, 3)


我可以检查平均值是否合理:

In [6]: output.mean()                                          
Out[6]: 99.991701388888885


前几个值对于我的黑暗办公室似乎很明智:

output                             
Out[12]: 
array([[[ 17,  11,  10],
        [ 15,   9,   8],
        [ 18,  14,  10],
        ..., 
        [ 21,  16,  15],
        [ 22,  18,  14],
        [ 25,  21,  17]],
        ...
        ..., 
        [253, 254, 251],
        [253, 254, 251],
        [253, 254, 251]]], dtype=uint8)


本质上,我要说的是output是RGB值的矩阵。因此,在图片的左上角:

R = output[0,0,0]
G = output[0,0,1]
B = output[0,0,2]


并在右下角:

R = output[239,319,0]
G = output[239,319,1]
B = output[239,319,2]


所有红色通道像素的平均值为output[:,:,0].mean(),所有蓝色通道像素的平均值为output[:,:,2].mean()

关于python - 如何获得由PiCam拍摄的图片的RGB值作为矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499604/

相关文章:

python类关键字参数

android - 3枪后相机崩溃

python - 将 getfem++ 导入 conda 环境? - 树莓派 4 - Ubuntu 21.04

python - 如何计算多项式概率值?

python - 打印 "\n"或换行符作为终端输出的一部分

python - Flask 应用程序回溯未显示在服务器日志中

android - 打开手电筒

camera - 人脸检测在 Nexus7 4.2 上不起作用

python - 如何用 Python 发送十六进制

python - 如何在使用 Circuit Python 时使 Raspberry Pico 不自动挂载为 USB 存储器