opencv 计时 帧率

c++统计执行时间,单位 s。

#include <opencv2/opencv.hpp>

double time0 = static_cast<double>(cv::getTickCount());
xxxxxx
time0 = ((double)cv::getTickCount() - time0) / cv::getTickFrequency();
std::cout << "time : " << time0 << std::endl;

C++版的getTickFrequency返回的是每秒钟的tick数

C版的cvGetTickFrequency返回的是每微妙的tick数

double t = (double)cvGetTickCount();
//  算法过程
t = (double)cvGetTickCount() - t;

printf( "run time = %gms\n", t/(cvGetTickFrequency()*1000) );

printf( "run time = %gs\n", t/(cvGetTickFrequency()*1000000) );

这//opencv 中如何进行程序的计时呢?

#include<opencv2/opencv.hpp>
using namespace cv;
#include<iostream>
using namespace std;
int main(void){
    //查看opencv 读取一张图像所需要的时间
    double time1 = static_cast<double>( getTickCount());//记录起始时间,其中getTickCount()函数返回CPU 自某个事件(如启动电脑)以来走过的时钟周期数
    Mat img = imread("E:/testpicture/crystal.jpg");//通过绝对路径从指定文件夹 读取一张名字为 crystal的 图像,图像格式为 .jpg 注意读取图像必须指定格式
    double time2 = (static_cast<double>( getTickCount()) - time1)/getTickFrequency();//获得了读取一张图像所需的时间单位为秒。 其中getTickFrequency()函数返回CPU一秒钟所走得时钟周期数。

    cout<<"从文件中读取一张图像的时间是:"<< time2 <<"秒"<<endl;//输出运行时间

    return 0;
}

 int64 tStart = 0;
 int64 tDuration = 0;

   tStart = getTickCount();

            tDuration = getTickCount() - tStart;


 double fps = static_cast<double>(getTickFrequency() / tDuration);int64 tStart = 0;
 int64 tDuration = 0;

   tStart = getTickCount();

            tDuration = getTickCount() - tStart;


 double fps = static_cast<double>(getTickFrequency() / tDuration);

  printf("fps:%f\n",fps);

printf("fps:%f\n",fps);

计时函数

1. 纯C 高精度(100ns)测量慢

[cpp]  view plain  copy
  1. #include <Windows.h>  
  2. #include <stdio.h>  
  3. int timer1_test()  
  4. {  
  5.     LARGE_INTEGER start;  
  6.     LARGE_INTEGER end ;  
  7.     LARGE_INTEGER frequency;  
  8.     int i = 0;  
  9.   
  10.     if (!QueryPerformanceFrequency(&frequency))  
  11.     {  
  12.         return -1;  
  13.     }  
  14.   
  15.     QueryPerformanceCounter(&start); //开始计时  
  16.   
  17. //     for (int i = 0; i < 100000; ++i)  
  18. //     {  
  19. //         ;// 用循环来测试计时  
  20. //     }  
  21.     Sleep(1);  
  22.     QueryPerformanceCounter(&end); //结束计时  
  23.   
  24.     printf("main cost:%f\n", (double)(end.QuadPart - start.QuadPart) / (double)frequency.QuadPart); //打印for循环执行时间  
  25.   
  26.     getchar();  
  27.     return 0;  
  28. }  

2.纯C 低精度(1ms)测量快

  1. #include <stdio.h>  
  2. #include <time.h>  
  3.   
  4. void timer2_test()  
  5. {  
  6.     clock_t start,end;  
  7.     start=clock();  
  8.     Sleep(1000);  
  9.     end=clock(); 
  10.     double duration = (double)(end - start);
        cout << "--> time: " << duration << " s" << endl;
        printf("time  : %f  ms \n",duration/1000);   double duration = (double)(end - start);
        cout << "--> time: " << duration << " s" << endl;
        printf("time  : %f  ms \n",duration/1000);
  11. }  

3.C++  OpenCV测试 高精度 view p copy

  1. #include"opencv.hpp"  
  2.   
  3. void timer3_test()  
  4. {  
  5.     int64 start=0,end=0;  
  6.     start = getTickCount();  
  7.     Sleep(1000);  
  8.     end = getTickCount();  
  9.     cout << "The differences: " << 1000.0*(end - start)/getTickFrequency()<<" ms"<< endl;  

这个是在多线程下比较精确的

#include <time.h>
struct timespec time1={0,0};
struct timespec time2={0,0};
int main()
{
   
   
        clock_gettime(CLOCK_REALTIME,&time1);
        sRect result = tracker.update( frame);
        clock_gettime(CLOCK_REALTIME,&time2);
        ///
        sum_time += (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000 ;
        printf("time  : %d  ms \n", (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000);
      
  
    return 0;
} <time.h>
struct timespec time1={0,0};
struct timespec time2={0,0};
int main()
{
   
   
        clock_gettime(CLOCK_REALTIME,&time1);
        sRect result = tracker.update( frame);
        clock_gettime(CLOCK_REALTIME,&time2);
        ///
        sum_time += (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000 ;
        printf("time  : %d  ms \n", (time2.tv_sec-time1.tv_sec)*1000+(time2.tv_nsec-time1.tv_nsec)/1000000);
      
  
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值