双线程读取两路摄像头数据
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;
}
#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;
}
相关文章推荐
- Eclipse 上安装 Maven3插件
- SPOJ 7001 VLATTICE【莫比乌斯反演】
- Qt之QStackedLayout
- Spring中bean注入初始化方式—通过注解@Configuration @bean
- windows下安装bpython
- SPOJ 7001 VLATTICE【莫比乌斯反演】
- 当TableView点击的时候,去除点击的阴影
- Qt之QStackedLayout
- CocoaPods版本升级
- JQuery做折叠二级菜单
- hadoop部署注意点
- iOS9新增关键字和一些新特性的学习
- mongo 启动方式
- MySQL常见报错解决方法
- Gradle依赖的统一管理
- 幻灯片自动播放的实现
- Java中文键树的一种实现(附带模糊查询功能)
- LaTeX技巧374:如何在指定列宽的表格里实现文字居中
- 信息安全工具
- Docker暴露2375端口,引起安全漏洞