并排显示 2 个视频非常简单 - 只需从两个摄像头获取帧并将结果复制到一个图像(Mat)中:
VideoCapture videoLeft(0), videoRight(1);
Mat left, right, both;
int width = 640, height = 480;
both = Mat(height + 100, 2 * width + 150, CV_MAKETYPE(8, 3), CV_RGB(100, 100, 100));
while(true)
{
videoLeft >> left;
videoRight >> right;
if (left.data == NULL || right.data == NULL)
break;
left.copyTo(Mat(both, Rect(50, 50, width, height)));
right.copyTo(Mat(both, Rect(100 + width, 50, width, height)));
imshow("images", both);
waitKey(30);
}
将此代码中的宽度和高度值替换为从您的相机抓取的图像大小 - 您可以通过检查 left.rows(height) 和 left.cols(width) 的值轻松检查它。
您可以调整这两条线的位置:
left.copyTo(Mat(both, Rect(50, 50, width, height)));
right.copyTo(Mat(both, Rect(100 + width, 50, width, height)));
只需使用 Rect 构造函数的第一个和第二个参数的值。第一个参数是 x 位置,第二个是 y。您可以在按下某个键后更改它们。更多信息 - [由于链接断开而更新为网络存档:] http://web.archive.org/web/20130619174223/http://bsd-noobz.com/opencv-guide/45-1-using-keyboard