本文介绍了使用OpenCV捕获来自多个网络摄像头(4个)的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我使用OpenCV 2.4.我的应用程序的源代码使用以下代码编译:

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

(在Ubuntu 10.04中).

(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

但是我设法同时启动了笔记本电脑网络摄像头和一个外部网络摄像头.这是lsusb的输出:

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

推荐答案

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

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

最简单的解决方法是为要连接的每个摄像机使用单独的USB总线.OpenCV(至少2.4.3.2)中的实现缺少为许多网络摄像头正确指定帧速率的支持,因此我出于自身目的对源进行了修补,以便能​​够拥有多个网络摄像头.

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.

您可以在我的 回答 到OpenCV问答网站上的问题.

You can find more details in my answer to a question on the OpenCV Q&A site.

这篇关于使用OpenCV捕获来自多个网络摄像头(4个)的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 04:41