如何使用 picamera2 捕获 Raspberry Pi HQ 摄像头 (IMX477) 高分辨率图像
以下代码将从使用 IMX477 传感器的 Raspberry Pi HQ 摄像头捕获单个 4056x3056px 图像到文件或 numpy 数组。这是该摄像头支持的最大分辨率。
捕获到文件
这将捕获单个帧到 CameraTest.png。PNG 文件会相当大,约 15-25 兆字节。
capture_camera_test.py
#!/usr/bin/env python3
import time
import picamera2
import numpy as np
with picamera2.Picamera2() as camera:
# Create high resolution still capture config
camera_config = camera.create_still_configuration({"size":(4056, 3040)})
camera.configure(camera_config)
# Start camera with config
camera.start()
# Capture a single frame to CameraTest.png
camera.capture_file("CameraTest.png")捕获到 numpy 数组
capture_to_numpy_array.py
#!/usr/bin/env python3
import time
import picamera2
import numpy as np
with picamera2.Picamera2() as camera:
# Create high resolution still capture config
camera_config = camera.create_still_configuration({"size":(4056, 3040)})
camera.configure(camera_config)
# Start capture
camera.start()
array = camera.capture_array("main")
# TODO Do something with [array]
print(array.shape)示例输出:
picamera2_capture_example_output.txt
[0:55:52.878964095] [5768] INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:55:52.913906171] [5769] INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/imx477@1a to Unicam device /dev/media3 and ISP device /dev/media0
[0:55:52.913998855] [5769] INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:55:52.921056014] [5768] INFO Camera camera.cpp:1033 configuring streams: (0) 4056x3040-BGR888 (1) 4056x3040-SBGGR12_CSI2P
[0:55:52.922090144] [5769] INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 4056x3040-SBGGR12_1X12 - Selected unicam format: 4056x3040-pBCC
(3040, 4056, 3)注意 NumPy 数组与 Python OpenCV 函数兼容。
Check out similar posts by category:
Python, Raspberry Pi
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow