Ask Your Question
0

openning multiple webcam

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

xiongtec gravatar image

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

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"

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

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

    return 0;

}
edit retag flag offensive close merge delete

Comments

1

and the trouble is ?

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

1 answer

Sort by ยป oldest newest most voted
1

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

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

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

Stats

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

Seen: 495 times

Last updated: Nov 22 '13