使用OpenCV从多个网络摄像头(4个)捕获图片

时间:2012-07-31 10:00:37

标签: opencv

我设法从4个相同的USB网络摄像头获取图像,并将它们保存在四个不同的.avi文件中。

我使用的是OpenCV 2.4。我的应用程序的源代码编译为:

g++ take_multicam_pictures.cpp -lcvaux -lopencv_highgui -lcv -lcxcore -o take_multicam_pictures

(在Ubuntu 10.04中)。

问题是我必须选择每个设备,拍照,然后我必须先释放设备才能打开新设备。因此,每个网络摄像头的帧数/秒非常低(如一帧/秒)。

如果我在打开新的捕获设备之前没有释放它,我会收到以下错误:

VIDIOC_STREAMON: No space left on device

但我设法同时开始使用笔记本电脑摄像头和一个外置网络摄像头。以下是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

这是我用来从所有四个网络摄像头获取图片的代码,并将它们保存在四个不同的视频文件中。

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

2 个答案:

答案 0 :(得分:0)

您收到的错误消息来自USB总线。它基本上告诉您USB连接上的 dataload 太高。原因是在OpenCV中打开网络摄像头流的默认设置。

最简单的解决方法是为要附加的每个摄像头使用单独的USB总线。 OpenCV中的实现(至少2.4.3.2)缺乏对为大量网络摄像头正确指定帧速率的支持,因此我为了自己的目的修补了源代码,以便能够拥有多个网络摄像头。

您可以在我的 answer 中找到有关OpenCV Q&amp; A网站上的问题的更多详细信息。

答案 1 :(得分:0)

我已经制作了一个程序,可以使用openCV和Tkinter for GUI从多个网络摄像头捕获图片。后记的照片可以在电影中推送。

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