我喜欢使用至少60Hz的RPi捕获图像。我的代码使用C++,我们为C++接口(interface)提供了一个库here。但是该库的最大频率为30Hz。
我的目标是最低60 Hz。
到目前为止,我发现raspistill可以达到90Hz,因此我正在尝试将C++程序连接到raspistill代码。
我在这里找到了一个与raspiSTLl直接接口(interface)的PiCam库。不太确定,它可以达到60Hz,我仍在尝试对其进行测试,并遇到一些问题。
我的查询是
(1)如何使用C++在RPi上获得60Hz fps?
(2)为了连接到PiCam,我已经编译,构建和安装了库,没有任何问题。
但是当我捕获时,我得到黑色图像。可能是什么问题?我的代码的一部分如下所示。
CCamera* cam = StartCamera(640, 480,60,1,true);
char mybuffer[640 * 480 * 4];
int ret = cam->ReadFrame(0, mybuffer, sizeof(mybuffer));
cout << " ret " << ret << endl;
Mat img(480, 640, CV_8UC4,mybuffer);
imwrite("img.jpg", img);
img.jpg被捕获为黑色图像。
(3)如何使用PiCam更改为灰度图像?
最佳答案
我在Raspberry Pi 3上使用here的 Raspicam ,在黑白模式下可达到约90 fps。
我目前正在将代码重新用于其他用途,因此它并不是100%完美满足您的需求,但应该可以帮助您。
#include <ctime>
#include <fstream>
#include <iostream>
#include <raspicam/raspicam.h>
#include <unistd.h> // for usleep()
using namespace std;
#define NFRAMES 1000
#define WIDTH 1280
#define HEIGHT 960
int main ( int argc,char **argv ) {
raspicam::RaspiCam Camera;
// Allowable values: RASPICAM_FORMAT_GRAY,RASPICAM_FORMAT_RGB,RASPICAM_FORMAT_BGR,RASPICAM_FORMAT_YUV420
Camera.setFormat(raspicam::RASPICAM_FORMAT_GRAY);
// Allowable widths: 320, 640, 1280
// Allowable heights: 240, 480, 960
// setCaptureSize(width,height)
Camera.setCaptureSize(WIDTH,HEIGHT);
// Open camera
cout<<"Opening Camera..."<<endl;
if ( !Camera.open()) {cerr<<"Error opening camera"<<endl;return -1;}
// Wait until camera stabilizes
cout<<"Sleeping for 3 secs"<<endl;
usleep(3000000);
cout << "Grabbing " << NFRAMES << " frames" << endl;
// Allocate memory for camera buffer
unsigned long bytes=Camera.getImageBufferSize();
cout << "Width: " << Camera.getWidth() << endl;
cout << "Height: " << Camera.getHeight() << endl;
cout << "ImageBufferSize: " << bytes << endl;;
unsigned char *data=new unsigned char[bytes];
for(int frame=0;frame<NFRAMES;frame++){
// Capture frame
Camera.grab();
// Extract the image
Camera.retrieve (data,raspicam::RASPICAM_FORMAT_IGNORE);
}
}
return 0;
}
顺便说一句,它与CImg一起很好地工作。
另外,我还没有时间查看创建新线程来处理每一帧,或者在开始时启动几个线程,并在获取每一帧后使用条件变量来启动一个线程,是否更快。