OpenCV:VideoCapture同时打开两个摄像头

程序如下:

#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)用来打开设备。

 

 

 

 

 

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值