Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying multiple camera feeds simultaneously with opencv?

Tags:

c++

opencv

camera

I have two cameras that I need to display video from at the same time, either in separate windows or in the same window. However, using the following code, only one of the camera feeds (camera(1)) displays. Could someone point out what needs to be changed in my code, or link to other code that would achieve the desired effect?

N.B. This is NOT for stereo vision.

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();


}
like image 569
JM92 Avatar asked Dec 05 '22 13:12

JM92


1 Answers

Just to clarify, are you using C or C++? If you are using C++, please use C++ interfaces of OpenCV.

Example below works for me:

#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;
}
like image 138
Rasim Avatar answered Dec 29 '22 00:12

Rasim