图像处理---《计算 处理过程 的耗时》

图像处理---《计算 处理过程 的耗时》

  学习知识点:耗时计算
        double time = getTickCount();  //开始计时

        //这里开始,处理图像,略
    
       double timecomsume =(getTickCount()-time)/getTickFrequency();   //总耗时计算
       printf("timecomsume %.5f ", timecomsume);                 
         //cout << "in filter2D, timecomsume= " << timecomsume << "seconds" << endl;

/***************************************************************************************
作者:@WP20190612
环境:VS2010 + OpenCV2.4.3
功能:耗时计算---getTickCount()
说明:   
    知识点: 耗时计算
    double time = getTickCount();                                 //开始计时

    //这里开始,处理图像,略
    
    double timecomsume =(getTickCount()-time)/getTickFrequency(); //总耗时计算
    printf("timecomsume %.5f 
", timecomsume);                   
    //cout << "in filter2D, timecomsume= " << timecomsume << "seconds" << endl;

***************************************************************************************/

//-------------------------------功能:耗时计算 举例------------------------------
//本例子结论:OPenCV自带的filter2D()掩膜函数 速度快于自己写的 掩膜处理方式
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;

int main (int argc, char** argv)  //argumentss 参数;argc命令行参数个数;
{
    Mat src, dst;
    src = imread("D:\work_VS2010\example_opencv\test001.png");
    if (!src.data)
    {
        printf("could not load image ...
");
        return -1;   //return 0 成功完成本函数;return -1 未能完成本函数
    }
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    imshow("input image", src);
    
    //----------------------------开始处理图像---------------------------------------
    
    /*
    double time = getTickCount();

    //方式一:自定义掩膜的公式
    //int cols = src.cols;                     // 单通道
    int cols    = (src.cols-1)*src.channels();  // 多通道图像的行数---图像的宽度
    int offsetX = src.channels();               // 通道数
    int rows    = src.rows;
    
    dst = Mat::zeros(src.size(), src.type());   // 初始化一个与原图像矩阵大小一致的0矩阵,用于存放处理后的图像
    //开始掩膜函数处理
    for (int row=1; row<(rows-1); row++)
    {
        //获取像素的位置指针
        const uchar* current =src.ptr<uchar>(row);    //当前像素的位置指针
        const uchar* previous=src.ptr<uchar>(row-1);  //当前像素之前一个像素的位置指针
        const uchar* next    =src.ptr<uchar>(row+1);  //当前像素之后一个像素的位置指针
        
        uchar* output = dst.ptr<uchar>(row);
        for(int col=offsetX; col<cols; col++)    
        {
            //output[col]=5*current[col]-(current[col-offsetX] + current[col+offsetX] + previous[col] + next[col]);//毛刺效果
            output[col]=saturate_cast<uchar>(5*current[col]-(current[col-offsetX] + current[col+offsetX] + previous[col] + next[col]) );
        }
    }

    double timecomsume =(getTickCount()-time)/getTickFrequency(); //总耗时计算
    printf("timecomsume %.5f 
", timecomsume);                   //0.02085s
    cout << "in filter2D, timecomsume= " << timecomsume << "seconds" << endl;
    */
    
    //方式二 借用API函数
    double time = getTickCount();                                 //开始计时

    Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    filter2D(src, dst, src.depth(), kernel);

    double timecomsume =(getTickCount()-time)/getTickFrequency(); //总耗时计算
    printf("timecomsume %.5f 
", timecomsume);                   //0.00856s
    cout << "in filter2D, timecomsume= " << timecomsume << "seconds" << endl;

    namedWindow("the image after mask", CV_WINDOW_AUTOSIZE );
    imshow("the image after mask",dst);
        
    //保存图像
    imwrite("D:\work_VS2010\example_opencv\test001_result.png", dst);
    
    //----------------------------结束处理图像---------------------------------------
    
    waitKey(0);
    return 0;
}

  另外,在windows下,统计VS中代码的运行时间。还可以使用函数:clock_t clock(void) ,就是该程序从启动到函数调用占用CPU的时间。

这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);

若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型。
 

#include “stdio.h”
#include “stdlib.h”

#include "time.h"//统计时间需添加的头文件

void main()
{
    clock_t start, end;
    double totalTime;
    start = clock();

    ..........;//操作步骤

    end = clock();
    totalTime = (double)(end - start) / CLOCKS_PER_SEC;
    printf("%f seconds
", totalTime);

}



原文地址:https://www.cnblogs.com/carle-09/p/11027944.html