#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;
}
上面的这个测试跑出来结果表明数组比指针效率更高

本文通过实验证明了在C++中使用数组相较于指针,在进行RGB到灰度图像转换任务时表现出更高的效率。通过具体代码实现和性能测试,对比了两种数据结构在图像处理应用中的实际表现。
527

被折叠的 条评论
为什么被折叠?



