首页 > 解决方案 > 使用 OpenCV C++ RaspberryPi 的慢速 FPS

问题描述

我使用 OpenCV 编写了一个简单的 C++ 程序来测试树莓派上的 fps。

#include <iostream>
#include <chrono>
#include <thread>
#include <unistd.h>
#include <string>

#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>

using namespace cv;
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
using namespace cv::ml;

int main(void) 
{
    VideoCapture camera(0); 
    camera.set(CAP_PROP_FRAME_WIDTH, 640);
    camera.set(CAP_PROP_FRAME_HEIGHT, 480);
    camera.set(CAP_PROP_FPS, 25);
    
    Mat frame;
    while (camera.read(frame)) {
        for (size_t i = 0; i < frame.dataend - frame.datastart; i++)
            std::cout << frame.data[i];
    }
}

然后我使用以下脚本来测试 fps。第一个使用 raspvid 作为输入,第二个使用我的 C++ 程序作为输入 raspvid 版本达到 25fps。我的 C++ 程序从不超过 10fps。知道这是为什么吗?

raspivid -w 640 -h 480 -fps 25 -t 120000 -o -| ffmpeg -re -i pipe:0 -y -an -c:v copy -f null /dev/null

./PiImageAnalyzer.out | ffmpeg -re -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 25 -i pipe:0 -y -an -c:v copy -f null /dev/null

更新

这些脚本也可以使用

raspivid -w 640 -h 480 -fps 25 -t 120000 -o -| ffplay -i -

./PiImageAnalyzer.out | ffplay -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 25 -i -

标签: c++opencvffmpegraspberry-piframe-rate

解决方案


推荐阅读