Commit 577dabb8 authored by Marina Kolpakova's avatar Marina Kolpakova

fixed bug #1571

parent f1cf411f
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
using namespace std; using namespace std;
using namespace cv; using namespace cv;
void help() void help()
...@@ -16,7 +16,7 @@ void help() ...@@ -16,7 +16,7 @@ void help()
<< "Usage:" << endl << "Usage:" << endl
<< "./howToScanImages imageNameToUse divideWith [G]" << endl << "./howToScanImages imageNameToUse divideWith [G]" << endl
<< "if you add a G parameter the image is processed in gray scale" << endl << "if you add a G parameter the image is processed in gray scale" << endl
<< "--------------------------------------------------------------------------" << endl << "--------------------------------------------------------------------------" << endl
<< endl; << endl;
} }
...@@ -26,11 +26,11 @@ Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table); ...@@ -26,11 +26,11 @@ Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);
int main( int argc, char* argv[]) int main( int argc, char* argv[])
{ {
help(); help();
if (argc < 3) if (argc < 3)
{ {
cout << "Not enough parameters" << endl; cout << "Not enough parameters" << endl;
return -1; return -1;
} }
Mat I, J; Mat I, J;
...@@ -51,115 +51,124 @@ int main( int argc, char* argv[]) ...@@ -51,115 +51,124 @@ int main( int argc, char* argv[])
s >> divideWith; s >> divideWith;
if (!s) if (!s)
{ {
cout << "Invalid number entered for dividing. " << endl; cout << "Invalid number entered for dividing. " << endl;
return -1; return -1;
} }
uchar table[256]; uchar table[256];
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
table[i] = divideWith* (i/divideWith); table[i] = divideWith* (i/divideWith);
const int times = 100; const int times = 100;
double t; double t;
t = (double)getTickCount(); t = (double)getTickCount();
for (int i = 0; i < times; ++i) 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 = 1000*((double)getTickCount() - t)/getTickFrequency();
t /= times; t /= times;
cout << "Time of reducing with the C operator [] (averaged for " cout << "Time of reducing with the C operator [] (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl; << times << " runs): " << t << " milliseconds."<< endl;
t = (double)getTickCount(); t = (double)getTickCount();
for (int i = 0; i < times; ++i) 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 = 1000*((double)getTickCount() - t)/getTickFrequency();
t /= times; t /= times;
cout << "Time of reducing with the iterator (averaged for " cout << "Time of reducing with the iterator (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl; << times << " runs): " << t << " milliseconds."<< endl;
t = (double)getTickCount(); t = (double)getTickCount();
for (int i = 0; i < times; ++i) 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 = 1000*((double)getTickCount() - t)/getTickFrequency();
t /= times; t /= times;
cout << "Time of reducing with the on-the-fly address generation - at function (averaged for " cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl; << times << " runs): " << t << " milliseconds."<< endl;
Mat lookUpTable(1, 256, CV_8U); Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.data; uchar* p = lookUpTable.data;
for( int i = 0; i < 256; ++i) for( int i = 0; i < 256; ++i)
p[i] = table[i]; p[i] = table[i];
t = (double)getTickCount(); t = (double)getTickCount();
for (int i = 0; i < times; ++i) for (int i = 0; i < times; ++i)
LUT(I, lookUpTable, J); LUT(I, lookUpTable, J);
t = 1000*((double)getTickCount() - t)/getTickFrequency(); t = 1000*((double)getTickCount() - t)/getTickFrequency();
t /= times; t /= times;
cout << "Time of reducing with the LUT function (averaged for " cout << "Time of reducing with the LUT function (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl; << times << " runs): " << t << " milliseconds."<< endl;
return 0; return 0;
} }
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table) Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{ {
// accept only char type matrices // accept only char type matrices
CV_Assert(I.depth() != sizeof(uchar)); CV_Assert(I.depth() != sizeof(uchar));
int channels = I.channels(); int channels = I.channels();
int nRows = I.rows * channels; int nRows = I.rows * channels;
int nCols = I.cols; int nCols = I.cols;
if (I.isContinuous()) if (I.isContinuous())
{ {
nCols *= nRows; nCols *= nRows;
nRows = 1; nRows = 1;
} }
int i,j; int i,j;
uchar* p; uchar* p;
for( i = 0; i < nRows; ++i) for( i = 0; i < nRows; ++i)
{ {
p = I.ptr<uchar>(i); p = I.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j) 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) Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{ {
// accept only char type matrices // accept only char type matrices
CV_Assert(I.depth() != sizeof(uchar)); CV_Assert(I.depth() != sizeof(uchar));
const int channels = I.channels(); const int channels = I.channels();
switch(channels) switch(channels)
{ {
case 1: case 1:
{ {
MatIterator_<uchar> it, end; MatIterator_<uchar> it, end;
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it) for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
*it = table[*it]; *it = table[*it];
break; break;
} }
case 3: case 3:
{ {
MatIterator_<Vec3b> it, end; MatIterator_<Vec3b> it, end;
for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it) for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
{ {
(*it)[0] = table[(*it)[0]]; (*it)[0] = table[(*it)[0]];
...@@ -168,29 +177,29 @@ Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table) ...@@ -168,29 +177,29 @@ Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
} }
} }
} }
return I; return I;
} }
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table) Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
{ {
// accept only char type matrices // accept only char type matrices
CV_Assert(I.depth() != sizeof(uchar)); CV_Assert(I.depth() != sizeof(uchar));
const int channels = I.channels(); const int channels = I.channels();
switch(channels) switch(channels)
{ {
case 1: case 1:
{ {
for( int i = 0; i < I.rows; ++i) for( int i = 0; i < I.rows; ++i)
for( int j = 0; j < I.cols; ++j ) for( int j = 0; j < I.cols; ++j )
I.at<uchar>(i,j) = table[I.at<uchar>(i,j)]; I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];
break; break;
} }
case 3: case 3:
{ {
Mat_<Vec3b> _I = I; Mat_<Vec3b> _I = I;
for( int i = 0; i < I.rows; ++i) for( int i = 0; i < I.rows; ++i)
for( int j = 0; j < I.cols; ++j ) for( int j = 0; j < I.cols; ++j )
{ {
...@@ -202,6 +211,6 @@ Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table) ...@@ -202,6 +211,6 @@ Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
break; break;
} }
} }
return I; return I;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment