intmain( int argc, char* argv[]) { if (argc != 2) { cout << "input parameters failed!" << endl; return-1; }
Mat I; I = imread(argv[1], IMREAD_COLOR);
if (I.empty()) { cout << "The image" << argv[1] << " could not be loaded." << endl; return-1; }
constint times = 100; double t = 0;
t = (double)getTickCount();
for (int i = 0; i < times; ++i) { cv::Mat clone_i = I.clone(); scan_image_c(clone_i); }
t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times;
cout << "Time of scan_image_c (averaged for " << times << " runs): " << t << " ms."<< endl;
t = (double)getTickCount();
for (int i = 0; i < times; ++i) { cv::Mat clone_i = I.clone(); scan_image_iterator(clone_i); }
t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times;
cout << "Time of scan_image_iterator (averaged for " << times << " runs): " << t << " ms."<< endl;
t = (double)getTickCount();
for (int i = 0; i < times; ++i) { cv::Mat clone_i = I.clone(); scan_image_random(clone_i); }
t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times;
cout << "Time of scan_image_random (averaged for " << times << " runs): " << t << " ms."<< endl;
return0; }
intscan_image_c(Mat &I) { int channels = I.channels(); if (channels != 3) { printf("test support only three channel.\n"); return-1; }
for (int i = 0; i < I.rows; i++) { Vec3b *ptr = I.ptr<Vec3b>(i); for (int j = 0; j < I.cols; j++) { ptr[j][0] = 0; ptr[j][1] = 0; ptr[j][2] = 0; } } return0; }
intscan_image_iterator(Mat &I) { int channels = I.channels(); if (channels != 3) { printf("test support only three channel.\n"); return-1; }
MatIterator_<Vec3b> it, end; for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it) { (*it)[0] = 0; (*it)[1] = 0; (*it)[2] = 0; } return0; }
intscan_image_random(Mat &I) { int channels = I.channels(); if (channels != 3) { printf("test support only three channel.\n"); return-1; }
for( int i = 0; i < I.rows; ++i) { for( int j = 0; j < I.cols; ++j { I.at<Vec3b>(i, j)[0] = 0; I.at<Vec3b>(i, j)[1] = 0; I.at<Vec3b>(i, j)[2] = 0; } }
return0; }
运行结果
运行结果如下:
1 2 3
Time of scan_image_c (averaged for 100 runs): 2.04884 ms. Time of scan_image_iterator (averaged for 100 runs): 4.77701 ms. Time of scan_image_random (averaged for 100 runs): 3.64237 ms.