一个给代码计时的类及测试用例

#include "../inc/opencv/cv.h"
#include "../inc/opencv/cvaux.h"
#include "../inc/opencv/cxcore.h"
#include "../inc/opencv/highgui.h"
using namespace cv;

#pragma comment(lib, "../lib/cv200.lib")
#pragma comment(lib, "../lib/cxcore200.lib")
#pragma comment(lib, "../lib/highgui200.lib")


#include <iostream>
using namespace std;

class Timer
{ 
public: 
	Timer()
	{ 
		QueryPerformanceFrequency(&_frequency); 
	} 

	void Start() 
	{ 
		QueryPerformanceCounter(&_begin); 
	} 

	double End(bool flag) 
	{ 
		QueryPerformanceCounter(&_end); 
		double timeUsed = 1.0 * (_end.QuadPart - _begin.QuadPart) / _frequency.QuadPart;         
		if(flag){     
			printf("Time used : %f s\n", timeUsed); 
		}     
		return timeUsed; 
	} 

private: 
	LARGE_INTEGER _frequency;
	LARGE_INTEGER _begin;
	LARGE_INTEGER _end; 
};


void yjjRgb2Gray(IplImage* Ipic, IplImage* Opic)
{
	for (int i = 0; i < Ipic->height; ++i)
	{
		char* p = Ipic->imageData + i * Ipic->widthStep;
		for (int j = 0; j < Ipic->widthStep/3 ; ++j)
		{
			*(Opic->imageData + i * Opic->widthStep + j) = 
				0.114 * p[j * 3] + 0.587 * p[j * 3 + 1] + 0.229 * p[j * 3 + 2];
		}
	}
}

void tRgb2Gray(IplImage* Ipic, IplImage* Opic)
{
	for (int i = 0; i < Ipic->height; ++i)
	{
		char* p = Ipic->imageData + i * Ipic->widthStep;
		char* q = Opic->imageData + i * Opic->widthStep;
		for (int j = 0; j < Ipic->widthStep/3 ; ++j)
		{
			q[j] = 0.114 * p[j * 3] + 0.587 * p[j * 3 + 1] + 0.229 * p[j * 3 + 2];				
		}
	}
}

int main()
{
	IplImage* pImg = cvLoadImage("P1010622.JPG");
	int height = pImg->height;
	int width = pImg->width;

	IplImage* pGray = cvCreateImage(cvSize(width, height),8,1);

	double t1 = 0, t2 = 0;
	Timer tx;
	for (int i = 0; i < 100; ++i)
	{
		tx.Start();
		yjjRgb2Gray(pImg, pGray);
		t1 += tx.End(false);

		tx.Start();
		tRgb2Gray(pImg, pGray);
		t2 += tx.End(false);
	}
	cvReleaseImage(&pImg);
	cvReleaseImage(&pGray);

	cout << t1 << endl << t2 << endl;
	system("pause");
	return 0;
}


上面的这个测试跑出来结果表明数组比指针效率更高


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值