【发布时间】:2016-04-12 11:06:26
【问题描述】:
我正在尝试使用 Raspberry Pi 相机录制视频。我想用openCV来做录音,因为当我有这个工作时,我会做更多的处理。
我正在使用来自here 的 raspicam/raspicam_cv 库。我可以打开相机并读取 cv::Mat 并创建视频,但帧速率在 1-2 Hz 之间。
我的代码在这里:
#include <ctime>
#include <iostream>
#include <raspicam/raspicam_cv.h>
#include <cstdio>
using namespace std;
int main ( int argc,char **argv ) {
time_t timer_begin,timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=100;
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time ( &timer_begin );
cv::VideoWriter writer("Avideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 30, cv::Size(1280,960), true);
for ( int i=0; i<nCount; i++ ) {
char name[32] = {0};
Camera.grab();
Camera.retrieve ( image);
writer.write(image);
}
cout<<"Stop camera..."<<endl;
Camera.release();
time ( &timer_end ); /* get current time; same as: timer = time(NULL) */
double secondsElapsed = difftime ( timer_end,timer_begin );
cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
}
当我运行程序 raspivid 时,我可以看到似乎以 30 Hz 运行的视频,这是我想要实现的。
由于某种原因,我也只能使用 1280x960 尺寸使此代码工作。
如果有人能给我一些建议,那就太好了。
【问题讨论】:
-
请首先确定是哪一行导致速度变慢。例如,我会先将视频作者
writer.write(image);注释掉,看看这对速度有何影响。 -
注释掉
writer.write(image)会使视频精确到 10 Hz -
请提供raspivid的cpp代码链接进行比较。
-
源代码中没有 rapisvid 文件/项目。你是说 raspicam_cv_test 吗?
标签: c++ opencv video raspberry-pi