首页 > 解决方案 > 使用opencv c ++(多线程)进行多摄像头捕获,但速度与串行速度相同

问题描述

我有五个 RTSP 流要读取,图像大小为 2160x3840。为了提高速度,我使用了多线程来提高图像读取的速度,但是我发现速度和串口是一样的。这是我的测试代码。感谢你的回复!这个问题困扰了我好几天。再次感谢您的回答!</p>

#if 1
#include <iostream>
#include <thread>
#include <vector>
#include <opencv2/opencv.hpp>
#include <time.h>
#include <unistd.h>

using namespace std;
using namespace cv;

#define LOGLN(msg) std::cout << msg << std::endl

//void detect(Mat img, String strCamera);
void stream(String strCamera, int i);


int main()
{
    thread cam1(stream, "../video/11.mp4", 1);
    thread cam2(stream, "../video/21.mp4", 2);
    thread cam3(stream, "../video/31.mp4", 3);
    thread cam4(stream, "../video/41.mp4", 4);
    thread cam5(stream, "../video/51.mp4", 5);
//  thread cam5(stream, "rtsp://ip:host/video5", 5);

    LOGLN("1111111111");
    cam1.join();
    cam2.join();
    cam3.join();
    cam4.join();
    cam5.join();
    LOGLN("22222222");
    return 0;
}

void stream(String strCamera, int i) {
    VideoCapture cap(strCamera);
    if (cap.isOpened()) {
        while (true) {
            clock_t t0 = clock();
            Mat frame;
            cap >> frame;
            LOGLN("decode video "<< i << " cpu : " << (clock()-t0) /1e6 << " sec");
        }
    }
//    while(1)
//    {
//    int b=0;
//    for (int i=0; i< 5000; i++)
//    {
//    b += i;
//    }
//    LOGLN("decode video "<< i << " sec");
//    sleep(2);
//    }
}
#endif


#if 0
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
//#include <opencv2/core/core.hpp>
#include <thread>
#include <time.h>
#define LOGLN(msg) std::cout << msg << std::endl

using namespace std;
using namespace cv;


int main()
{
    VideoCapture cap1;
    VideoCapture cap2;
    VideoCapture cap3;
    VideoCapture cap4;
    VideoCapture cap5;


    //cap1.open(0);
    cap1.open("../video/11.mp4");
    cap2.open("../video/21.mp4");
    cap3.open("../video/31.mp4");
    cap4.open("../video/41.mp4");
    cap5.open("../video/51.mp4");


    /*
    cap1.set(CAP_PROP_FRAME_WIDTH, 640);
    cap1.set(CAP_PROP_FRAME_HEIGHT, 480);
    cap2.set(CAP_PROP_FRAME_WIDTH, 640);
    cap2.set(CAP_PROP_FRAME_HEIGHT, 480);
    cap3.set(CAP_PROP_FRAME_WIDTH, 640);
    cap3.set(CAP_PROP_FRAME_HEIGHT, 480);
    cap4.set(CAP_PROP_FRAME_WIDTH, 640);
    cap4.set(CAP_PROP_FRAME_HEIGHT, 480);
    */


    if (!cap1.isOpened() || !cap2.isOpened()) { //|| !cap3.isOpened() || !cap4.isOpened()) {
        cout << "sorry try again" << endl;
        return -1;
    }

    Mat frame1, frame2, frame3, frame4, frame5;// , frame3, frame4;

    while (true)
    {
        clock_t t0 = clock();
        bool s1 = cap1.read(frame1);
        bool s2 = cap2.read(frame2);
        bool s3 = cap3.read(frame3);
        bool s4 = cap4.read(frame4);
        bool s5 = cap5.read(frame5);
        LOGLN("decode video "<< 1 << " cpu : " << (clock()-t0) /1e6 << " sec");


//      if (!s1 || !s2) {
//          cout << "sorry I can't read" << endl;
//          break;
//      }
    }
    return 0;
}
#endif

标签: c++multithreadingopencv

解决方案


推荐阅读