帧率(FPS, frame per second)计算是视频处理,算法运行中常见的一个话题,因为表现在画面刷新与视觉感官上,所以相对而言,帧率非常影响用户体验,在一般开发中,开发者也会关注算法或视频相关处理的速度,因此统计FPS是一个比较重要的手段,如下是一种统计一段时间内,即固定时间的帧数统计,算出每秒的帧数,即FPS。
一般有好几种计算方法,列举一以下三种:
- 固定帧数计算时间
- 固定时间计算帧数(本文使用)
- 前后帧时间差计算
代码如下
#include#include #include #include void fps_average_count(int cycle_seconds) { static struct timeval time_start, time_end; static bool init = false; static int count = 0, fps = 0; if (!init) { init = true; gettimeofday(&time_start, NULL); gettimeofday(&time_end, NULL); printf("FPS count init done ...n"); } else { int cycle = time_end.tv_sec - time_start.tv_sec; gettimeofday(&time_end, NULL); if (cycle == cycle_seconds) { gettimeofday(&time_start, NULL); fps = count; count = 0; printf("----------n"); printf("%d S average FPS value is: %dn", cycle_seconds, fps / cycle_seconds); } count++; } } int main(int argc, char *argv[]) { while (1) { usleep(33333); fps_average_count(2); } return 0; }
用于统计视频帧率,或者buffer轮转的帧率,可以计算一段时间内的平均帧率!
fps_average_count(2) 代表统计2S 内的平均帧率,如果想要1S的填1就ok啦!



