Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can try this code, I haven’t tested with multiple camera, hope this will work,

#include "opencv2/opencv.hpp"
#include<iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

int main(int, char**)
{
    int maxCamNo=5;
    int camNo=0;
    int camIdx=0;

    VideoCapture cap[maxCamNo];

    while(1){
    cap[camIdx].open(camIdx);
     if(!cap[camIdx].isOpened())  // check if we succeeded
        break;
      camIdx ++;
     }

    camNo = camIdx;

        if(camNo<1) {
    cout<<"No camera attached...!"<<endl;
    return -1;
    }

    Mat frame[camNo];

    char WinName[10];
    for(;;)
    {
        for(int i=0;i<camNo;i++){
         cap[i] >> frame[i];
         sprintf(WinName,"Camera%d",i);
         imshow(WinName, frame[i]);
        }

        char c=waitKey(30);

        if( c== 27) break;
    }

    return 0;
}

You can try this code, I haven’t tested with multiple camera, hope this will work,

#include "opencv2/opencv.hpp"
#include<iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

int main(int, char**)
{
    int maxCamNo=5;
    int camNo=0;
    int camIdx=0;

    VideoCapture cap[maxCamNo];

    while(1){
    cap[camIdx].open(camIdx);
     if(!cap[camIdx].isOpened())  // check if we succeeded
        break;
      camIdx ++;
     }

    camNo = camIdx;

        if(camNo<1) {
    cout<<"No camera attached...!"<<endl;
    return -1;
    }

    Mat frame[camNo];

    char WinName[10];
    for(;;)
    {
        for(int i=0;i<camNo;i++){
         cap[i] >> frame[i];
         sprintf(WinName,"Camera%d",i);
         imshow(WinName, frame[i]);
        }

        char c=waitKey(30);

        if( c== 27) break;
    }

    return 0;
}

Edit

Based on the comments below here is the code for multiple video stream from internet. I got these links from here and working fine for me on windows vs2010 environment.

   VideoCapture vcap1;
   VideoCapture vcap2;
   Mat stream1, stream2;
   Mat grains(480,640,CV_8UC3,Scalar::all(0));
   const string videoStreamAddress1 = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8?dummy=param.mjpg"; //From mjpeg streamer 
   const string videoStreamAddress2 = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8?dummy=param.mjpg"; //From mjpeg streamer

 if(!vcap1.open(videoStreamAddress1)) {
    cout << "Error opening video stream1" <<endl;
    }  
   if(!vcap2.open(videoStreamAddress2)) {
    cout << "Error opening video stream2 " <<endl;
    }

    for(;;){   
     if(!vcap1.read(stream1)) {
         putText(grains, std::string("Stream  not avilable....."), cv::Point(150,200),  cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(0,255,0,255));
         imshow("Stream 1", grains);
     }
     else
      imshow("Stream 1", stream1);

     if(!vcap2.read(stream2)) {
         putText(grains, std::string("Stream  not avilable....."), cv::Point(150,200),  cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(0,255,0,255));
         imshow("Stream 2", grains);
     }
    else
      imshow("Stream 2", stream2);

     if(waitKey(33) >= 0) break;
     }  
    return 0;