我正在测量C++图像处理程序中几个函数的执行时间。特别是,我想要用我的USB摄像头捕获帧的实际执行时间。
问题是,结果似乎与相机参数不一致:相机应该是最多30fps,而我经常得到的测量时间小于33ms来获得一帧,这是我认为应该期待的值。例如,我得到了很多12毫秒的间隔,这似乎真的太少了。
代码如下:
#include <time.h>
#include <sys/time.h>
double get_wall_time(){
struct timeval time;
if (gettimeofday(&time,NULL)){
// Handle error
return 0;
}
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
int main(){
while (true) {
double previoustime = get_wall_time();
this->camera.readFrame();
double currenttime = get_wall_time();
std::cout << currenttime-previoustime << std::endl;
// Other stuff
// ...
// ...
usleep(3000);
}
}
发布于 2017-11-09 03:15:29
正如@Revolver_Ocelot所说,您正在测量从get_wall_time
结束到另一个类似调用结束所花费的时间。要修复您的代码,请执行以下操作:
double currenttime = get_wall_time();
while (true) {
double previoustime = currenttime;
this->camera.readFrame();
...
currentime = get_wall_time();
}
你能找出不同之处吗?此代码测量每次传递之间的间隔,这是您希望获得的每秒帧数。
您可以读取相机的速度不会与它完成新帧的速率相同。您的摄像机可以以30 FPS的速度记录,您可以以15 FPS或90 FPS的速度读取它,从而对帧流进行次采样或过采样。
您可以过采样的限制是读取并存储图像所需的时间为1/次。
这就是@雅各布·赫尔关于阻塞的意思;如果readFrame
只读取最后一帧,直到新的帧,它才会阻塞,您将获得像测量一样的结果。
https://stackoverflow.com/questions/47186951
复制相似问题