Ask Your Question

Dynamic display multiple video feed from multiple webcam

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

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

namedWindow("Camera 1",1);
namedWindow("Camera 2",1);
namedWindow("Camera 3",1);
namedWindow("Camera 4",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


edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

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

Haris gravatar image

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

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

#include "opencv2/opencv.hpp"
#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];

     if(!cap[camIdx].isOpened())  // check if we succeeded
      camIdx ++;

    camNo = camIdx;

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

    Mat frame[camNo];

    char WinName[10];
        for(int i=0;i<camNo;i++){
         cap[i] >> frame[i];
         imshow(WinName, frame[i]);

        char c=waitKey(30);

        if( c== 27) break;

    return 0;


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 = ""; //From mjpeg streamer 
   const string videoStreamAddress2 = ""; //From mjpeg streamer

 if(! {
    cout << "Error opening video stream1" <<endl;
   if(! {
    cout << "Error opening video stream2 " <<endl;

     if(! {
         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);
      imshow("Stream 1", stream1);

     if(! {
         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);
      imshow("Stream 2", stream2);

     if(waitKey(33) >= 0) break;
    return 0;
edit flag offensive delete link more



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

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

You are welcome.

Haris gravatar imageHaris ( 2013-10-26 05:05:03 -0500 )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 -0500 )edit

You can directly open live stream using OpenCV VideoCapture. You can have something like"")

Haris gravatar imageHaris ( 2013-11-01 04:40:10 -0500 )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 -0500 )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;""); / 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 -0500 )edit
   VideoCapture vcap;
   Mat image;
  const string videoStreamAddress = "";//From mjpeg streamer
 if(! {
    cout &lt;&lt; "Error opening video stream or file" &lt;&lt; std::endl;
    return -1;
    for(;;) {
      return 0;
    imshow("Output Window", image);
     if(waitKey(1) &gt;= 0) break;
Haris gravatar imageHaris ( 2013-12-16 07:28:56 -0500 )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 -0500 )edit

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


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

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

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

Question Tools


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

Seen: 8,860 times

Last updated: Dec 19 '13