c++打开多个摄像头
时间: 2023-08-04 22:17:37 浏览: 64
可以使用OpenCV库来打开多个摄像头,以下是一个简单的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap1(0);
VideoCapture cap2(1);
if (!cap1.isOpened() || !cap2.isOpened()) {
cerr << "Error: Unable to open camera" << endl;
return -1;
}
Mat frame1, frame2;
while (true) {
cap1 >> frame1;
cap2 >> frame2;
if (frame1.empty() || frame2.empty()) {
cerr << "Error: Unable to capture frame" << endl;
break;
}
imshow("Camera 1", frame1);
imshow("Camera 2", frame2);
if (waitKey(30) == 27) {
break;
}
}
cap1.release();
cap2.release();
destroyAllWindows();
return 0;
}
```
这段代码使用了两个 VideoCapture 对象来打开两个摄像头,然后在 while 循环中读取每个摄像头的帧,并在窗口中显示出来。请注意,摄像头的索引从 0 开始。在此示例中,我们打开索引为 0 和 1 的两个摄像头。