I have five RTSP streams to read, and the image size is 2160x3840. In order to improve the speed, I use multithreading to improve the speed of image reading, but I find that the speed is the same as that of serial. Here is my test code. Thank you for your reply! This question has bothered me for a few days. Thank you again for your answer!
#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