iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Image.C
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
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 
35 
36 #include <nrt/Core/Image/Image.H>
42 #include <nrt/Core/Image/Layout.H>
44 
45 #include <type_traits>
46 
47 using namespace nrt;
48 
49 template <class T>
50 void printImage(Image<T> const img)
51 {
52  NRT_INFO("Image(" << img.width() << ", " << img.height() <<") = { ");
53  for (int y = 0; y < img.height(); ++y)
54  {
55  std::stringstream str; str << " {";
56  for (int x = 0; x < img.width(); ++x) str << " " << img.at(x, y) << " ";
57  NRT_INFO(str.str() << "}");
58  }
59  NRT_INFO("}");
60 }
61 
62 int main(int argc, char const* argv[])
63 {
64  if (argc != 2) NRT_FATAL("Give me the pixel value (format \"PixRGB(255,255,255)\" (quotes needed)");
65  nrt::PixRGB<nrt::byte> x; std::stringstream stream; stream << argv[1]; stream >> x;
66 
67  NRT_INFO("-----------------------RGB:");
68  NRT_INFO("Pixel: " << x);
69  NRT_INFO("first channel: " << x.channels[0]);
70 
71  NRT_INFO("-----------------------HSV:");
72  PixHSV<byte> y(x);
73  NRT_INFO(y);
74 
75  NRT_INFO("-----------------------RGB:");
76  PixRGB<byte> z(y);
77  NRT_INFO(z);
78 
79  NRT_INFO("-----------------------Image tests:");
80 
81  Image<PixGray<nrt::byte>> im1 { { 1, 2, 3 } }; // construction from initializer list
82  printImage(im1);
83 
84  // test range-based for loop
85  for (auto const & pix : im1) NRT_INFO("pix = " << pix);
86 
87  Image<PixGray<nrt::byte>> im2 { { 1, 2, 3 }, { 4, 5, 6} };
88  printImage(im2);
89 
90  Image<PixGray<nrt::byte>> im3 { { 1 }, { 2 }, { 3 } };
91  printImage(im3);
92 
93  // Some ImageSet testing:
95  is.push_back(im1);
96  is.push_back(im2);
97  is.push_back(im3);
98 
99  NRT_INFO("This should be 2: " << is.at(1).at(1, 0));
100  NRT_INFO("Interpolated should be 3: " << is.at(1).interpolate(0, 0.55));
101 
102  Image<PixRGB<byte> > colim { { PixRGB<nrt::byte>(2,12,52) },
103  { PixRGB<nrt::byte>(4,22,62) },
104  { PixRGB<nrt::byte>(6,32,72) } };
105 
106  NRT_INFO("RGB interp should be(3,17,57) " << colim.interpolate(0,0.5));
107  // Some Layout testing:
109  NRT_INFO("Layout dims, should be (7,3): " << la.dims());
110 
111  Image<PixGray<byte>> r = la.render();
112  NRT_INFO("Rendered dims, should be (7,2): " << r.dims());
113  NRT_INFO("Rendered pixel, should be 5: " << r.at(4, 1));
114 
115  //testing iterators
116  Image<PixGray<float>> imiter = { { 1, 2, 3 }, { 4, 5, 6} };
117  Image<PixGray<float>>::const_iterator i = imiter.const_begin();
118  NRT_INFO("Test read iterators: result should display Values: 1-6.");
119  while (i != imiter.const_end())
120  NRT_INFO("Value: " << *i++);
121 
122  NRT_INFO("Test read row iterator: result should display Values: 4-6.");
123  Image<PixGray<float>>::const_row_iterator irow = imiter.const_row_begin(1);
124  while(irow != imiter.const_row_end(1))
125  NRT_INFO("Value: " << *irow++);
126 
127  NRT_INFO("Test read column iterator: result should display Values: 2,5.");
128  Image<PixGray<float>>::const_col_iterator icol = imiter.const_col_begin(1);
129  while(icol != imiter.const_col_end(1))
130  NRT_INFO("Value: " << *icol++);
131 
132  NRT_INFO("Test read rectangle iterator: result should display Values: 1,2,4,5.");
134  Image<PixGray<float>>::const_rect_iterator irect = imiter.const_rect_begin(rect);
135  while(irect != imiter.const_rect_end(rect))
136  NRT_INFO("Value: " << *irect++);
137 
138  Image<PixGray<float>>::rect_iterator jrect = imiter.rect_begin(rect);
139  NRT_INFO("Test write rectangle iterator: result should be 0,0,3;0,0,6.");
140  while(jrect != imiter.rect_end(rect))
141  *jrect++ = PixGray<float>(0.0F);
142  printImage(imiter);
143 
144  NRT_INFO("Test write iterator: result should be all 1s.");
145  Image<PixGray<float>>::iterator j = imiter.begin();
146  while (j != imiter.end())
147  *j++ = PixGray<float>(1.0F);
148  printImage(imiter);
149 
150  // Some math operator testing
151  Image<PixGray<float>> imo1 = { { 1, 1, 1 }, { 1, 1, 1} };
152  Image<PixGray<float>> imo2 = { { 2, 2, 2 }, { 2, 2, 2} };
153  Image<PixGray<float>> imo3;
154 
155  // Image operators with images, pixels, or pod types, the underyling pod type
156  // can be different, but the pixel type must be the same
157  imo1 += imo2;
158  imo1 -= PixGray<int>(2);
159  imo1 *= 2.0F;
160  //imo1 /= PixRGB<float>(); //can't operate on different pixels
161  imo3 = (imo1 + imo2 - PixGray<float>(0.0F))/2.0;
162 
163  NRT_INFO("Operator result should be all 2's");
164  printImage(imo3);
165 
166  //test accumulate
167  Image<PixGray<float>> ima = { { 1, 2, 3 }, { 4, 5, 6} };
168  PixGray<float> val = nrt::accumulate(ima);//sum all elements
169  PixGray<float> vale = nrt::accumulate(ima, PixGray<float>(4.0F));//sum starting at a value
170  NRT_INFO("Accumulate result should be 21 and 25: " << val << "," << vale );
171 
172  //these tests are the same as above, but we pass in our own function, or
173  //lambda or function object
174  PixGray<float> val1 =
176  [](PixGray<float> const & val1, PixGray<float> const & val2){ return val1 + val2; });
177 
178  PixGray<float> vale1 = nrt::accumulate(ima, PixGray<float>(4.0F), std::plus<PixGray<float>>());
179  NRT_INFO("Accumulate result should be 21 and 25: " << val1 << "," << vale1 );
180 
181  Image<PixGray<float>> imt1 = { { 1, 2, 3 }, { 4, 5, 6} };
182  Image<PixGray<float>> imt2;
183 
184  //create a new image from a single image by applying our lambda at every pixel
185  imt2 = nrt::transform(imt1, [](PixGray<float> const & v){ return v*v; });
186 
187  //create a new image from two images by applying our function at every pixel,
188  //return is promoted type so inputs can be of different types. Computations
189  //performed as input types of functor.
190  imt2 = nrt::transform(imt1, imt1, std::multiplies<PixGray<float>>());
191  NRT_INFO("Transform result should be { { 1 4 9 }, { 16 25 36 } }: ");
192  printImage(imt2);
193 
194  //in place version of single input transform
195  nrt::transformInPlace(imt1, [](PixGray<float> const & v){ return v*v; });
196  NRT_INFO("Transform result should be { { 1 4 9 }, { 16 25 36 } }: ");
197  printImage(imt1);
198 
199  //in place version of two input transform
200  nrt::transformInPlace(imt2, imt2, std::divides<PixGray<float>>());
201  NRT_INFO("Transform result should be { { 1 1 1 }, { 1 1 1 } }: ");
202  printImage(imt2);
203 
204  //for_each test
205  struct print { void operator()(PixGray<float> const & val)
206  {NRT_INFO("for_each test using a print functor: " << val); }};
207 
208  nrt::for_each(imt1, print());
209 
210  // Test rescaling:
211  NRT_INFO("testing rescale...");
213  for (auto & pix : ii) pix = PixRGB<float>(10.0F, 11.0F, 12.0F);
214 
215  for (int32 w = 1; w < 121; ++w)
216  for (int32 h = 1; h < 55; ++h) {
217  Image<PixRGB<float> > rii = rescaleBilinear<void>(ii, Dims<int32>(w, h));
218  for (auto const & val : rii) for (auto const & c : val.channels) if (isnan(c)) NRT_FATAL("found a NaN");
219  }
220  NRT_INFO("No NaN's found after rescale. Good.");
221 
222  return 0;
223 }