由于树莓派5 os系统升级,正常libcamera创建对象每次失败。
改如下方法成功。
1 创建管道
rpicam-vid -t 0 --codec mjpeg -o udp://127.0.0.1:8554 > /dev/null 2>&1
2 opencv从管道里读取
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
// 通过 UDP 读取视频流
//cv::VideoCapture cap("udp://127.0.0.1:8554", cv::CAP_FFMPEG);
cv::VideoCapture cap("udp://127.0.0.1:8554", cv::CAP_FFMPEG);
if (!cap.isOpened()) {
std::cerr << "无法打开摄像头视频流!" << std::endl;
return -1;
}
cv::Mat frame;
while (true) {
cap >> frame;
if (frame.empty()) break;
cv::imshow("Camera Stream", frame);
if (cv::waitKey(1) == 27) break; // 按 ESC 退出
}
cap.release();
cv::destroyAllWindows();
return 0;
}
编译
g++ camera_capture.cpp -o camera_capture pkg-config --cflags --libs opencv4
运行
sudo ./camera_capture
正常