opencv 四种图像遍历的时间度量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv 四种图像遍历的时间度量相关的知识,希望对你有一定的参考价值。
本文查看.at、pointers、reshape及iterators四种遍历图像方式所消耗时间。
示例代码:
1 //Test running time,edited by yunfung 2 #include <opencv2/core/core.hpp> 3 #include <opencv2/highgui/highgui.hpp> 4 #include <opencv2/imgproc/imgproc.hpp> 5 #include <iostream> 6 using namespace cv; 7 using namespace std; 8 9 void colorReduce1(Mat& image, int div = 64) 10 { 11 for (int i = 0; i < image.rows; i++) 12 { 13 for (int j = 0; j < image.cols; j++) 14 { 15 image.at<Vec3b>(i, j)[0] = image.at<Vec3b>(i, j)[0] / div*div + div / 2; 16 image.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, j)[1] / div*div + div / 2; 17 image.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, j)[2] / div*div + div / 2; 18 } 19 } 20 } 21 22 void colorReduce2(cv::Mat image, int div = 64) { 23 int nl = image.rows; 24 int nc = image.cols * image.channels(); 25 for (int j = 0; j < nl; j++) { 26 uchar* data = image.ptr<uchar>(j); 27 for (int i = 0; i < nc; i++) { 28 data[i] = data[i] / div*div + div / 2; 29 30 } 31 } 32 } 33 34 void colorReduce3(cv::Mat &image, int div = 64) { 35 if (image.isContinuous()){ 36 image.reshape(1, 1); 37 } 38 int nl = image.rows; 39 int nc = image.cols * image.channels(); 40 for (int j = 0; j < nl; j++) { 41 uchar* data = image.ptr<uchar>(j); 42 for (int i = 0; i < nc; i++) { 43 data[i] = data[i] / div*div + div / 2; 44 } 45 } 46 } 47 48 void colorReduce4(cv::Mat &image, int div = 64) { 49 cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>(); 50 cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>(); 51 for (; it != itend; ++it) { 52 (*it)[0] = (*it)[0] / div * div + div / 2; 53 (*it)[1] = (*it)[1] / div * div + div / 2; 54 (*it)[2] = (*it)[2] / div * div + div / 2; 55 } 56 } 57 58 void calrunTime(int v, Mat&image) 59 { 60 double duration; 61 duration = static_cast<double>(getTickCount()); 62 for (int i = 0; i < 10; i++) 63 { 64 switch (v) 65 { 66 case 1: 67 colorReduce1(image); //at 35.16mS 68 break; 69 case 2: 70 colorReduce2(image); //pointers 1.59mS 71 break; 72 case 3: 73 colorReduce3(image); //reshape(continuous) 1.47mS 74 break; 75 case 4: 76 colorReduce4(image); //iterators 15.71mS 77 break; 78 default: 79 break; 80 } 81 } 82 duration = static_cast<double>(getTickCount()) - duration; 83 duration /= getTickFrequency(); 84 duration *= (1000/10); //average 85 cout << "duration" << v << " : " << duration << "mS"<< endl; 86 } 87 88 int main() 89 { 90 Mat image = imread("test.jpg"); 91 Mat imageClone = image.clone(); 92 calrunTime(1, image); 93 94 image = imread("test.jpg"); 95 imageClone = image.clone(); 96 calrunTime(2, image); 97 image = imread("test.jpg"); 98 imageClone = image.clone(); 99 calrunTime(3, image); 100 image = imread("test.jpg"); 101 imageClone = image.clone(); 102 calrunTime(4, image); 103 104 namedWindow("Image Result"); 105 imshow("Image Result", imageClone); 106 waitKey(); 107 return 0; 108 }
测试图片:
运行结果(at效率最低,指针效率最高):
以上是关于opencv 四种图像遍历的时间度量的主要内容,如果未能解决你的问题,请参考以下文章
在 Python 多处理进程中运行较慢的 OpenCV 代码片段