程序如下:
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
//initialize and allocate memory to load the video stream from camera
VideoCapture camera0(1);
camera0.set(CAP_PROP_FRAME_WIDTH,320);
camera0.set(CAP_PROP_FRAME_HEIGHT,240);
VideoCapture camera1(0);
camera1.set(CAP_PROP_FRAME_WIDTH,320);
camera1.set(CAP_PROP_FRAME_HEIGHT,240);
if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
while(true) {
//grab and retrieve each frames of the video sequentially
Mat3b frame0;
camera0 >> frame0;
Mat3b frame1;
camera1 >> frame1;
imshow("Video0", frame0);
imshow("Video1", frame1);
//std::cout << frame1.rows() << std::endl;
//wait for 40 milliseconds
int c = waitKey(20);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}
return 0;
}
[注]:
1. 分辨率为320*240,能够正常显示,分辨率太高,只有一个摄像头能正常显示,另一个显示灰度图。原因是USB的带宽有限。
2. 摄像头的序号在opencv中是采用降序的形式,才能正确读取多个摄像头,否则就不能。
3. VideoCapture类有两种用法,一种是VideoCapture(const string& filename)用来打开视频文件,一种是VideoCapture(int device)用来打开设备。