iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-rgb2hsv-Image_vs_Blitz_vs_OpenCV.C
/*! @file
@author Unknown
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <new>
#include <malloc.h>
#include <iostream>
using namespace std;
int newCounter = 0;
int deleteCounter = 0;
inline void* operator new(size_t size) throw(std::bad_alloc)
{
newCounter++;
return malloc(size);
}
inline void operator delete(void* p) throw()
{
deleteCounter++;
if(p) free(p);
}
inline void reportNewDeleteUsage(bool resetCounters = true)
{
cout << ">> operator new is called " << newCounter << " times" << endl;
cout << ">> operator delete is called " << deleteCounter << " times" << endl;
if( resetCounters )
{
newCounter = deleteCounter = 0;
cout << ">> new/delete counters are set to 0 now" << endl;
}
}
//////////////////////////////////////////////// Test 1
using namespace nrt;
void Test1()
{
// create an RGB image
Image<PixRGB<byte>> rgb_img(Dims(640,480));
PixRGB<byte> bgColor = PixRGB<byte>(128, 196, 255);
for(PixRGB<byte> &pix : rgb_img)
pix = bgColor;
// create a HSV image
Image<PixHSV<byte>> hsv_img(Dims(640,480));
for (int i=0; i<255; i++)
{
hsv_img = rgb_img;
}
}
//////////////////////////////////////////////// Test 2
#include <blitz/array.h>
using namespace blitz;
// tell blitz how to go from RGB to HSV
inline const PixHSV<byte> RGB2HSV(const PixRGB<byte>& rgb)
{ return static_cast<PixHSV<byte>>(rgb); }
BZ_DECLARE_FUNCTION_RET(RGB2HSV,PixHSV<byte>)
void Test2()
{
// create an RGB image
blitz::Array<PixRGB<byte>,2> rgb_img(640,480);
rgb_img = PixRGB<byte>(128, 196, 255);
// create an HSV image
blitz::Array<PixHSV<byte>,2> hsv_img(640,480);
for (int i=0; i<255; i++)
{
hsv_img = RGB2HSV(rgb_img);
}
}
//////////////////////////////////////////////// Test 3
#include <opencv/cv.h>
void Test3()
{
// create an RGB image
IplImage* rgb_img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
unsigned char* ptr = reinterpret_cast<unsigned char*>(rgb_img->imageData);
for( int count = 640*480-1; count >= 0; --count )
{
*ptr++ = 255;
*ptr++ = 196;
*ptr++ = 128;
}
// create an HSV image
IplImage* hsv_img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
for (int i=0; i<255; i++)
{
cvCvtColor(rgb_img,hsv_img,CV_BGR2HSV); // src -> dst
}
cvReleaseImage(&hsv_img);
cvReleaseImage(&rgb_img);
}
////////////////////////////////////////////////
int main(int argc, char* argv[])
{
cout << "========= START OF TESTING RGB2HSV - nrt::Image vs blitz::Array vs OpenCV" << endl;
reportNewDeleteUsage();
cout << "========= START OF TEST1 - using nrt::Image" << endl;
timer1.begin();
// for(int ii = 0; ii < 10; ++ii)
Test1();
timer1.end();
cout << timer1.report() << endl;
reportNewDeleteUsage();
cout << "========= START OF TEST2 - using blitz::Array" << endl;
timer2.begin();
// for(int ii = 0; ii < 10; ++ii)
Test2();
timer2.end();
cout << timer2.report() << endl;
reportNewDeleteUsage();
cout << "========= START OF TEST3 - using OpenCV" << endl;
timer3.begin();
// for(int ii = 0; ii < 10; ++ii)
Test3();
timer3.end();
cout << timer3.report() << endl;
reportNewDeleteUsage();
return 0;
}