我正在测量C ++图像处理程序中几个功能的执行时间。特别是,我想拥有使用USB摄像机捕获帧的实际执行时间。

问题在于结果似乎与相机参数不一致:相机的最大拍摄速度应该为30 fps,而我得到的测量时间通常少于33 ms,这就是我得到的值认为应该是可以预期的。例如,我得到很多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);
    }
}

最佳答案

正如@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。

这就是@Jacob Hull表示阻止的含义;如果readFrame只是读取最后一帧,则直到新帧之前它都不会阻塞,您将获得与测量相同的结果。

关于c++ - 测得的fps高于理论值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47186951/

10-12 20:43