C++ 如何增加fps opencv多摄像头设置

C++ 如何增加fps opencv多摄像头设置,c++,opencv,C++,Opencv,我有一个OpenCV程序,它可以尽可能快地同时从两个摄像头抓取图像。为了让两台相机尽可能近距离拍摄图像,我使用了grab()和retrieve()函数。我现在得到的帧速率非常慢(大约6 fps)。我想增加这个fps。有人知道如何提高fps吗 我在想也许可以把每个摄像头放在不同的线程上,或者以某种方式利用多线程,但我担心它们不会以这种方式同时捕获图像 void takeImages(VideoCapture& cap, VideoCapture& cap1, int imageco

我有一个OpenCV程序,它可以尽可能快地同时从两个摄像头抓取图像。为了让两台相机尽可能近距离拍摄图像,我使用了grab()和retrieve()函数。我现在得到的帧速率非常慢(大约6 fps)。我想增加这个fps。有人知道如何提高fps吗

我在想也许可以把每个摄像头放在不同的线程上,或者以某种方式利用多线程,但我担心它们不会以这种方式同时捕获图像

void takeImages(VideoCapture& cap, VideoCapture& cap1, int imagecount){


    if(!cap.isOpened()|| !cap1.isOpened()) {  // check if we succeeded
        cout << "Can not open cameras." << endl;
        return;
    }


    Mat frame, frame2;
    for(int x = 0;x < imagecount; x++)
    {
        // Capture image from both camera at the same time (important).
        if( !cap.grab() || !cap1.grab() )
        {

            cout << "Can not grab images." << endl;
            return;
        }


        if( cap.retrieve(frame,3) || cap1.retrieve(frame,3)){

            //process images here..

        } else {
            cout << "Can not retrieve images." << endl;
            return;
        }

        printf("x = %d\n", x);
    }

    waitKey(0);
}



int main(int, char**)
{

    // trying to set higher program priority to increase fps here
    cout << "Nice = " << nice(21) << endl;

    VideoCapture cap(0); // open the default camera
    VideoCapture cap1(1);


    namedWindow("cam1",1);
    namedWindow("cam2",1);

    std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
    //Get the time it takes to get 200 frames from BOTH cameras.
    takeImages(cap,cap1, 200);

    std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();
    std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::seconds>(end - begin).count() <<std::endl;

    return 0;
}
void takeImages(VideoCapture&cap、VideoCapture&cap1、int imagecount){
如果(!cap.isOpened()| |!cap1.isOpened()){//检查我们是否成功

cout通过将每个摄像头移动到自己的线程,您应该能够将fps翻倍。要同步摄像头,您可以使用
std::chrono::staid_clock
确保它们同时捕获

也许是这样的:

#include <thread>

using steady_clock = std::chrono::steady_clock;

void takeImages(VideoCapture& cap, int imagecount,
    steady_clock::time_point next_frame, unsigned fps)
{
    assert(fps > 0);
    assert(fps <= 1000);

    Mat frame;
    for(int x = 0; x  < imagecount; x++)
    {
        // Capture image from both camera at the same time (important).
        std::this_thread::sleep_until(next_frame);
        next_frame += std::chrono::milliseconds(1000 / fps);

        if(!cap.grab())
            throw std::runtime_error("Can not grab image.");

        if(!cap.retrieve(frame, 3))
            throw std::runtime_error("Can not retrieve image.");

        // process image here

    }

    waitKey(0);
}

int main(int, char**)
{
    try
    {
        // trying to set higher program priority to increase fps here
        // cout << "Nice = " << nice(21) << endl;

        VideoCapture cap0(0); // open the default camera
        VideoCapture cap1(1 + CAP_V4L2);

        if(!cap0.isOpened() || !cap1.isOpened())
        {  // check if we succeeded
            cout << "Can not open cameras." << endl;
            return EXIT_FAILURE;
        }

//      namedWindow("cam1", WINDOW_AUTOSIZE);
//      namedWindow("cam2", WINDOW_AUTOSIZE);

        std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

        // pass the same time point to both threads to synchronize their
        // frame start times
        steady_clock::time_point next_frame = steady_clock::now() + std::chrono::milliseconds(50);

        std::thread t1{takeImages, std::ref(cap0), 200, next_frame, 10};
        std::thread t2{takeImages, std::ref(cap1), 200, next_frame, 10};

        t1.join();
        t2.join();

        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
        std::cout << "Time difference = "
            << std::chrono::duration_cast<std::chrono::seconds>(end - begin).count()
            << std::endl;
    }
    catch(std::exception const& e)
    {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
#包括
使用稳定时钟=标准::时钟::稳定时钟;
void takeImages(视频捕获和cap、int imagecount、,
稳定时钟::时间点(下一帧,无符号fps)
{
断言(fps>0);

断言(fps通过将每个摄像头移动到其自己的线程,您应该能够将fps翻倍。要同步摄像头,您可以使用
std::chrono::stable_clock
确保它们同时捕获

也许是这样的:

#include <thread>

using steady_clock = std::chrono::steady_clock;

void takeImages(VideoCapture& cap, int imagecount,
    steady_clock::time_point next_frame, unsigned fps)
{
    assert(fps > 0);
    assert(fps <= 1000);

    Mat frame;
    for(int x = 0; x  < imagecount; x++)
    {
        // Capture image from both camera at the same time (important).
        std::this_thread::sleep_until(next_frame);
        next_frame += std::chrono::milliseconds(1000 / fps);

        if(!cap.grab())
            throw std::runtime_error("Can not grab image.");

        if(!cap.retrieve(frame, 3))
            throw std::runtime_error("Can not retrieve image.");

        // process image here

    }

    waitKey(0);
}

int main(int, char**)
{
    try
    {
        // trying to set higher program priority to increase fps here
        // cout << "Nice = " << nice(21) << endl;

        VideoCapture cap0(0); // open the default camera
        VideoCapture cap1(1 + CAP_V4L2);

        if(!cap0.isOpened() || !cap1.isOpened())
        {  // check if we succeeded
            cout << "Can not open cameras." << endl;
            return EXIT_FAILURE;
        }

//      namedWindow("cam1", WINDOW_AUTOSIZE);
//      namedWindow("cam2", WINDOW_AUTOSIZE);

        std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

        // pass the same time point to both threads to synchronize their
        // frame start times
        steady_clock::time_point next_frame = steady_clock::now() + std::chrono::milliseconds(50);

        std::thread t1{takeImages, std::ref(cap0), 200, next_frame, 10};
        std::thread t2{takeImages, std::ref(cap1), 200, next_frame, 10};

        t1.join();
        t2.join();

        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
        std::cout << "Time difference = "
            << std::chrono::duration_cast<std::chrono::seconds>(end - begin).count()
            << std::endl;
    }
    catch(std::exception const& e)
    {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
#包括
使用稳定时钟=标准::时钟::稳定时钟;
void takeImages(视频捕获和cap、int imagecount、,
稳定时钟::时间点(下一帧,无符号fps)
{
断言(fps>0);

断言(fps单台相机可以获得多少fps?多线程应该是探索的解决方案,以提高fps。

单台相机可以获得多少fps?多线程应该是探索的解决方案,以提高fps。

我必须澄清,我的fps很慢,因为没有任何处理,而且正在运行我发布的代码。这种情况下会有帮助吗?@bakalolo因为
cap.retrieve()
被认为是速度较慢的部分。以前的方法可能提供了一些好处。但是我发布了一个新方法,该方法应该更有效,因为它不会在每次迭代中启动新线程。似乎waitkey阻止了我的代码,当我删除它时,它工作得很好。我必须澄清,我的fps很慢,因为没有还没有处理,只是在运行我发布的代码。这种情况下会有帮助吗?@bakalolo,因为
cap.retrieve()
应该是速度较慢的部分,以前的方法可能提供了一些好处。但是我发布了一个新方法,该方法应该更有效,因为它不会在每次迭代中启动新线程。似乎waitkey阻塞了我的代码,当我删除它时,它工作得很好。。