简体   繁体   English

测得的fps高于理论值

[英]Measured fps is higher than theoretical one

I'm measuring execution time for several functions in an image processing program in C++. 我正在测量C ++图像处理程序中几个功能的执行时间。 In particular, I want to have the actual execution time for capturing a frame with my USB camera. 特别是,我想拥有使用USB摄像机捕获帧的实际执行时间。

The problem is that the results don't seem consistent with the camera parameters: the camera is supposed to be 30 fps at most, and I often get a measured time that is less than 33 ms for getting a frame, which is the value I think should be expected. 问题在于结果似乎与相机参数不一致:相机的最大拍摄速度应该为30 fps,而我得到的获取帧的测量时间通常少于33毫秒,这就是I认为应该是可以预期的。 For instance, I get a lot of 12 ms intervals and that really seems too little. 例如,我得到很多12毫秒的间隔,这似乎太少了。

Here is the code: 这是代码:

#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);
    }
}

As @Revolver_Ocelot remarked, you are measuring time spent from the end of get_wall_time to the end of another similar call. 正如@Revolver_Ocelot所述,您正在测量从get_wall_time结束到另一个类似调用结束的时间。 To fix your code, do this: 要修复您的代码,请执行以下操作:

double currenttime = get_wall_time();    
while (true) {
    double previoustime = currenttime;
    this->camera.readFrame();
    ... 
    currentime = get_wall_time();
}

Can you spot the difference? 您看得出来差别吗? This code measures the interval between each pass, which is what you want to get frames per second. 此代码测量每次通过之间的间隔,这就是您希望每秒获得的帧数。

The speed at which you can read your camera will not be the same as the rate at which it will complete a new frame. 您读取相机的速度将不同于完成新帧的速度。 Your camera could be recording at 30 FPS and you could be reading it at 15 FPS or 90 FPS, thus subsampling or oversampling the frame stream. 您的相机可能以30 FPS的速度进行录制,而您可能以15 FPS或90 FPS的速度进行读取,因此对帧流进行了二次采样或过采样。

The limit at which you can oversample is 1 / time that it takes to read in an image and store it. 可以超采样的限制是读入图像和存储图像所需的时间/ 1。

That's what @Jacob Hull meant with blocking; 这就是@Jacob Hull表示阻止的含义; if readFrame just reads the last frame, it's not blocking until a new frame and you will get the results like you do with your measurement. 如果readFrame只是读取最后一帧,则直到新帧之前它都不会阻塞,您将获得与测量相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM