使用OpenCV读取树莓派摄像头帧时全黑问题求助
树莓派OpenCV调用摄像头捕获全黑帧问题
我尝试使用OpenCV从树莓派摄像头模块捕获图像,代码运行无报错,但获取的帧完全是黑色。使用rpicam-still -o Desktop/image.jpg命令可以正常拍摄静态照片,但运行网上找到的如下通用OpenCV代码时,仅能得到全黑帧:
import numpy as np import cv2 as cv cap = cv.VideoCapture(-1) if not cap.isOpened(): print("Cannot open camera") exit() while True: ret, frame = cap.read() print(frame.max(), frame.mean()) if not ret: print("Can't receive frame (stream end?). Exiting ...") break print(f"Max Pixel Value: {frame.max()}, Mean Pixel Value: {frame.mean()}") print(f"Frame Shape: {frame.shape}, Frame Data Type: {frame.dtype}") cv.imshow('frame', frame) cv.imwrite('frame.png', frame) if cv.waitKey(1) == ord('q'): break cap.release() cv.destroyAllWindows()
程序输出如下:
Max Pixel Value: 0, Mean Pixel Value: 0.0 Frame Shape: (480, 640, 3), Frame Data Type: uint8 0.0.0
捕获到的帧为全黑图像。
- 更新:按照建议修改代码后,结果仍为全黑帧,此前尝试设置曝光时间也无改善,若需再次尝试或补充信息请告知。
- 更新2:多次修改代码后问题依旧,怀疑是系统层面问题,OpenCV无法直接识别树莓派摄像头;尝试Picamera代码也出现类似问题,猜测可能与树莓派3的图像处理能力有关。
运行环境
- Thonny IDE
- 树莓派3(64位Raspberry Pi OS)
- RaspiCam模块
已确认OpenCV安装正常,版本信息如下:
$ pip show opencv-python Name: opencv-python Version: 4.10.0.84 Summary: Wrapper package for OpenCV python bindings. Home-page: https://github.com/opencv/opencv-python Author: Author-email: License: Apache 2.0 $ python3 --version Python 3.11.2
(这是我首次发帖,非英语母语,如有表述疑问请提出。)
内容的提问来源于stack exchange,提问作者Lucas Flores Hinrichsen
相关产品推荐
相关产品推荐

