《OpenCV一个窗口显示两个摄像头》

【一】原理

1、开启两个摄像头,系统默认从0开始查找,两个摄像头使用0和1就够啦,然后把显示窗口尺寸固定,这里用的是320和240

VideoCapture capture1(0);
VideoCapture capture2(1);

2、创建一个黑色图片,大小刚好可以容纳两个摄像头窗口,width=640,height=240

Mat frame(240,640,CV_8UC3,Scalar(0,0,0));

3、创建ROI区域

ROIImage1 = frame(Rect(0,0,320,240));
ROIImage2 = frame(Rect(320, 0, 320, 240));

4、创建掩膜

掩膜必须是灰度图,因此先要把图片转化为灰度图

cvtColor(frame1, gray_Image1,CV_RGB2GRAY);
cvtColor(frame2, gray_Image2, CV_RGB2GRAY);

然后把图片复制给掩膜

mask1 = gray_Image1;
mask2 = gray_Image2;

5、把掩膜复制到ROI区域,frame1为要覆盖的图片,意思就是把frame1复制ROI区域的掩膜上。

frame1.copyTo(ROIImage1, mask1);

frame2.copyTo(ROIImage2, mask2);

【二】效果图

《OpenCV一个窗口显示两个摄像头》_第1张图片

 

《OpenCV一个窗口显示两个摄像头》_第2张图片

 

《OpenCV一个窗口显示两个摄像头》_第3张图片

 

【三】源代码

#include
#include

using namespace cv;
using namespace std;

//【画框】
void Draw_rectangle(Mat img,Point pt1, Point pt2)
{
	rectangle(img, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}

int main()
{
	Mat frame1, frame2,ROIImage1, ROIImage2,mask1,mask2,gray_Image1, gray_Image2;
	VideoCapture capture1(0);
	VideoCapture capture2(1);
	capture1.set(CV_CAP_PROP_FRAME_WIDTH, 320);
	capture1.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
	capture2.set(CV_CAP_PROP_FRAME_WIDTH, 320);
	capture2.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
	if (!capture1.isOpened())
	{
		cout << "Come on baby 1 !" << endl;
		return -1;
	}
	if (!capture2.isOpened())
	{
		cout << "Come on baby 2 !" << endl;
		return -1;
	}
	Mat frame3(240,640,CV_8UC3,Scalar(0,0,0));
	char temp_1[20];
	char temp_2[20];
	while (true)
	{
		capture1 >> frame1;
		capture2 >> frame2;
		Draw_rectangle(frame1, Point(0,0),Point(frame1.cols, frame1.rows));
		Draw_rectangle(frame2, Point(0, 0), Point(frame2.cols, frame2.rows));
		sprintf(temp_1, "capture1");
		putText(frame1, temp_1, Point(frame1.cols / 16, frame1.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
		sprintf(temp_2, "capture2");
		putText(frame2, temp_2, Point(frame2.cols / 16, frame2.rows / 10), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 0, 255));
		ROIImage1 = frame3(Rect(0,0,320,240));
		ROIImage2 = frame3(Rect(320, 0, 320, 240));
		//imshow("【ROIImage1】", ROIImage1);
		//imshow("【ROIImage2】", ROIImage2);
		cvtColor(frame1, gray_Image1,CV_RGB2GRAY);
		cvtColor(frame2, gray_Image2, CV_RGB2GRAY);
		mask1 = gray_Image1;
		mask2 = gray_Image2;
		frame1.copyTo(ROIImage1, mask1);
		frame2.copyTo(ROIImage2, mask2);
		imshow("【视频窗口一】", frame1);
		imshow("【视频窗口二】", frame2);
		imshow("【总窗口】", frame3);
		waitKey(1);
	}

	return 0;
}

 

 

 

 

 

你可能感兴趣的:(《OpenCV学习笔记》)