我设法从 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;
}