我有一个OpenCV程序,可以尽可能快地同时抓取2台摄像机的图像 . 为了使两个摄像头尽可能接近图像,我使用了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;
}
2 回答
你应该能够通过将每个相机移动到自己的线程来加倍你的fps . 要同步摄像机,您可以使用
std::chrono::steady_clock
确保它们同时捕获 .也许是这样的:
单个相机可以获得多少fps?多线程应该是探索的解决方案,以增加你的fps .