title: how to make dynamic OpenCV webcam video streaming in c++
body: <p>The below is used to get the live video feed from the 4 webcam. But all the code i have written is limited only for four camera. I want to make this code dynamic that is. If two camera is connected it should only display two windows, if 3 are connected it should show 3 window. I am getting the number of camera attached to the System. I want to implement the code for that number of camera. Can someone please point me in right direction.</p>
  
  <pre><code>using namespace cv;
  
  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(); //getting number of camera attached to system                     dynamically.   
  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 &gt;&gt; image;
      cap1 &gt;&gt; image1;
      cap2 &gt;&gt; image2;
      cap3 &gt;&gt; image3;
      cap4 &gt;&gt; image4;
      //cap1 &gt;&gt; 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);
  }
   }
  </code></pre>