实现了windows下多线程的图像抓取,并后续进行处理。
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
const int num_thread = 10;
HANDLE hMutex = NULL;
int count = 0;
VideoCapture capture(0);
//DWORD WINAPI Fun( LPVOID lpParameter ) {
// int i = 0;
// while (i<10)
// {
// WaitForSingleObject(hMutex,INFINITE);
// cout << "A Thread is Display" << endl;
// Sleep(100);
// ReleaseMutex(hMutex);
// }
// return 0L;
//}
//
//int test() {
// HANDLE hThread = CreateThread(NULL,0,Fun,NULL,0,NULL);
// hMutex = CreateMutex(NULL,FALSE,"screen");
//
// CloseHandle(hThread);//释放线程资源 非关闭
// for (int i = 0; i < num_thread; i++)
// {
// WaitForSingleObject(hMutex,INFINITE);
// cout << "Main Thread Display" << endl;
// Sleep(100);
// ReleaseMutex(hMutex);
// }
// return 0;
//}
DWORD WINAPI pull_pic (LPVOID lpParameter) {
Mat frame1;
while (1)
{
//WaitForSingleObject(hMutex, INFINITE);
capture >> frame1;
putText(frame1,"test", Point (0,100), FONT_HERSHEY_COMPLEX,1, Scalar(255,0,0),2);
imshow("test",frame1);
waitKey(30);
cout << "test" << endl;
//putText(Mat& img, const string& text, Point org,
// int fontFace, double fontScale, Scalar color,
// int thickness = 1, int lineType = 8,
// bool bottomLeftOrigin = false);
//ReleaseMutex(hMutex);
}
return 0L;
}
int main()
{
HANDLE hThread0 = CreateThread(NULL, 0, pull_pic, NULL, 0, NULL);
//hMutex = CreateMutex(NULL, FALSE, "screen");
CloseHandle(hThread0);//释放线程资源 非关闭
HANDLE hThread1 = CreateThread(NULL, 0, pull_pic, NULL, 0, NULL);
//hMutex = CreateMutex(NULL, FALSE, "screen");
CloseHandle(hThread1);//释放线程资源 非关闭
HANDLE hThread = CreateThread(NULL, 0, pull_pic, NULL, 0, NULL);
//hMutex = CreateMutex(NULL, FALSE, "screen");
CloseHandle(hThread1);//释放线程资源 非关闭
HANDLE hThread2 = CreateThread(NULL, 0, pull_pic, NULL, 0, NULL);
//hMutex = CreateMutex(NULL, FALSE, "screen");
CloseHandle(hThread2);//释放线程资源 非关闭
HANDLE hThread3 = CreateThread(NULL, 0, pull_pic, NULL, 0, NULL);
//hMutex = CreateMutex(NULL, FALSE, "screen");
CloseHandle(hThread3);//释放线程资源 非关闭
Mat frame; //定义一个Mat变量,用于存储每一帧的图像
for (;;)
{
capture >> frame; //读取当前帧
imshow("test", frame); //若当前帧捕捉成功,显示
waitKey(30); //延时30毫秒
cout << "main" << endl;
}
}
程图像的抓取,并可以进行相应的处理。