如何使用 OpenCV 和 Python 捕获 Raspi 相机图像

首先,为 Python 3 安装 OpenCV:

install-opencv.sh
sudo apt install python3-opencv

以下是获取图像并存储到 image.png 的代码:

cv-raspicapture.py
#!/usr/bin/env python3
import cv2
video_capture = cv2.VideoCapture(0)
# 检查成功
if not video_capture.isOpened():
    raise Exception("Could not open video device")
# 读取图片。成功时 ret === True
ret, frame = video_capture.read()

cv2.imwrite('image.png', frame)
# 关闭设备
video_capture.release()

使用以下命令运行

cv-raspicapture.sh
python3 cv-raspicapture.py

Check out similar posts by category: OpenCV, Python, Raspberry Pi