Ask Your Question
0

Dynamic display multiple video feed from multiple webcam

asked 2013-10-26 00:02:08 -0600

xMayank gravatar image

I have implemented code for capturing live video feed from webcam. But the code i have implemented is used 4 cams only. I want to make it dynamic. I.e: If i connect 2 webcam then it should display two windows with live feed. If i connect 3 cams then it should get 3 windows with live feed from all three cameras. I am getting the number of cameras being connected to the system using videoInput.h So now only part is how to make it dynamic for webcam. Here's the code.

int main(){

//Create matrix to store image
Mat image;
Mat image1;
Mat image2;
Mat image3;

//Mat image1;
//initailize capture
videoInput VI;
int numDevices = VI.listDevices();  
VideoCapture cap;
VideoCapture cap1;
VideoCapture cap2;
VideoCapture cap3;

bool x = false;
//VideoCapture cap1;
cap.open(0);
cap1.open(1);
cap2.open(2);
cap3.open(3);

namedWindow("Camera 1",1);
namedWindow("Camera 2",1);
namedWindow("Camera 3",1);
namedWindow("Camera 4",1);

while(1){
    //copy webcam stream to image
    cap >> image;
    cap1 >> image1;
    cap2 >> image2;
    cap3 >> image3;
    cap4 >> image4;
    //cap1 >> image1;
    imshow("Camera 1",image);
    imshow("Camera 2",image1);
    imshow("Camera 3",image2);
    imshow("Camera 4",image3);

    //imshow("Camera 2",image1);
    //delay 33ms
    waitKey(33);
}

}

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2013-10-26 04:52:41 -0600

Haris gravatar image

updated 2013-12-19 06:02:24 -0600

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;
edit flag offensive delete link more

Comments

1

Thanks mate...Let me try the code..

xMayank gravatar imagexMayank ( 2013-10-26 05:03:48 -0600 )edit
1

You are welcome.

Haris gravatar imageHaris ( 2013-10-26 05:05:03 -0600 )edit

@Haris Moonamkunnu: I want to access live video feed from IP camera how can i do it, and the same condition goes with this also, i want multipler camera live feed even for IP camera..

xMayank gravatar imagexMayank ( 2013-11-01 03:14:40 -0600 )edit

You can directly open live stream using OpenCV VideoCapture. You can have something like cap.open("http://192.168.1.30:8080/?action=stream?dummy=param.mjpg")

Haris gravatar imageHaris ( 2013-11-01 04:40:10 -0600 )edit

@Haris Moonamkunnu: I am having problem with getting the live feed from IP Cam. Can you please provide me with a sample code.

xMayank gravatar imagexMayank ( 2013-12-16 06:50:52 -0600 )edit

Here's the code which i tried

include <opencv/cv.h>

include <opencv/highgui.h>

using namespace cv;

int main(int argc, char argv[]) { Mat frame; namedWindow("video", 1); VideoCapture cap; cap.open("http://172.41.20.55:80/video/mjpg.cgi?dummy=.mjpg"); / VideoCapture cap(0);*/ while ( cap.isOpened() ) { cap >> frame; if(frame.empty()) break;

    imshow("video", frame);
    if(waitKey(30) &gt;= 0) break;
}

return 0;

} But I am unable to get the video feed.

xMayank gravatar imagexMayank ( 2013-12-16 06:53:39 -0600 )edit
   VideoCapture vcap;
   Mat image;
  const string videoStreamAddress = "http://192.168.1.206:8080/?action=stream?dummy=param.mjpg";//From mjpeg streamer
 if(!vcap.open(videoStreamAddress)) {
    cout &lt;&lt; "Error opening video stream or file" &lt;&lt; std::endl;
    return -1;
    }
    for(;;) {
    if(!vcap.read(image)) 
      return 0;
    imshow("Output Window", image);
     if(waitKey(1) &gt;= 0) break;
     }
Haris gravatar imageHaris ( 2013-12-16 07:28:56 -0600 )edit

@Haris Moonamkunnu: I get and error saying warning: Error opening file <../../modules/highgui/src/cap_ffmpeg_impl.hpp:529>

xMayank gravatar imagexMayank ( 2013-12-16 07:34:42 -0600 )edit

Got a new error: Can you please tell me what is it related to ? warning: Could not find codec parameters

(../../modules/highgui/src/cap_ffmpeg_impl.hpp:540)

xMayank gravatar imagexMayank ( 2013-12-16 08:32:27 -0600 )edit

Are you able to capture using webcam?. Seems codec issue with ffmpeg.

Haris gravatar imageHaris ( 2013-12-16 09:40:36 -0600 )edit

Question Tools

Stats

Asked: 2013-10-26 00:02:08 -0600

Seen: 9,367 times

Last updated: Dec 19 '13