Displaying multiple camera feeds simultaneously with opencv?

19,594

Solution 1

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;
}

Solution 2

Try this:

    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;
    }

Solution 3

put the declaration of frame1 and frame2 outside the while loop

I've had the same problem creating a WebCam class. Worked fine for a single instance with the declaration of frame and capture inside but multiple instances required the constructor to be passed both the frame and capture pointers.

openCV seemingly can not multi-thread some important facilities

BTW, with X11, to get multi-threading to work with cvWaitKey() you need to link in libX11.so.6 and call XInitThreads(); as the first instruction in main()

hope this helps Dave laine.

Share:
19,594
JM92
Author by

JM92

Updated on June 08, 2022

Comments

  • JM92
    JM92 almost 2 years

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