Advertisement
bkit4s0

[scan image]

Apr 25th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. using namespace cv;
  7.  
  8. Mat& ScanImageAndReduceC(Mat& I, const uchar* table);
  9. Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);
  10. Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);
  11.  
  12. int main(int argc,char** argv)
  13. {
  14.     if (argc < 3)
  15.     {
  16.         cout << "Not enough parameters" << endl;
  17.         return -1;
  18.     }
  19.  
  20.     Mat I, J;
  21.     if( argc == 4 && !strcmp(argv[3],"G") )
  22.         I = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
  23.     else
  24.         I = imread(argv[1], CV_LOAD_IMAGE_COLOR);
  25.  
  26.     if (!I.data)
  27.     {
  28.         cout << "The image" << argv[1] << " could not be loaded." << endl;
  29.         return -1;
  30.     }
  31.     int divideWith = 0; // convert our input string to number - C++ style
  32.     stringstream s;
  33.     s << argv[2];
  34.     s >> divideWith;
  35.     if (!s || !divideWith)
  36.     {
  37.         cout << "Invalid number entered for dividing. " << endl;
  38.         return -1;
  39.     }
  40.  
  41.     uchar table[256];
  42.     for (int i = 0; i < 256; ++i)
  43.        table[i] = (uchar)(divideWith * (i/divideWith));
  44.     //------------------------------------------
  45.     const int times = 100;
  46.     double t;
  47.     t = (double)getTickCount();
  48.     for (int i = 0; i < times; ++i)
  49.     {
  50.         cv::Mat clone_i = I.clone();
  51.         J = ScanImageAndReduceC(clone_i, table);
  52.     }
  53.  
  54.     t = 1000*((double)getTickCount() - t)/getTickFrequency();
  55.     t /= times;
  56.  
  57.     cout << "Time of reducing with the C operator [] (averaged for "
  58.          << times << " runs): " << t << " milliseconds."<< endl;
  59.     //------------------------------------------
  60.     t = (double)getTickCount();
  61.  
  62.     for (int i = 0; i < times; ++i)
  63.     {
  64.         cv::Mat clone_i = I.clone();
  65.         //J = ScanImageAndReduceIterator(clone_i, table);
  66.     }
  67.  
  68.     t = 1000*((double)getTickCount() - t)/getTickFrequency();
  69.     t /= times;
  70.  
  71.     cout << "Time of reducing with the iterator (averaged for "
  72.         << times << " runs): " << t << " milliseconds."<< endl;
  73.     //------------------------------------------
  74.     t = (double)getTickCount();
  75.  
  76.     for (int i = 0; i < times; ++i)
  77.     {
  78.         cv::Mat clone_i = I.clone();
  79.         //ScanImageAndReduceRandomAccess(clone_i, table);
  80.     }
  81.  
  82.     t = 1000*((double)getTickCount() - t)/getTickFrequency();
  83.     t /= times;
  84.  
  85.     cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "
  86.         << times << " runs): " << t << " milliseconds."<< endl;
  87.     //------------------------------------------
  88.     Mat lookUpTable(1, 256, CV_8U);
  89.     uchar* p = lookUpTable.data;
  90.     for( int i = 0; i < 256; ++i)
  91.         p[i] = table[i];
  92.  
  93.     t = (double)getTickCount();
  94.  
  95.     for (int i = 0; i < times; ++i)
  96.         LUT(I, lookUpTable, J);
  97.  
  98.     t = 1000*((double)getTickCount() - t)/getTickFrequency();
  99.     t /= times;
  100.  
  101.     cout << "Time of reducing with the LUT function (averaged for "
  102.         << times << " runs): " << t << " milliseconds."<< endl;
  103.     //------------------------------------------
  104.     waitKey(0);
  105.     //system("pause");
  106.     return 0;
  107. }
  108. Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
  109. {
  110.     // accept only char type matrices
  111.     CV_Assert(I.depth() != sizeof(uchar));
  112.  
  113.     int channels = I.channels();
  114.  
  115.     int nRows = I.rows;
  116.     int nCols = I.cols * channels;
  117.  
  118.     if (I.isContinuous())
  119.     {
  120.         nCols *= nRows;
  121.         nRows = 1;
  122.     }
  123.  
  124.     int i,j;
  125.     uchar* p;
  126.     for( i = 0; i < nRows; ++i)
  127.     {
  128.         p = I.ptr<uchar>(i);
  129.         for ( j = 0; j < nCols; ++j)
  130.         {
  131.             p[j] = table[p[j]];
  132.         }
  133.     }
  134.     // show image here !!
  135.     namedWindow("n", WINDOW_AUTOSIZE);
  136.     imshow("n", I);
  137.  
  138.     return I;
  139. }
  140. Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
  141. {
  142.     // accept only char type matrices
  143.     CV_Assert(I.depth() != sizeof(uchar));
  144.  
  145.     const int channels = I.channels();
  146.     switch(channels)
  147.     {
  148.     case 1:
  149.         {
  150.             MatIterator_<uchar> it, end;
  151.             for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
  152.                 *it = table[*it];
  153.             break;
  154.         }
  155.     case 3:
  156.         {
  157.             MatIterator_<Vec3b> it, end;
  158.             for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
  159.             {
  160.                 (*it)[0] = table[(*it)[0]];
  161.                 (*it)[1] = table[(*it)[1]];
  162.                 (*it)[2] = table[(*it)[2]];
  163.             }
  164.         }
  165.     }
  166.  
  167.     return I;
  168. }
  169. Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
  170. {
  171.     // accept only char type matrices
  172.     CV_Assert(I.depth() != sizeof(uchar));
  173.  
  174.     const int channels = I.channels();
  175.     switch(channels)
  176.     {
  177.     case 1:
  178.         {
  179.             for( int i = 0; i < I.rows; ++i)
  180.                 for( int j = 0; j < I.cols; ++j )
  181.                     I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];
  182.             break;
  183.         }
  184.     case 3:
  185.         {
  186.          Mat_<Vec3b> _I = I;
  187.  
  188.          for( int i = 0; i < I.rows; ++i)
  189.             for( int j = 0; j < I.cols; ++j )
  190.                {
  191.                    _I(i,j)[0] = table[_I(i,j)[0]];
  192.                    _I(i,j)[1] = table[_I(i,j)[1]];
  193.                    _I(i,j)[2] = table[_I(i,j)[2]];
  194.             }
  195.          I = _I;
  196.          break;
  197.         }
  198.     }
  199.  
  200.     return I;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement