iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-rgb2hsv-Image_vs_Blitz_vs_OpenCV.C
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 #include <new>
35 #include <malloc.h>
36 #include <iostream>
37 
38 using namespace std;
39 
40 int newCounter = 0;
41 int deleteCounter = 0;
42 
43 inline void* operator new(size_t size) throw(std::bad_alloc)
44 {
45  newCounter++;
46  return malloc(size);
47 }
48 
49 inline void operator delete(void* p) throw()
50 {
51  deleteCounter++;
52  if(p) free(p);
53 }
54 
55 inline void reportNewDeleteUsage(bool resetCounters = true)
56 {
57  cout << ">> operator new is called " << newCounter << " times" << endl;
58  cout << ">> operator delete is called " << deleteCounter << " times" << endl;
59  if( resetCounters )
60  {
61  newCounter = deleteCounter = 0;
62  cout << ">> new/delete counters are set to 0 now" << endl;
63  }
64 }
65 
66 //////////////////////////////////////////////// Test 1
67 
68 #include <nrt/Core/Image/Image.H>
70 
71 using namespace nrt;
72 
73 void Test1()
74 {
75  // create an RGB image
76  Image<PixRGB<byte>> rgb_img(Dims(640,480));
77  PixRGB<byte> bgColor = PixRGB<byte>(128, 196, 255);
78  for(PixRGB<byte> &pix : rgb_img)
79  pix = bgColor;
80 
81  // create a HSV image
82  Image<PixHSV<byte>> hsv_img(Dims(640,480));
83 
84  for (int i=0; i<255; i++)
85  {
86  hsv_img = rgb_img;
87  }
88 }
89 
90 //////////////////////////////////////////////// Test 2
91 #include <blitz/array.h>
92 
93 using namespace blitz;
94 
95 // tell blitz how to go from RGB to HSV
96 inline const PixHSV<byte> RGB2HSV(const PixRGB<byte>& rgb)
97 { return static_cast<PixHSV<byte>>(rgb); }
98 BZ_DECLARE_FUNCTION_RET(RGB2HSV,PixHSV<byte>)
99 
100 void Test2()
101 {
102  // create an RGB image
103  blitz::Array<PixRGB<byte>,2> rgb_img(640,480);
104  rgb_img = PixRGB<byte>(128, 196, 255);
105 
106  // create an HSV image
107  blitz::Array<PixHSV<byte>,2> hsv_img(640,480);
108 
109  for (int i=0; i<255; i++)
110  {
111  hsv_img = RGB2HSV(rgb_img);
112  }
113 }
114 
115 //////////////////////////////////////////////// Test 3
116 #include <opencv/cv.h>
117 
118 void Test3()
119 {
120  // create an RGB image
121  IplImage* rgb_img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
122  unsigned char* ptr = reinterpret_cast<unsigned char*>(rgb_img->imageData);
123  for( int count = 640*480-1; count >= 0; --count )
124  {
125  *ptr++ = 255;
126  *ptr++ = 196;
127  *ptr++ = 128;
128  }
129 
130  // create an HSV image
131  IplImage* hsv_img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
132 
133  for (int i=0; i<255; i++)
134  {
135  cvCvtColor(rgb_img,hsv_img,CV_BGR2HSV); // src -> dst
136  }
137 
138  cvReleaseImage(&hsv_img);
139  cvReleaseImage(&rgb_img);
140 }
141 
142 ////////////////////////////////////////////////
143 
145 
146 int main(int argc, char* argv[])
147 {
148  cout << "========= START OF TESTING RGB2HSV - nrt::Image vs blitz::Array vs OpenCV" << endl;
149  reportNewDeleteUsage();
150 
151  cout << "========= START OF TEST1 - using nrt::Image" << endl;
152  nrt::TimeProfiler<1> timer1;
153  timer1.begin();
154  // for(int ii = 0; ii < 10; ++ii)
155  Test1();
156  timer1.end();
157  cout << timer1.report() << endl;
158  reportNewDeleteUsage();
159 
160  cout << "========= START OF TEST2 - using blitz::Array" << endl;
161  nrt::TimeProfiler<2> timer2;
162  timer2.begin();
163  // for(int ii = 0; ii < 10; ++ii)
164  Test2();
165  timer2.end();
166  cout << timer2.report() << endl;
167  reportNewDeleteUsage();
168 
169  cout << "========= START OF TEST3 - using OpenCV" << endl;
170  nrt::TimeProfiler<3> timer3;
171  timer3.begin();
172  // for(int ii = 0; ii < 10; ++ii)
173  Test3();
174  timer3.end();
175  cout << timer3.report() << endl;
176  reportNewDeleteUsage();
177 
178  return 0;
179 }
180