Ask Your Question

openning multiple webcam

asked 2013-11-22 09:52:50 -0500

xiongtec gravatar image

updated 2013-11-22 10:08:25 -0500

berak gravatar image

Hey guyes i'm having trouble opening two webcam with version 2.4.6. One webcam is the mounted laptop webcam and the other is a usb webcam.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc, char* argv[])
   VideoCapture cap(0); // open the video file for reading
    VideoCapture cap2(1);
    if ( !cap.isOpened() )  // if not success, exit program
         cout << "Cannot open the video file" << endl;
         return -1;

    if ( !cap2.isOpened() )  // if not success, exit program
         cout << "Cannot open the video2 file" << endl;
         return -2;
    //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

     cout << "Frame per seconds : " << fps << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

        Mat frame;
        Mat frame2;
        bool bSuccess =; // read a new frame from video
        bool bSuccess2 =; // read a new frame from video
         if (!bSuccess) //if not success, break loop
                        cout << "Cannot read the frame from video file" << endl;

        imshow("MyVideo", frame); //show the frame in "MyVideo" window
        imshow("MyVideo", frame2); //show the frame in "MyVideo" window
        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
                cout << "esc key is pressed by user" << endl; 

    return 0;

edit retag flag offensive close merge delete



and the trouble is ?

berak gravatar imageberak ( 2013-11-22 10:09:56 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-22 10:20:04 -0500

updated 2013-11-22 10:24:17 -0500

You are displaying the frames in one window... the first replaced by the second one, and you see just the second one ...

try with:

namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
namedWindow("MyVideo2",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo2"

imshow("MyVideo", frame); //show the frame in "MyVideo" window
imshow("MyVideo2", frame2); //show the frame in "MyVideo2" window

p.p. You are using some example for template and when make some of the checks, you are making them just for the first cam ... Try to understand every line of the code just for one cam and write your own code for two cams ;]

edit flag offensive delete link more

Question Tools


Asked: 2013-11-22 09:52:50 -0500

Seen: 300 times

Last updated: Nov 22 '13