我正在尝试比较OpenCV 3.0版中灰度图像的扫描图像像素的运行时间。我已经阅读了OpenCV文档how_to_scan_images
我理解cv::convertTo
因各种优化而成为最快的。我也明白cpointer
样式方法将是第二好的。但是,我对MatIterator
(method3)和Mat::at
运算符(方法2)之间的差异感到惊讶。文档how_to_scan_images提到MatIterator
应该比Mat::at<>
运算符更快,但我收到的结果却不同。
我错过了什么吗?这个结果有望吗?
在算法开发的初始阶段,我想使用MatIterator,因为它与STL迭代器相似,通常被认为是一种更安全的方式。
关于我做错的任何想法?我在Ubuntu上使用OpenCV 3.0。对于多次运行,下面显示的输出在+ - 0.5 ms内大致一致。
在下面的代码中,我有效地计算(newImage = alpha*oldImage +beta
)并比较以下方法的性能
方法1:使用cv:: convertTo
,时间以ms为单位= 0.709923
方法2:使用img.at<uchar>(row,col)
,时间以ms为单位= 5.09625
方法3:使用MatIterator_<uchar> it
,时间ms = 18.277
方法4:cpointer uchar * p = img.ptr<uchar>(row)
,时间ms = 3.49983
方法5:cpointer uchar * src = img.data
,时间ms = 3.28267
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace cv;
int main(int argc, char ** argv)
{
string imgname("baboon.jpg");
if (argc>1)
imgname = argv[1];
Mat im = imread(imgname.c_str(), IMREAD_COLOR);
if (im.empty())
{ cout<<" Invalid image read, imgname =argv[1] = "<<imgname<<endl;
return -1;
}
Mat img;
cvtColor(im, img,COLOR_BGR2GRAY);
// method 1: using convertTo
Mat img1;
double alpha =1, beta =50;
double t = (double)(getTickCount());
img.convertTo(img1, img.type(),alpha, beta);
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"Method 1: using convertTo, Time elasped in ms = "<<t*1000<<endl;
namedWindow("img1", WINDOW_AUTOSIZE);
imshow("img1", img1);
// Method2: using img.at<>(row, col) index
Mat img2 = Mat::zeros(img.size(), img.type());
t = (double)(getTickCount());
for (int row=0; row<img.rows; row++)
{ for (int col =0; col<img.cols; col++)
{ img2.at<uchar>(row,col) = (saturate_cast<uchar>) (alpha*img.at<uchar> (row,col) + beta);
}
}
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"Method2: using img.at<uchar>(row,col), Time in ms = "<<t*1000<<endl;
//Method3: using Matiterator for each image point
Mat img3 = Mat::zeros(img.size(),img.type());
t = (double)(getTickCount());
MatIterator_<uchar> it, itDest, end;
itDest= img3.begin<uchar>(); end =img.end<uchar>();
for (it = img.begin<uchar>(); it!= end; it++,itDest++)
{ *itDest = (saturate_cast<uchar>)((*it)*alpha + beta);
}
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"Method3: using MatIterator_<uchar> it, Time in ms = "<<t*1000<<endl;
//Method4: using c-style pointer, char * p = img.ptr<uchar>(rowNum)
Mat img4 = Mat::zeros(img.size(), img.type());
t = (double)(getTickCount());
for (int row =0; row<img.rows; row++)
{ uchar * srcPtr = img.ptr<uchar>(row);
uchar * destPtr = img4.ptr<uchar>(row);
for (int col =0; col<img.cols; col++)
{ destPtr[col] = (saturate_cast<uchar>) (alpha* srcPtr[col] + beta);
}
}
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"Method4: cpointer uchar * p = img.ptr<uchar>(row), Time in ms = "<<t*1000<<endl;
// method 5: using the address given by img.data() and iterating untill the end
Mat img5 = Mat::zeros(img.size(), img.type());
t = (double)(getTickCount());
uchar * src = img.data;
uchar * dest =img5.data;
for (int i=0; i<img.rows*img.cols; i++)
*(dest++) = (saturate_cast<uchar>)(alpha * (*(src++)) + beta);
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"Method5: cpointer uchar * src = img.data, Time in ms = "<<t*1000<<endl;
return 0;
}
答案 0 :(得分:1)
在我自己挖掘之后是结论: 基于性能(从最高到最低)的图像扫描方法比较
仅在发布模式下“Mat :: at”比“MatIterator”更快。 MatIterator通过附加检查带来安全系数。但是,在调试模式下,“MatIterator”比“Mat :: at”运算符
更快