对OpenCV中的Mat矩阵的复制,创建等操作的时间消耗
代码如下:
clock_t t1,t2;
t1 = clock();
Mat mat1(10000,5000,CV_64FC1,Scalar::all(0.0));
t2 = clock();
cout<<"Matmat1(10000,5000,CV_64FC1,Scalar::all(0.0))\t"<
{
t1 = clock();
Mat mat2(mat1);
t2 = clock();
cout<<"Mat mat2(mat1)\t"<
{
Mat mat3(500,500,CV_64FC1,Scalar::all(0.0));
t1 = clock();
mat3 = mat1;
t2 = clock();
cout<<"mat3 = mat1;\t"<
{
Mat mat4;
t1 = clock();
mat1.copyTo(mat4);
t2 = clock();
cout<<"mat1.copyTo(mat4);\t"<
{
Mat mat4;
t1 = clock();
mat4 = mat1.clone();
t2 = clock();
cout<<"mat4 =mat1.clone();\t"<
{
Mat mat5(10000,5000,CV_32FC1,Scalar::all(0.0));
t1 = clock();
mat1.convertTo(mat5,CV_32FC1);
t2 = clock();
cout<<"mat1.convertTo(mat5,CV_32FC1);\t"<
一个典型的输出。
Mat mat1(10000,5000,CV_64FC1,Scalar::all(0.0)) 540
Mat mat2(mat1) 0
mat3 = mat1; 1
mat1.copyTo(mat4); 515
mat4 =mat1.clone(); 465
mat1.convertTo(mat5,CV_32FC1); 488
另一个运行的输出。
Mat mat1(10000,5000,CV_64FC1,Scalar::all(0.0)) 391
Mat mat2(mat1) 0
mat3 = mat1; 0
mat1.copyTo(mat4); 433
mat4 =mat1.clone(); 453
mat1.convertTo(mat5,CV_32FC1); 495
从结果中可以看到,拷贝构造函数和赋值的时间可以忽略不计,但Mat::copyTo、Mat::clone、Mat::convertTo等需要申请空间或复制数据的操作就需要大量的时间。
附:
for (std::vector::iterator iter = vecSrcImg.begin(); iter !=vecSrcImg.end(); iter++)
{
t1 = (double)cvGetTickCount();
Mat srcMat( srcImg.rows, srcImg.cols, CV_8UC(1), Scalar::all(0));
//12ms
Mat srcMat(srcMatTemp);
//0.2ms
srcMat = *iter;
//10ms
//Mat dstMat(srcImg.rows, srcImg.cols, CV_8UC(1),Scalar::all(0));//12ms
Mat dstMat(srcMatTemp);//0.2ms
Gassian_Histogram(srcMat, dstMat, 2.0);
unsigned char* imgData = srcMat.data;
Gassian_like_histogram(imgData, srcImg.cols, srcImg.rows, 2.0);
tempT = (double)cvGetTickCount() - t1;
printf( " histogramGaussian cost time = %gms\n",tempT/((double)cvGetTickFrequency()*1000.) );
dstMat.data = imgData;
vecDstImg.push_back(dstMat);
}