Captura de imágenes de múltiples cámaras web (4) usando OpenCV

I managed to get images from 4 identical USB webcams and save them in four different .avi files.

I use OpenCV 2.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 Ubuntu 10.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 webcam is very low (like one frame/ second).

If I don't release each capture device before opening a new one, I get the following error:

VIDIOC_STREAMON: No space left on device

But I managed to start simultaneously the laptop webcam, and one external webcam. 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 webcams, 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;
}

preguntado el 31 de julio de 12 a las 11:07

I am not very experienced in camera capture, but as I know, OpenCV was not written with good input capabilities. Its main strength is image processing. For a real app, both image input and user interface should be written with dedicated libraries. highgui was meant as a help for rapid prototyping, and not for production. Many questions here on SO are related to the limitations of the input/display/output of OpenCV -

@vasile Thank you for answering. Can you tell me some c/c++ libraries that have good input capabilities (like opening multiple USB web cams, and save frames into separate .avi files, i really don't need image processing)? I am not looking for user interface libraries (at least for now). Maybe later, i will have to send live images to another computer on the internet. -

Chech this out. I did not use it myself, but I heard very positive reviews about it muonics.net/school/spring05/videoInput -

@sammy: videoInput is for Windows - the question mentions Ubuntu as OS. -

2 Respuestas

The error message you get comes from the USB bus. It basically tells you that the dataload on your USB connection is demasiado alto. The reason are the default settings for opening a stream of a webcam in OpenCV.

The simplest workaround is to use a separate USB bus for every camera you want to attach. The implementation in OpenCV (at least 2.4.3.2) lacks support for specifying the framerate correctly for a lot of webcams so I patched the sources for my own purposes to be able to have more than one webcam.

You can find more details in my https://www.youtube.com/watch?v=xB-eutXNUMXJtA&feature=youtu.be to a question on the OpenCV Q&A site.

respondido 08 mar '13, 16:03

Hola, separate USB bus for every camera how can you do this? - PicoGen

Every USB port is connected to one. You have to identify the ports leading to different USB buses. Then connect the cameras to ports leading to different USB buses. lsusb should give you the wanted information, or the device manager in windows. If you only have one bus, buy an USB PCI card. - Pascal

I have made a program to capture pictures from multiple webcams with openCV and Tkinter for the GUI. Afterwords the pictures made can be pushed in a movie.

Link to my github page where you can find source code and exe file

Respondido 14 Oct 20, 19:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.