Capture pictures from multiple(4) web cams using OpenCV
===

i managed to get images from 4 identical USB web cams and save them in four different .avi files. 

I use OpenCv2.4. The source code for my application is compiled with "g++ take_multicam_pictures.cpp -lcvaux -lopencv_highgui -lcv -lcxcore -o take_multicam_pictures", in Ubuntu10.04.

The problem is that i have to select each device, take a picture, and i have to release the device before a new one can be opened. And because of this, the number of frames/second for each web cam is very low (like one frame/second). 

If i don't release each capture device before opening a new one, i get the error "VIDIOC_STREAMON: No space left on device". 

But i managed to start simultaneously the laptop webcam, and one external web cam. Here is the output from lsusb.

    $ lsusb
    Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 007 Device 002: ID 093a:2510 Pixart Imaging, Inc. Hama Optical Mouse
    Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
    Bus 002 Device 007: ID 0ac8:3450 Z-Star Microelectronics Corp. 
    Bus 002 Device 006: ID 0ac8:3450 Z-Star Microelectronics Corp. 
    Bus 002 Device 005: ID 0ac8:3450 Z-Star Microelectronics Corp. 
    Bus 002 Device 004: ID 0ac8:3450 Z-Star Microelectronics Corp. 
    Bus 002 Device 003: ID 0409:005a NEC Corp. HighSpeed Hub
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 001 Device 002: ID 04f2:b071 Chicony Electronics Co., Ltd 2.0M UVC Webcam / CNF7129
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

This is the code i used for getting pictures from all four web cams, and save them in four different video files. 

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    #include <stdio.h>
    #include <opencv/cv.h>

    using namespace cv;
    using namespace std; 
    IplImage* frame1 = 0;

    int record_cam(int dev, CvVideoWriter *writer)
    { 
        CvCapture* capture1 = cvCaptureFromCAM(dev);	// select device 
    
        cvGrabFrame(capture1);				            // capture a frame
        
        frame1 = cvRetrieveFrame(capture1);             // retrieve the captured frame
    
        cvWriteFrame(writer, frame1);					// write captured           frame to .avi file	
       
        cvReleaseCapture(&capture1);					// release the capture device
    }

    // A Simple Camera Capture Framework 
    int main() 
    {
        CvVideoWriter *writer1 = 0;			//create writer device that allows us to place frames in video file
        CvVideoWriter *writer2 = 0;
        CvVideoWriter *writer3 = 0;
        CvVideoWriter *writer4 = 0;
        int isColor = 1;					//video properties
        int fps     = 3; 
        int frameW  = 640; 					//webcam dimension
        int frameH  = 480;
    
        writer1 = cvCreateVideoWriter("out1.avi",CV_FOURCC('F', 'L', 'V', '1'),fps,cvSize(frameW,frameH),isColor);
        writer2 = cvCreateVideoWriter("out2.avi",CV_FOURCC('F', 'L', 'V', '1'),fps,cvSize(frameW,frameH),isColor);
        writer3 = cvCreateVideoWriter("out3.avi",CV_FOURCC('F', 'L', 'V', '1'),fps,cvSize(frameW,frameH),isColor);
        writer4 = cvCreateVideoWriter("out4.avi",CV_FOURCC('F', 'L', 'V', '1'),fps,cvSize(frameW,frameH),isColor);
    
        int nFrames = 20, i = 0, key;		//capture 20 frames from each webcam and saves them in video files (out1.avi, out2.avi ...)
   
        for(i=0;i<nFrames;i++)
        {
            record_cam(1, writer1);			//capture frames from each web cam and writes them in different .avi files
            record_cam(2, writer2);
            record_cam(3, writer3);
            record_cam(4, writer4);
        }
    
        cvReleaseVideoWriter(&writer1);
        cvReleaseVideoWriter(&writer2);
        cvReleaseVideoWriter(&writer3);
        cvReleaseVideoWriter(&writer4);
    
        return 0;
    }