简体   繁体   中英

Measured fps is higher than theoretical one

I'm measuring execution time for several functions in an image processing program in C++. In particular, I want to have the actual execution time for capturing a frame with my USB camera.

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. For instance, I get a lot of 12 ms intervals and that really seems too little.

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. 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.

The limit at which you can oversample is 1 / time that it takes to read in an image and store it.

That's what @Jacob Hull meant with blocking; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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