iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-GenericImage.C
/*! @file
@author Laurent Itti
@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 <nrt/config.h>
#include <boost/mpl/list.hpp>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE PixelTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(GenericImageTest)
{
// ***** Default-constructed GenericImage:
BOOST_CHECK_EQUAL(gImg.width(), 0);
BOOST_CHECK_EQUAL(gImg.height(), 0);
BOOST_CHECK_EQUAL(gImg.size(), 0);
// Note that a boost variant is never empty, instead it contains a default-constructed object of the first type listed
// in the variant's list. In GenericImage, the first in the list is PixGray<byte>.
//
// Be very careful with typeStr() as it returns whatever the compiler calls your type. For example, the compiler will
// use 'unsigned char' instead of nrt::byte. So you should not use this in actual code, use isa() or hasPixels()
// instead are those will check for actual equality of types (and nrt::byte is indeed equal to unsigned char):
BOOST_CHECK_EQUAL(gImg.typeStr(), "nrt::Image<nrt::PixGray<unsigned char>, 0u>");
// ***** Let's stuff a real image into our generic image:
gImg = img;
BOOST_CHECK_EQUAL(gImg.isa< nrt::Image< nrt::PixRGB<nrt::byte> > >(), true);
BOOST_CHECK_EQUAL(gImg.hasPixels< nrt::PixRGB<nrt::byte> >(), true);
BOOST_CHECK_EQUAL(gImg.typeStr(), "nrt::Image<nrt::PixRGB<unsigned char>, 0u>");
// ***** How about promotions:
// Remember that we do not support unary operators on GenericImage as those would be confusing if the underlying Image
// type changes as the result of an operation. However, we support binary operators:
BOOST_CHECK_EQUAL(gImg.isa< nrt::Image< nrt::PixRGB<int> > >(), true);
BOOST_CHECK_EQUAL(gImg.typeStr(), "nrt::Image<nrt::PixRGB<int>, 0u>");
// ***** Conversion:
BOOST_CHECK_EQUAL(hsvImg.at(0, 0), nrt::PixHSV<nrt::byte>(67, 222, 255));
nrt::Image<nrt::PixGray<float> > grayImg = gImg.convertTo<nrt::PixGray<float>>();
BOOST_CHECK_EQUAL(grayImg.at(0, 0), nrt::PixGray<float>(277.0F));
// ***** GenericImage operators with a constant:
// RGB<byte> + RGB<byte> = RGB<int>
gImg = img;
nrt::GenericImage gImg2 = gImg + nrt::PixRGB<nrt::byte>(1, 2, 3);
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<int>, 0u>");
// Gray<float> + RGB<byte> = RGB<float> (more channels wins)
gImg2 = nrt::PixGray<float>(1.23F) * gImg;
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
// Gray<foat> + HSV<float> = HSV<float> (more channels wins)
gImg = grayImg;
gImg2 = gImg - nrt::PixHSV<float>(1.0F, 2.0F, 3.0F);
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<float>, 0u>");
// HSV<float> + Gray<float> = HSV<float> (more channels wins)
gImg2 = nrt::PixHSV<float>(1.0F, 2.0F, 3.0F) + gImg;
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<float>, 0u>");
// RGB<byte> + HSV<float> = RGB<float> (same number of channels, LHS wins)
gImg = img;
gImg2 = gImg / nrt::PixHSV<float>(1.0F, 2.0F, 3.0F);
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
// HSV<float> + RGB<byte> = HSV<float> (same number of channels, LHS wins)
gImg2 = nrt::PixHSV<float>(1.0F, 2.0F, 3.0F) * gImg;
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<float>, 0u>");
// ***** GenericImage operators with another GenericImage:
// RGB<byte> + Gray<float> = RGB<float> (more channels wins)
gImg = img;
gImg2 = gImg + nrt::GenericImage(img2);
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
// RGB<byte> + HSV<byte> = RGB<int> (same number of channels, LHS wins)
gImg2 = gImg - nrt::GenericImage(hsvImg);
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixRGB<int>, 0u>");
// HSV<byte> + RGB<byte> = HSV<int> (same number of channels, LHS wins)
gImg2 = nrt::GenericImage(hsvImg) * gImg;
BOOST_CHECK_EQUAL(gImg2.typeStr(), "nrt::Image<nrt::PixHSV<int>, 0u>");
// ***** Apply some generic processing
nrt::GenericImage gImg3 = nrt::lowPass3<float>(gImg);
BOOST_CHECK_EQUAL(gImg3.isa< nrt::Image< nrt::PixRGB<float> > >(), true);
BOOST_CHECK_EQUAL(gImg3.typeStr(), "nrt::Image<nrt::PixRGB<float>, 0u>");
// ***** clear the Image to zeros
nrt::GenericImage gImg4(grayImg);
gImg4.clear();
nrt::Image<nrt::PixGray<float> > resultImg = gImg4.convertTo<nrt::PixGray<float> >();
BOOST_CHECK_EQUAL(resultImg.at(0, 0), nrt::PixGray<float>(0.0F));
}