iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-GenericImage.C
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
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/config.h>
41 
42 #include <boost/mpl/list.hpp>
43 
44 #define BOOST_TEST_DYN_LINK
45 #define BOOST_TEST_MODULE PixelTest
46 #include <boost/test/unit_test.hpp>
47 
48 
49 BOOST_AUTO_TEST_CASE(GenericImageTest)
50 {
51  // ***** Default-constructed GenericImage:
52  nrt::GenericImage gImg;
53 
54  BOOST_CHECK_EQUAL(gImg.width(), 0);
55  BOOST_CHECK_EQUAL(gImg.height(), 0);
56  BOOST_CHECK_EQUAL(gImg.size(), 0);
57 
58  // Note that a boost variant is never empty, instead it contains a default-constructed object of the first type listed
59  // in the variant's list. In GenericImage, the first in the list is PixGray<byte>.
60  //
61  // Be very careful with typeStr() as it returns whatever the compiler calls your type. For example, the compiler will
62  // use 'unsigned char' instead of nrt::byte. So you should not use this in actual code, use isa() or hasPixels()
63  // instead are those will check for actual equality of types (and nrt::byte is indeed equal to unsigned char):
64  BOOST_CHECK_EQUAL(gImg.typeStr(), "nrt::Image<nrt::PixGray<unsigned char>, 0u>");
65 
66  // ***** Let's stuff a real image into our generic image:
68  gImg = img;
69 
70  BOOST_CHECK_EQUAL(gImg.isa< nrt::Image< nrt::PixRGB<nrt::byte> > >(), true);
71  BOOST_CHECK_EQUAL(gImg.hasPixels< nrt::PixRGB<nrt::byte> >(), true);
72  BOOST_CHECK_EQUAL(gImg.typeStr(), "nrt::Image<nrt::PixRGB<unsigned char>, 0u>");
73 
74  // ***** How about promotions:
75 
76  // Remember that we do not support unary operators on GenericImage as those would be confusing if the underlying Image
77  // type changes as the result of an operation. However, we support binary operators:
78  gImg = nrt::GenericImage(img) + nrt::GenericImage(img);
79  BOOST_CHECK_EQUAL(gImg.isa< nrt::Image< nrt::PixRGB<int> > >(), true);
80  BOOST_CHECK_EQUAL(gImg.typeStr(), "nrt::Image<nrt::PixRGB<int>, 0u>");
81 
82  // ***** Conversion:
83  nrt::Image<nrt::PixHSV<nrt::byte> > hsvImg = gImg.convertTo<nrt::PixHSV<nrt::byte>>();
84  BOOST_CHECK_EQUAL(hsvImg.at(0, 0), nrt::PixHSV<nrt::byte>(67, 222, 255));
85 
86  nrt::Image<nrt::PixGray<float> > grayImg = gImg.convertTo<nrt::PixGray<float>>();
87  BOOST_CHECK_EQUAL(grayImg.at(0, 0), nrt::PixGray<float>(277.0F));
88 
89  // ***** GenericImage operators with a constant:
90 
91  // RGB<byte> + RGB<byte> = RGB<int>
92  gImg = img;
93  nrt::GenericImage gImg2 = gImg + nrt::PixRGB<nrt::byte>(1, 2, 3);
94  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<int>, 0u>");
95 
96  // Gray<float> + RGB<byte> = RGB<float> (more channels wins)
97  gImg2 = nrt::PixGray<float>(1.23F) * gImg;
98  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
99 
100  // Gray<foat> + HSV<float> = HSV<float> (more channels wins)
101  gImg = grayImg;
102  gImg2 = gImg - nrt::PixHSV<float>(1.0F, 2.0F, 3.0F);
103  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<float>, 0u>");
104 
105  // HSV<float> + Gray<float> = HSV<float> (more channels wins)
106  gImg2 = nrt::PixHSV<float>(1.0F, 2.0F, 3.0F) + gImg;
107  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<float>, 0u>");
108 
109  // RGB<byte> + HSV<float> = RGB<float> (same number of channels, LHS wins)
110  gImg = img;
111  gImg2 = gImg / nrt::PixHSV<float>(1.0F, 2.0F, 3.0F);
112  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
113 
114  // HSV<float> + RGB<byte> = HSV<float> (same number of channels, LHS wins)
115  gImg2 = nrt::PixHSV<float>(1.0F, 2.0F, 3.0F) * gImg;
116  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<float>, 0u>");
117 
118  // ***** GenericImage operators with another GenericImage:
119 
120  // RGB<byte> + Gray<float> = RGB<float> (more channels wins)
122  gImg = img;
123  gImg2 = gImg + nrt::GenericImage(img2);
124  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
125 
126  // RGB<byte> + HSV<byte> = RGB<int> (same number of channels, LHS wins)
127  gImg2 = gImg - nrt::GenericImage(hsvImg);
128  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<int>, 0u>");
129 
130  // HSV<byte> + RGB<byte> = HSV<int> (same number of channels, LHS wins)
131  gImg2 = nrt::GenericImage(hsvImg) * gImg;
132  BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<int>, 0u>");
133 
134  // ***** Apply some generic processing
135  nrt::GenericImage gImg3 = nrt::lowPass3<float>(gImg);
136  BOOST_CHECK_EQUAL(gImg3.isa< nrt::Image< nrt::PixRGB<float> > >(), true);
137  BOOST_CHECK_EQUAL(gImg3.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
138 
139  // ***** clear the Image to zeros
140  nrt::GenericImage gImg4(grayImg);
141  gImg4.clear();
142  nrt::Image<nrt::PixGray<float> > resultImg = gImg4.convertTo<nrt::PixGray<float> >();
143  BOOST_CHECK_EQUAL(resultImg.at(0, 0), nrt::PixGray<float>(0.0F));
144 }
145