The operation of the pixel using a total of five methods, timing and comparison, the final velocity Opencv Copy mode is the fastest, followed by a pointer traversal.
#include " opencv2 \ opencv.hpp " #include <iostream> a using namespace std; a using namespace CV; BOOL copyByArrayWay (Mat srcImg, Mat & resultImg); // array at traversal BOOL copyByRowPtrWay (Mat srcImg, Mat & resultImg); // row pointer traversal BOOL copyByPtrWay (Mat srcImg, Mat & resultImg); // pointer traversal BOOL copyByIteratorWay (Mat srcImg, Mat & resultImg); // iteratively BOOL copyByOpenCv (Mat srcImg, Mat & resultImg); // OpenCv Copy mode int main ( int argv, char** argc) { Mat src = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\lena.jpg"); if (src.empty()) { printf("Could not load image...\n"); return -1; } int64 startTime, endTime; double usedTime; ////数组遍历方式 Mat arrayWayResult = Mat::zeros(src.size(), src.type()); startTime = getTickCount(); copyByArrayWay(src, arrayWayResult); endTime =GetTickCount (); usedTime = (endTime - the startTime) / getTickFrequency () * 1000 ; COUT << " array traversal Processed: " << << usedTime " ms " << endl; imshow ( " arrayWay " , arrayWayResult) ; /// / row pointer traversal Mat rowPtrWayResult = Mat :: zeros (src.size (), src.type ()); the startTime = GetTickCount (); copyByRowPtrWay (the src, rowPtrWayResult); endTime = GetTickCount (); usedTime= (EndTime - the startTime) / getTickFrequency () * 1000 ; COUT << " row pointer traversal Processed: " << << usedTime " ms " << endl; imshow ( " rowPtrWay " , rowPtrWayResult); /// / traversing pointers embodiment Mat ptrWayResult = Mat :: zeros (src.size (), src.type ()); the startTime = GetTickCount (); copyByPtrWay (the src, ptrWayResult); endTime = GetTickCount (); usedTime = (endTime - the startTime) / getTickFrequency () * 1000 ; COUT << " pointer traversal consuming way: " <<usedTime << " ms " << endl; imshow ( " ptrWay " , ptrWayResult); /// / iteratively traversing Mat iterWayResult = Mat :: zeros (src.size ( ), src.type ()); the startTime = GetTickCount (); copyByIteratorWay (the src, iterWayResult); endTime = GetTickCount (); usedTime = (endTime - the startTime) / getTickFrequency () * 1000 ; COUT << " iterative manner Processed : " << << usedTime" Ms" << endl; imshow("iterWay", iterWayResult); ////OpenCv copy方式 Mat copyWayResult = Mat::zeros(src.size(), src.type()); startTime = getTickCount(); copyByOpenCv(src, copyWayResult); endTime = getTickCount(); usedTime = (endTime - startTime) / getTickFrequency() * 1000; cout << "Opencv copy方式耗时:" << usedTime << "毫秒" << endl; imshow("OpencvCopyWay", copyWayResult); waitKey(0); return 0; } //数组遍历方式 at bool copyByArrayWay(Mat srcImg,Mat &resultImg) { if (srcImg.empty()) { printf("Could not load image...\n"); return false; } int rows = srcImg.rows; int cols = srcImg.cols; int ch = srcImg.channels(); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols;col++) { if (ch == 3) { resultImg.at<Vec3b>(row, col) = srcImg.at<Vec3b>(row,col); } else if (ch == 1) { resultImg.at<uchar>(row, col) = srcImg.at<uchar>(row, col); } } } return true; } //行指针遍历方式 bool copyByRowPtrWay(Mat srcImg, Mat &resultImg) { if (srcImg.empty()) { printf("Could not load image...\n"); return false; } int rows = srcImg.rows; int rowPixelNums = (srcImg.cols) * (srcImg.channels()); for (int row = 0; row < rows; row++) { uchar *srcRowPtr = srcImg.ptr<uchar>(row); uchar *resultRowPtr = resultImg.ptr<uchar>(row); for (int pixelNum = 0; pixelNum < rowPixelNums; pixelNum++) { resultRowPtr[pixelNum] = srcRowPtr[pixelNum]; } } return true; } //指针遍历方式 bool copyByPtrWay(Mat srcImg, Mat &resultImg) { if (srcImg.empty()) { printf("Could not load image...\n"); return false; } int ch = srcImg.channels(); int totalPixelNums = (srcImg.rows) * (srcImg.cols) * ch; uchar *srcRowPtr = srcImg.ptr<uchar>(0); uchar *resultRowPtr = resultImg.ptr<uchar>(0); for (int pixel = 0; pixel < totalPixelNums; pixel++) { resultRowPtr[pixel] = srcRowPtr[pixel]; } return true; } //迭代方式 bool copyByIteratorWay(Mat srcImg, Mat &resultImg) { if (srcImg.empty()) { printf("Could not load image...\n"); return false; } int ch = srcImg.channels(); if (ch == 3) { Mat_<Vec3b>::iterator srcPtrBegin = srcImg.begin<Vec3b>(); Mat_<Vec3b>::iterator outPtrBegin = resultImg.begin<Vec3b>(); Mat_<Vec3b>::iterator srcPtrEnd = srcImg.end<Vec3b>(); while (srcPtrBegin != srcPtrEnd) { *outPtrBegin = *srcPtrBegin; srcPtrBegin++; outPtrBegin++; } return true; } else if (ch == 1) { Mat_<uchar>::iterator srcPtrBegin = srcImg.begin<uchar>(); Mat_<uchar>::iterator outPtrBegin = resultImg.begin<uchar>(); Mat_<uchar>::iterator srcPtrEnd = srcImg.end<uchar>(); while (srcPtrBegin != srcPtrEnd) { *outPtrBegin = *srcPtrBegin; srcPtrBegin++; outPtrBegin++; } return true; } else return false; } //OpenCv copy方式 bool copyByOpenCv(Mat srcImg, Mat &resultImg) { if (srcImg.empty()) { printf("Could not load image...\n"); return false; } srcImg.copyTo(resultImg); return true; }