码迷,mamicode.com
首页 > 其他好文 > 详细

opencv 四种图像遍历的时间度量

时间:2017-05-13 17:56:23      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:tor   proc   pre   示例   result   四种   gui   logs   png   

    本文查看.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 四种图像遍历的时间度量

标签:tor   proc   pre   示例   result   四种   gui   logs   png   

原文地址:http://www.cnblogs.com/yunfung/p/6849539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!