#include <iostream>
#include <sstream>
static void help()
{
cout
<< "\n--------------------------------------------------------------------------" << endl
<< "This program shows how to scan image objects in OpenCV (cv::Mat). As use case"
<< " we take an input image and divide the native color palette (255) with the " << endl
<< "input. Shows C operator[] method, iterators and at function for on-the-fly item address calculation."<< endl
<< "Usage:" << endl
<< "./how_to_scan_images <imageNameToUse> <divideWith> [G]" << endl
<< "if you add a G parameter the image is processed in gray scale" << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
Mat& ScanImageAndReduceIterator(
Mat& I,
const uchar* table);
Mat& ScanImageAndReduceRandomAccess(
Mat& I,
const uchar * table);
int main(
int argc,
char* argv[])
{
help();
if (argc < 3)
{
cout << "Not enough parameters" << endl;
return -1;
}
if( argc == 4 && !strcmp(argv[3],"G") )
I =
imread(argv[1], IMREAD_GRAYSCALE);
else
I =
imread(argv[1], IMREAD_COLOR);
{
cout << "The image" << argv[1] << " could not be loaded." << endl;
return -1;
}
int divideWith = 0;
stringstream s;
s << argv[2];
s >> divideWith;
if (!s || !divideWith)
{
cout << "Invalid number entered for dividing. " << endl;
return -1;
}
for (int i = 0; i < 256; ++i)
table[i] = (
uchar)(divideWith * (i/divideWith));
const int times = 100;
double t;
for (int i = 0; i < times; ++i)
{
J = ScanImageAndReduceC(clone_i, table);
}
t /= times;
cout << "Time of reducing with the C operator [] (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl;
for (int i = 0; i < times; ++i)
{
J = ScanImageAndReduceIterator(clone_i, table);
}
t /= times;
cout << "Time of reducing with the iterator (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl;
for (int i = 0; i < times; ++i)
{
ScanImageAndReduceRandomAccess(clone_i, table);
}
t /= times;
cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "
<< times << " runs): " << t << " milliseconds."<< endl;
uchar* p = lookUpTable.ptr();
for( int i = 0; i < 256; ++i)
p[i] = table[i];
for (int i = 0; i < times; ++i)
t /= times;
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)
{
int nCols = I.
cols * channels;
{
nCols *= nRows;
nRows = 1;
}
int i,j;
for( i = 0; i < nRows; ++i)
{
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];
}
}
return I;
}
Mat& ScanImageAndReduceIterator(
Mat& I,
const uchar*
const table)
{
switch(channels)
{
case 1:
{
*it = table[*it];
break;
}
case 3:
{
{
(*it)[0] = table[(*it)[0]];
(*it)[1] = table[(*it)[1]];
(*it)[2] = table[(*it)[2]];
}
}
}
return I;
}
Mat& ScanImageAndReduceRandomAccess(
Mat& I,
const uchar*
const table)
{
switch(channels)
{
case 1:
{
for(
int i = 0; i < I.
rows; ++i)
for(
int j = 0; j < I.
cols; ++j )
break;
}
case 3:
{
for(
int i = 0; i < I.
rows; ++i)
for(
int j = 0; j < I.
cols; ++j )
{
_I(i,j)[0] = table[_I(i,j)[0]];
_I(i,j)[1] = table[_I(i,j)[1]];
_I(i,j)[2] = table[_I(i,j)[2]];
}
I = _I;
break;
}
}
return I;
}
Matrix read-write iterator.
Definition mat.hpp:3219
Template matrix class derived from Mat.
Definition mat.hpp:2247
n-dimensional dense array class
Definition mat.hpp:829
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
MatIterator_< _Tp > end()
Returns the matrix iterator and sets it to the after-last matrix element.
MatIterator_< _Tp > begin()
Returns the matrix iterator and sets it to the first matrix element.
uchar * ptr(int i0=0)
Returns a pointer to the specified matrix row.
int depth() const
Returns the depth of a matrix element.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
int channels() const
Returns the number of matrix channels.
int cols
Definition mat.hpp:2155
bool isContinuous() const
Reports whether the matrix is continuous or not.
bool empty() const
Returns true if the array has no elements.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2155
Template class for short numerical vectors, a partial case of Matx.
Definition matx.hpp:369
void LUT(InputArray src, InputArray lut, OutputArray dst)
Performs a look-up table transform of an array.
#define CV_8U
Definition interface.h:73
unsigned char uchar
Definition interface.h:51
double getTickFrequency()
Returns the number of ticks per second.
int64 getTickCount()
Returns the number of ticks.
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition base.hpp:359
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3