In general, it is faster to use a pointer ptr than to use an at if you are traversing the data. Especially under the debug version. Because in Debug, OPENCV will check for an overflow in the coordinates in at, which is very time consuming.
The code is as follows
#include <opencv2\core\core.hpp>#include<opencv2\highgui\highgui.hpp>#include<iostream>intMain () {Char*srcname="e://vedio//001.jpg"; Char*dstname="e://vedio//001dst.jpg"; Cv::mat src=Cv::imread (Srcname,cv::imread_color); Cv::mat DST=Src.clone (); intnchannels=src.channels (); intntimes= +; Doublet=Cv::gettickcount (); for(intI=0; i<ntimes;i++) { for(intR=0; r<src.rows;r++) { for(intC=0; c<src.cols;c++) {dst.at<cv::Vec3b> (r,c) =cv::vec3b (src.at<cv::vec3b> (R,C) [0]/2,src.at<cv::vec3b> (R,C) [1]/2,src.at<cv::vec3b> (R,C) [2]/2);; } }} t= (Cv::gettickcount ()-T)/cv::gettickfrequency (); Std::cout<<"the 1th method consumes time as"<<t<<"seconds"<<Std::endl; Uchar*input,*output; T=Cv::gettickcount (); for(intI=0; i<ntimes;i++) { for(intR=0; r<src.rows;r++) {input=src.ptr<uchar>(R); Output=src.ptr<uchar>(R); for(intC=0; c<src.cols*nchannels;c++) {Output[c]=input[c]/2; } }} t= (Cv::gettickcount ()-T)/cv::gettickfrequency (); Std::cout<<"the 2nd method consumes time as"<<t<<"seconds"<<Std::endl; T=Cv::gettickcount (); for(intI=0; i<ntimes;i++) { for(intR=0; r<src.rows;r++) {input=src.ptr<uchar>(R); Output=src.ptr<uchar>(R); for(intC=0; c<src.cols*nchannels;c++) { *output++=*input++/2; } }} t= (Cv::gettickcount ()-T)/cv::gettickfrequency (); Std::cout<<"the 3rd method consumes time as"<<t<<"seconds"<<Std::endl; intNrows=src.rows; intncols=Src.cols; if(Src.iscontinuous ()) {NCols*=nRows; NRows=1; } t=Cv::gettickcount (); for(intI=0; i<ntimes;i++) { for(intR=0; r<nrows;r++) {input=src.ptr<uchar>(R); Output=src.ptr<uchar>(R); for(intC=0; c<ncols*nchannels;c++) { *output++=*input++/2; } }} t= (Cv::gettickcount ()-T)/cv::gettickfrequency (); Std::cout<<"the 4th method consumes time as"<<t<<"seconds"<<Std::endl; return 0; }
View Code
OpenCV several methods of accessing Cv::mat data