测试小工具1:同时打开多个相机(多线程)

    当电脑与多台相机设备相连接时,需要打开多个相机进行图像信息的采集。下面使用两个线程打开两个摄像头。

#include 
#include 
#include 
#include

using namespace cv;
using namespace std;

DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter);



int main()
{
	HANDLE handle1 = CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
	HANDLE handle2 = CreateThread(NULL, 0, ThreadProc2, NULL, 0, NULL);



	CloseHandle(handle1);
	CloseHandle(handle2);

	system("pause");


	return 0;
}

DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
	VideoCapture cap0;
	Mat frame0;

	cap0.open(0);

	//Sleep(2000);
	cap0.set(CV_CAP_PROP_FRAME_WIDTH, 640);
	cap0.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

	
	if (!cap0.isOpened())
	{
		cout << "camera0 open failed!" << endl;
	}
	
	
	while (true)
	{
		cap0.grab();
		cap0.read(frame0);
		if (frame0.empty())
		{
			cout << "Camera0 grab frame failed!" << endl;
		}
		else
		{
			imshow("Video0", frame0);
		}

		if (waitKey(30) == 27)
			break;
	}

	return 0;
}


DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
	VideoCapture cap1;
	Mat frame1;

	cap1.open(1);

	//Sleep(2000);
	cap1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
	cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

	
	if (!cap1.isOpened())
	{
		cout << "camera1 open failed!" << endl;
	}
	
	while (true)
	{
		cap1.grab();
		cap1.read(frame1);
		if (frame1.empty())
		{
		//	Sleep(2000);
			cout << "Camera1 grab frame failed!" << endl;
		}
		else
		{
			imshow("Video1", frame1);
		}

		if (waitKey(30) == 27)
			break;
	}

	

	return 0;
}


效果图:


测试小工具1:同时打开多个相机(多线程)_第1张图片

你可能感兴趣的:(图像处理)