您的位置:首页 > 其它

双线程读取两路摄像头数据

2016-05-30 17:44 429 查看
利用windows.h中的CreateThread来创建多线程,并基于OpenCV中的VideoCapture实现摄像头读取操作。在此简单记录一下。

#include <windows.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
HANDLE HThread1, HThread2;
cv::Mat g_matFrame1, g_matFrame2;
void thread1()
{
VideoCapture capture;
capture.open(0);
while (1)
{
capture >> g_matFrame1;
}
}
void thread2()
{
VideoCapture capture;
capture.open(1);
while (1)
{
capture >> g_matFrame2;
}
}

int main()
{
HThread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread1, NULL, 0, 0);
HThread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread2, NULL, 0, 0);
int iFrameCount = 0;
while (1)
{
if (g_matFrame1.empty() || g_matFrame2.empty())
{
printf("empty frame!\n");
continue;
}
printf("Frame: %d\n", iFrameCount);
imshow("camera 1:", g_matFrame1);
imshow("camera 2:", g_matFrame2);
waitKey(30);
iFrameCount++;
}
CloseHandle(HThread1);
CloseHandle(HThread2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: