使用opencv同时显示多个摄像头源?

时间:2012-12-18 03:35:02

标签: c++ opencv camera

我有两台相机需要同时显示视频,无论是在单独的窗口还是在同一个窗口中。但是,使用以下代码,只显示其中一个摄像机输入(摄像机(1))。有人可以指出我的代码中需要更改的内容,或链接到其他可以达到预期效果的代码吗?

N.B。这不适用于立体视觉。

int main()
{

    //initialize and allocate memory to load the video stream from camera 
    CvCapture *capture1 = cvCaptureFromCAM(0);

    if( !capture1 ) return 1;

    //create a window with the title "Video1"
    cvNamedWindow("Video1");

    while(true) {
        //grab and retrieve each frames of the video sequentially 
        IplImage* frame1 = cvQueryFrame( capture1 );

        if( !frame1 ) break;

        //show the retrieved frame in the "Video1" window
        cvShowImage( "Video1", frame1 );

        //wait for 40 milliseconds
        int c = cvWaitKey(40);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if((char)c==27 ) break;
    }

    //initialize and allocate memory to load the video stream from camera 
    CvCapture *capture2 = cvCaptureFromCAM(1);

    if( !capture2 ) return 1;

    //create a window with the title "Video2"
    cvNamedWindow("Video2");

    while(true) {
        //grab and retrieve each frames of the video sequentially 
        IplImage* frame2 = cvQueryFrame( capture2 );

        if( !frame2 ) break;        

        //show the retrieved frame in the "Video2" window
        cvShowImage( "Video2", frame2 );

        //wait for 40 milliseconds
        int c = cvWaitKey(40);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if((char)c==27 ) break;
    }

    //destroy the opened window
    cvDestroyWindow("Video1"); 
    cvDestroyWindow("Video2");   
    //release memory
    cvReleaseCapture( &capture1 );
    cvReleaseCapture( &capture2 );

    return 0;  

    //VideoCapture1();
    //VideoCapture2();


}

3 个答案:

答案 0 :(得分:11)

只是澄清一下,您使用的是C还是C ++?如果您使用的是C ++,请使用OpenCV的C ++接口。

以下示例适用于我:

#include <opencv2/opencv.hpp>

int main()
{
    //initialize and allocate memory to load the video stream from camera 
    cv::VideoCapture camera0(0);
    cv::VideoCapture camera1(1);

    if( !camera0.isOpened() ) return 1;
    if( !camera1.isOpened() ) return 1;

    while(true) {
        //grab and retrieve each frames of the video sequentially 
        cv::Mat3b frame0;
        camera0 >> frame0;
        cv::Mat3b frame1;
        camera1 >> frame1;

        cv::imshow("Video0", frame0);
        cv::imshow("Video1", frame1);

        //wait for 40 milliseconds
        int c = cvWaitKey(40);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if(27 == char(c)) break;
    }

    return 0;
}

答案 1 :(得分:3)

试试这个:

    CvCapture *capture1 = cvCaptureFromCAM(0);
    CvCapture *capture2 = cvCaptureFromCAM(1);

    if( !capture1 ) return 1;
    if (!capture2) return 1 ; 
    cvNamedWindow("Video1");
    cvNamedWindow("Video2") ;

    while(true) {
        //grab and retrieve each frames of the video sequentially 
        IplImage* frame1 = cvQueryFrame( capture1 );
        IplImage* frame2 = cvQueryFrame( capture2 );

        if( !frame1 || !frame2 ) break;

        cvShowImage( "Video1", frame1 );
        cvShowImage( "Video2", frame2 );

        //wait for 40 milliseconds
        int c = cvWaitKey(40);

        //exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
        if((char)c==27 ) break;
    }

答案 2 :(得分:2)

将frame1和frame2的声明放在while循环之外

我在创建WebCam类时遇到了同样的问题。 通过框架和捕获声明在单个实例中工作正常 但是多个实例需要传递构造函数 帧和捕获指针。

openCV看似无法多线程一些重要的设施

使用X11,BTW要获得多线程以使用cvWaitKey(),你需要 链接libX11.so.6 并调用XInitThreads();作为main()

中的第一条指令 希望这会有所帮助 戴夫莱恩。