From 577dabb8bcfd27a741fea356d81843b8a63071e3 Mon Sep 17 00:00:00 2001 From: Marina Kolpakova Date: Tue, 20 Mar 2012 09:44:40 +0000 Subject: [PATCH] fixed bug #1571 --- .../how_to_scan_images/how_to_scan_images.cpp | 101 ++++++++++-------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/samples/cpp/tutorial_code/core/how_to_scan_images/how_to_scan_images.cpp b/samples/cpp/tutorial_code/core/how_to_scan_images/how_to_scan_images.cpp index bce6a5c09..b020c5ec1 100644 --- a/samples/cpp/tutorial_code/core/how_to_scan_images/how_to_scan_images.cpp +++ b/samples/cpp/tutorial_code/core/how_to_scan_images/how_to_scan_images.cpp @@ -3,7 +3,7 @@ #include #include -using namespace std; +using namespace std; using namespace cv; void help() @@ -16,7 +16,7 @@ void help() << "Usage:" << endl << "./howToScanImages imageNameToUse divideWith [G]" << endl << "if you add a G parameter the image is processed in gray scale" << endl - << "--------------------------------------------------------------------------" << endl + << "--------------------------------------------------------------------------" << endl << endl; } @@ -26,11 +26,11 @@ Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table); int main( int argc, char* argv[]) { - help(); + help(); if (argc < 3) { cout << "Not enough parameters" << endl; - return -1; + return -1; } Mat I, J; @@ -51,115 +51,124 @@ int main( int argc, char* argv[]) s >> divideWith; if (!s) { - cout << "Invalid number entered for dividing. " << endl; + cout << "Invalid number entered for dividing. " << endl; return -1; } - - uchar table[256]; + + uchar table[256]; for (int i = 0; i < 256; ++i) table[i] = divideWith* (i/divideWith); - const int times = 100; + const int times = 100; double t; - t = (double)getTickCount(); - + t = (double)getTickCount(); + for (int i = 0; i < times; ++i) - J = ScanImageAndReduceC(I.clone(), table); + { + cv::Mat clone_i = I.clone(); + J = ScanImageAndReduceC(clone_i, table); + } t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times; - cout << "Time of reducing with the C operator [] (averaged for " - << times << " runs): " << t << " milliseconds."<< endl; + cout << "Time of reducing with the C operator [] (averaged for " + << times << " runs): " << t << " milliseconds."<< endl; - t = (double)getTickCount(); + t = (double)getTickCount(); for (int i = 0; i < times; ++i) - J = ScanImageAndReduceIterator(I.clone(), table); + { + cv::Mat clone_i = I.clone(); + J = ScanImageAndReduceIterator(clone_i, table); + } t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times; - cout << "Time of reducing with the iterator (averaged for " - << times << " runs): " << t << " milliseconds."<< endl; + cout << "Time of reducing with the iterator (averaged for " + << times << " runs): " << t << " milliseconds."<< endl; - t = (double)getTickCount(); + t = (double)getTickCount(); for (int i = 0; i < times; ++i) - ScanImageAndReduceRandomAccess(I.clone(), table); + { + cv::Mat clone_i = I.clone(); + ScanImageAndReduceRandomAccess(clone_i, table); + } t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times; - cout << "Time of reducing with the on-the-fly address generation - at function (averaged for " - << times << " runs): " << t << " milliseconds."<< endl; + cout << "Time of reducing with the on-the-fly address generation - at function (averaged for " + << times << " runs): " << t << " milliseconds."<< endl; Mat lookUpTable(1, 256, CV_8U); - uchar* p = lookUpTable.data; + uchar* p = lookUpTable.data; for( int i = 0; i < 256; ++i) p[i] = table[i]; - t = (double)getTickCount(); - + t = (double)getTickCount(); + for (int i = 0; i < times; ++i) LUT(I, lookUpTable, J); t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times; - cout << "Time of reducing with the LUT function (averaged for " - << times << " runs): " << t << " milliseconds."<< endl; - return 0; + cout << "Time of reducing with the LUT function (averaged for " + << times << " runs): " << t << " milliseconds."<< endl; + return 0; } Mat& ScanImageAndReduceC(Mat& I, const uchar* const table) { // accept only char type matrices - CV_Assert(I.depth() != sizeof(uchar)); + CV_Assert(I.depth() != sizeof(uchar)); int channels = I.channels(); - int nRows = I.rows * channels; + int nRows = I.rows * channels; int nCols = I.cols; if (I.isContinuous()) { nCols *= nRows; - nRows = 1; + nRows = 1; } int i,j; - uchar* p; + uchar* p; for( i = 0; i < nRows; ++i) { p = I.ptr(i); for ( j = 0; j < nCols; ++j) { - p[j] = table[p[j]]; + p[j] = table[p[j]]; } } - return I; + return I; } Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table) { // accept only char type matrices - CV_Assert(I.depth() != sizeof(uchar)); - + CV_Assert(I.depth() != sizeof(uchar)); + const int channels = I.channels(); switch(channels) { - case 1: + case 1: { - MatIterator_ it, end; + MatIterator_ it, end; for( it = I.begin(), end = I.end(); it != end; ++it) *it = table[*it]; break; } - case 3: + case 3: { - MatIterator_ it, end; + MatIterator_ it, end; for( it = I.begin(), end = I.end(); it != end; ++it) { (*it)[0] = table[(*it)[0]]; @@ -168,29 +177,29 @@ Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table) } } } - - return I; + + return I; } Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table) { // accept only char type matrices - CV_Assert(I.depth() != sizeof(uchar)); + CV_Assert(I.depth() != sizeof(uchar)); const int channels = I.channels(); switch(channels) { - case 1: + case 1: { for( int i = 0; i < I.rows; ++i) for( int j = 0; j < I.cols; ++j ) I.at(i,j) = table[I.at(i,j)]; break; } - case 3: + case 3: { Mat_ _I = I; - + for( int i = 0; i < I.rows; ++i) for( int j = 0; j < I.cols; ++j ) { @@ -202,6 +211,6 @@ Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table) break; } } - + return I; } \ No newline at end of file