C++ 如何以MJPEG像素格式从usb摄像头捕获视频

C++ 如何以MJPEG像素格式从usb摄像头捕获视频,c++,opencv,camera,opencv3.0,C++,Opencv,Camera,Opencv3.0,我在立体相机项目中工作(将两个usb相机连接到我的笔记本电脑),但问题是我的网络相机格式是“YUYV”,而这种格式不支持同时运行两个相机,所以我怎么做?这是我的C++代码和OpenCV3/P> #include <opencv2/opencv.hpp> /** * @file main.cpp * @brief OpenCV Stereo Webcam. * @author Martin Peris (http://www.martinperis.

我在立体相机项目中工作(将两个usb相机连接到我的笔记本电脑),但问题是我的网络相机格式是“YUYV”,而这种格式不支持同时运行两个相机,所以我怎么做?这是我的C++代码和OpenCV3/P>
#include <opencv2/opencv.hpp>
/**
*       @file main.cpp
*       @brief OpenCV Stereo Webcam.
*       @author Martin Peris (http://www.martinperis.com)
*       @date 21/08/2011
*/

/*
        main.cpp - Implement a simple stereo webcam with OpenCV and C++
        Copyright (c) 2011 Martin Peris (http://www.martinperis.com).
        All right reserved.

        This application is free software; you can redistribute it and/or
        modify it under the terms of the GNU Lesser General Public
        License as published by the Free Software Foundation; either
        version 2.1 of the License, or (at your option) any later version.

        This application is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
        Lesser General Public License for more details.

        You should have received a copy of the GNU Lesser General Public
        License along with this application; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


//This works for me on OpenCV 2.0 with 2 Logicool webcams.
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
#include <sstream>
#include <opencv2/video/background_segm.hpp>  
#include <opencv2/video/background_segm.hpp>
#include "cv.h"
#include "highgui.h"
#include <iostream>

//Maybe in OpenCV2.2 the correct include statement would be:
//#include "opencv2/opencv.hpp"


int main(int, char**)
{
    cv::VideoCapture capLeft(4); // open the Left camera
    cv::VideoCapture capRight(2); // open the Right camera

    if(!capLeft.isOpened() || !capRight.isOpened())  // check if we succeeded
    {
        std::cerr << "ERROR: Could not open cameras." << std::endl;
        return -1;
    }

    capLeft.set(CV_CAP_PROP_FPS, 25);
    capRight.set(CV_CAP_PROP_FPS, 25);

// Values taken from output of Version 1 and used to setup the exact same parameters with the exact same values!
    capLeft.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    capLeft.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    capRight.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    capRight.set(CV_CAP_PROP_FRAME_HEIGHT, 240);    
    cv::namedWindow("Left",1);
    cv::namedWindow("Right",1);

    for(;;)
    {
        bool isValid = true;


        cv::Mat frameLeft;
        cv::Mat frameRight;

        try
        {
          capLeft >> frameLeft; // get a new frame from left camera
          capRight >> frameRight; //get a new frame from right camera
        }
        catch( cv::Exception& e )
        {
          std::cout << "An exception occurred. Ignoring frame. " << e.err << std::endl;
          isValid = false;
        }

        if (isValid)
        {
          try
          {
            cv::imshow("Left", frameLeft);
            cv::imshow("Right", frameRight);

            /************************************************************
            *    This is the place for all the cool stuff that you      *
            *    want to do with your stereo images                     *
            ************************************************************/

            //TODO:...

          }
          catch( cv::Exception& e )
          {
            /************************************************************ 
            *    Sometimes an "Unrecognized or unsuported array type"   * 
            *    exception is received so we handle it to avoid dying   *
            ************************************************************/
            std::cout << "An exception occurred. Ignoring frame. " << e.err << std::endl;
          }
        }
        if(cv::waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
#包括
/**
*@file main.cpp
*@brief-OpenCV立体声网络摄像头。
*@作者马丁·佩里斯(http://www.martinperis.com)
*@date 21/08/2011
*/
/*
MCP.CPP——用OpenCV和C++实现一个简单的立体摄像头
版权所有(c)2011 Martin Peris(http://www.martinperis.com).
保留所有权利。
此应用程序是免费软件;您可以重新分发它和/或
根据GNU小公众的条款对其进行修改
自由软件基金会发布的许可证;任何一个
许可证的2.1版,或(由您选择)任何更高版本。
此应用程序的发布是为了希望它有用,
但无任何保证;甚至没有任何关于
适销性或适合某一特定目的。见GNU
有关更多详细信息,请参阅较低的通用公共许可证。
您应该已经收到GNU Lesser General Public的副本
许可证随本申请书一起提交;如果没有,请写信给自由软件
基金会,51富兰克林ST,第五楼,波士顿,美国02110-1301
*/
//我在OpenCV 2.0上使用了2个Logicool网络摄像头。
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“cv.h”
#包括“highgui.h”
#包括
//在OpenCV2.2中,正确的include语句可能是:
//#包括“opencv2/opencv.hpp”
int main(int,char**)
{
cv::VideoCapleft(4);//打开左侧摄像头
cv::VideoCapture capRight(2);//打开右侧摄像头
如果(!capLeft.isopend()| |!capRight.isopend())//检查是否成功
{
std::cerr frameLeft;//从左侧摄影机获取新帧
capRight>>frameRight;//从右侧摄影机获取新帧
}
捕获(cv::异常和e)
{
标准::cout