作者:破晓
QQ:358892790
时间:2013-08-14
图像处理过程中常常需要访问图像数据,在要求实时性的项目中,一个高效的图像的访问方式就显得尤为重要,下面测试了vector,array及Mat存储数据情况下的不同访问方式的效率,代码如下:
// 需要添加以下两个头文件
#include <MMSystem.h>
#pragma comment(lib,"Winmm.lib")
vector<float> test;
float test_array[10000];
Mat img(5000,5000,CV_32FC1);
test.resize(10000);
float test_sum = 0;
CString str;
LARGE_INTEGER privious, current;
LARGE_INTEGER frequence;
QueryPerformanceFrequency( &frequence);
for (int i = 0;i<10000;++i)
{
test.push_back(1.777);
test_array[i] = 1.88;
}
QueryPerformanceCounter( &privious );
vector<float>::iterator iter;
for (iter=test.begin();iter!=test.end();iter++)
{
float t = *iter;
test_sum += t;
}
QueryPerformanceCounter( ¤t );
str.Format("vector iterator----------------------%f--------------------------\n",1000*(current.QuadPart-privious.QuadPart)/(double)frequence.QuadPart);
OutputDebugString(str);
QueryPerformanceCounter( &privious );
for (int i = 0;i<test.size();++i)
{
float t = test.at(i);
test_sum += t;
}
QueryPerformanceCounter( ¤t );
str.Format("vector at----------------------%f--------------------------\n",1000*(current.QuadPart-privious.QuadPart)/(double)frequence.QuadPart);
OutputDebugString(str);
QueryPerformanceCounter( &privious );
for (int i = 0;i<10000;++i)
{
test_array[i] = 1.99;
}
QueryPerformanceCounter( ¤t );
str.Format("array----------------------%lf--------------------------\n",1000*(current.QuadPart-privious.QuadPart)/(double)frequence.QuadPart);
OutputDebugString(str);
QueryPerformanceCounter( &privious );
for (int h = 0;h<img.rows;++h)
{
for (int w = 0;w<img.cols;++w)
{
img.at<float>(h,w) = 1.99;
}
}
QueryPerformanceCounter( ¤t );
str.Format("Mat at<short>(h,w)----------------------%f--------------------------\n",1000*(current.QuadPart-privious.QuadPart)/(double)frequence.QuadPart);
OutputDebugString(str);
QueryPerformanceCounter( &privious );
float *p = NULL;
for (int h = 0;h<img.rows;++h)
{
p = img.ptr<float>(h);
for (int w = 0;w<img.cols;++w)
{
p[w] = 1.99;
}
}
QueryPerformanceCounter( ¤t );
str.Format("Mat img.ptr<float>(h)----------------------%f--------------------------\n",1000*(current.QuadPart-privious.QuadPart)/(double)frequence.QuadPart);
OutputDebugString(str);
QueryPerformanceCounter( &privious );
float *magDataP = NULL;
int occIndex = 0;
int step = img.step/sizeof(float);
magDataP= (float*)img.data;
for (int h = 0;h<img.rows;++h)
{
for (int w = 0;w<img.cols;++w)
{
occIndex = h*step +w;
magDataP[occIndex] = 1.99;
}
}
QueryPerformanceCounter( ¤t );
str.Format("Mat img.data;----------------------%f--------------------------\n",1000*(current.QuadPart-privious.QuadPart)/(double)frequence.QuadPart);
OutputDebugString(str);
测试结果:
-DEBUG模式
vector iterator----------------------30.301132--------------------------
vector at----------------------27.345137--------------------------
array----------------------0.027407--------------------------
Mat at<short>(h,w)----------------------1790.321925--------------------------
Mat img.ptr<float>(h)----------------------41.638419--------------------------
Mat img.data;----------------------54.197859--------------------------
-RELEASE模式
vector iterator----------------------0.237624--------------------------
vector at----------------------0.150586--------------------------
array----------------------0.000301--------------------------
Mat at<short>(h,w)----------------------68.487226--------------------------
Mat img.ptr<float>(h)----------------------19.388496--------------------------
Mat img.data;----------------------13.896339--------------------------
结论:
1、数组访问的效率高于vector。
2、release模式下,先获取Mat的数据指针再访问(最后一种Mat访问方式)效率最高。