opencv双摄像头采集图像(可用作双目视觉的标定图片制作)

环境:opencv2.4.9 + vs2013

代码:

#include 
#include 
#include 
using namespace std;
using namespace cv;

int main()
{
	VideoCapture capture, capture1, capture2;
	capture.open(0); capture1.open(1); capture2.open(2);
	// Read options
	//read_options(argc, argv, capture);
	// Init camera
	if (!capture.isOpened())
	{
		cout << "capture device 0 failed to open!" << endl;
		return 1;
	}
	if (!capture1.isOpened())
	{
		cout << "capture device 1 failed to open!" << endl;
		return 1;
	}
	if (!capture2.isOpened())
	{
		cout << "capture device 2 failed to open!" << endl;
		return 1;
	}
	// Register mouse callback to draw the tracking box
	namedWindow("0", CV_WINDOW_AUTOSIZE);
	namedWindow("1", CV_WINDOW_AUTOSIZE);
	namedWindow("2", CV_WINDOW_AUTOSIZE);
	char filename_l[15];
	char filename_r[15];
	int i = 0;
	Mat frame;
	Mat frame1;
	Mat frame2;
	while (capture.read(frame) && capture1.read(frame1) && capture2.read(frame2))
	{

		imshow("0", frame);
		imshow("1", frame1);
		imshow("2", frame2);
		char c = cv::waitKey(1);
		if (cvWaitKey(10) == 'c') //按c采集图像  
		{
			sprintf(filename_l, "left%d.jpg", i);
			imwrite(filename_l, frame);
			sprintf(filename_r, "right%d.jpg", i++);
			imwrite(filename_r, frame1);
		}
		if (cvWaitKey(33) == 'q') { break; }
	}

	waitKey();
	return 0;
}

说明:可通过按键C控制截取帧

你可能感兴趣的:(opencv双摄像头采集图像(可用作双目视觉的标定图片制作))