Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

StructurePlot.H

Go to the documentation of this file.
00001 /*!@file ModelNeuron/StructurePlot.H Class declaration for StructurePlot
00002    which creates hierarchical 2d image represenation of a structures
00003    activity*/
00004 
00005 // //////////////////////////////////////////////////////////////////// //
00006 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00007 // University of Southern California (USC) and the iLab at USC.         //
00008 // See http://iLab.usc.edu for information about this project.          //
00009 // //////////////////////////////////////////////////////////////////// //
00010 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00011 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00012 // in Visual Environments, and Applications'' by Christof Koch and      //
00013 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00014 // pending; application number 09/912,225 filed July 23, 2001; see      //
00015 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00016 // //////////////////////////////////////////////////////////////////// //
00017 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00018 //                                                                      //
00019 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00020 // redistribute it and/or modify it under the terms of the GNU General  //
00021 // Public License as published by the Free Software Foundation; either  //
00022 // version 2 of the License, or (at your option) any later version.     //
00023 //                                                                      //
00024 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00025 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00026 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00027 // PURPOSE.  See the GNU General Public License for more details.       //
00028 //                                                                      //
00029 // You should have received a copy of the GNU General Public License    //
00030 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00031 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00032 // Boston, MA 02111-1307 USA.                                           //
00033 // //////////////////////////////////////////////////////////////////// //
00034 //
00035 // Primary maintainer for this file: David J. Berg <dberg@usc.edu>
00036 // $HeadURL:svn://ilab.usc.edu/trunk/saliency/src/ModelNeuron/StructurePlot.H$
00037 
00038 #ifndef MODELNEURON_STRUCTUREPLOT_H_DEFINED
00039 #define MODELNEURON_STRUCTUREPLOT_H_DEFINED
00040 
00041 #include "Image/Layout.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Image/DrawOps.H"
00045 #include "Image/ShapeOps.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/Range.H"
00048 #include "Util/StringConversions.H"
00049 
00050 #include "ModelNeuron/LayerDecoder.H"
00051 #include "ModelNeuron/Location.H"
00052 #include "ModelNeuron/PlotBuffer.H"
00053 
00054 class StructureModule;
00055 // ######################################################################
00056 // ! A class for displaying a hierarchy of activity from a
00057 // StructureModule's sub units.
00058 // ######################################################################
00059 enum NormalizeType {SCALE, RANGE, SET_RANGE};
00060 
00061 class StructurePlot
00062 {
00063 public:
00064   //!Constructor 
00065   StructurePlot(const StructureModule& structure,
00066                 const NeuralDecoder& decoder, 
00067                 const NormalizeType normtype = SCALE, 
00068                 const double& rangemin = 0, const double& rangemax = 0);
00069 
00070   //!Constructor 
00071   StructurePlot(const StructureModule& structure,
00072                 const NormalizeType normtype = SCALE, 
00073                 const double& rangemin = 0, const double& rangemax = 0);
00074   
00075   //!Destructor
00076   ~StructurePlot();
00077   
00078   //!plot the structure
00079   Layout<PixRGB<byte> > draw(const StructureModule& structure, 
00080                              const uint w = 320, const uint h = 240, 
00081                              const Dims dims = Dims(0,1),
00082                              const uint depth = 2);
00083   
00084    //!set the probe
00085   void setProbe(Location probe);
00086   
00087 private:
00088   //!get the total number of subs
00089   uint countSubs(const StructureModule& structure);
00090 
00091   //!plot the structures 2d components
00092   Layout<byte> drawStructure(const StructureModule& structure, 
00093                              const uint w = 320, const uint h = 240, 
00094                              const uint depth = 2);
00095 
00096   //!prohibit copy constructor and assignment
00097   StructurePlot(const StructurePlot& rhs);
00098   StructurePlot& operator=(const StructurePlot& rhs);
00099 
00100   NormalizeType itsNormType;
00101   Range<double> itsRange;
00102   std::vector<std::string> itsNames;
00103   int itsTotal, itsCount;
00104   std::vector<LayerDecoder*> itsDecoder;
00105   Location itsProbe;
00106   PlotBufferList itsN;
00107 };
00108 
00109 // ######################################################################
00110 // ! inline implementation for StructurePlot
00111 // ######################################################################
00112 inline
00113 StructurePlot::StructurePlot(const StructureModule& structure,
00114                              const NeuralDecoder& decoder, 
00115                              const NormalizeType normtype, 
00116                              const double& rangemin, const double& rangemax) : 
00117   itsNormType(normtype), itsRange(rangemin, rangemax), 
00118   itsNames(), itsTotal(countSubs(structure)+1), itsCount(-1), 
00119   itsDecoder(), itsProbe(), itsN()
00120 {
00121   for (int ii = 0; ii < itsTotal; ++ii)
00122     itsDecoder.push_back(new LayerDecoder(decoder, structure.getDims())); 
00123 }
00124 
00125 // ######################################################################
00126 inline
00127 StructurePlot::StructurePlot(const StructureModule& structure,
00128                              const NormalizeType normtype, 
00129                              const double& rangemin, const double& rangemax) : 
00130   itsNormType(normtype), itsRange(rangemin, rangemax), 
00131   itsNames(), itsTotal(countSubs(structure)+1), itsCount(-1), itsDecoder(),
00132   itsProbe(), itsN()
00133 {
00134 }
00135 
00136 // ######################################################################
00137 inline
00138 StructurePlot::~StructurePlot()  
00139 {
00140   std::vector<LayerDecoder*>::const_iterator 
00141     begin(itsDecoder.begin()), end(itsDecoder.end());
00142   while (begin != end)
00143       delete *begin++;
00144 }
00145 
00146 // ######################################################################
00147 void StructurePlot::setProbe(Location probe)
00148 {
00149   itsN.clear();
00150   itsProbe = probe; 
00151 };
00152 
00153 // ######################################################################
00154 inline
00155 Layout<PixRGB<byte> > 
00156 StructurePlot::draw(const StructureModule& structure,
00157                     const uint w, const uint h, const Dims dims,
00158                     const uint depth)
00159 {
00160   Layout<PixRGB<byte> > out;
00161   //check for probe and we need to check for initialization
00162   Image<PixRGB<byte> > ds = drawStructure(structure,w,h,depth).render();
00163   //if we have a probe collect that info
00164   if (itsProbe.getDims() > 0)
00165     {
00166       itsN.push( structure.getUnit(itsProbe) );
00167       out = itsN.draw(false, 640, ds.getHeight(), dims);
00168     }
00169   //compine the probe and layer plots. 
00170   out = hcat(ds,out);
00171   return out;
00172 }
00173 
00174 // ######################################################################
00175 inline
00176 Layout<byte> 
00177 StructurePlot::drawStructure(const StructureModule& structure,
00178                              const uint w, const uint h,
00179                              const uint depth)
00180 {
00181   Layout<byte> layout;
00182   //perform depth first grab of output data
00183   if (depth != 0)
00184     for (uint i = 0; i < structure.numSubs(); ++i)
00185       {
00186         const StructureModule& temp = structure.getSub(i);
00187         uint mydepth = depth - 1;
00188         layout = hcat(layout, drawStructure(temp, w, h, mydepth));
00189       }
00190   
00191   //base case
00192   ++itsCount;
00193   Image<double> out;
00194   if (itsDecoder.size() > 0)
00195     {
00196       itsDecoder[itsCount]->push(structure.getOutput());
00197       out = itsDecoder[itsCount]->getOutput();
00198     }
00199   else
00200     out = structure.getOutput();
00201 
00202   //scale and convert type, rescale if necessary
00203   Image<byte> outb;
00204   if (itsNormType == SCALE)
00205     {
00206       inplaceNormalize(out, 0.0, 255.0);
00207       outb = out;
00208     }
00209   else
00210     {
00211       if (itsNormType == RANGE)
00212         itsRange.merge(rangeOf(out));
00213       outb = remapRange(out, itsRange, Range<double>(0.0, 255.0)); 
00214     }
00215 
00216   outb = rescale(outb, Dims(w,h));
00217   if ((itsNormType == RANGE) || (itsNormType == SET_RANGE))
00218     {
00219       std::string temp = itsNames[itsCount] + " " + 
00220         convertToString(itsRange.min()) + ":" + 
00221         convertToString(itsRange.max());
00222       writeText(outb, Point2D<int>(0,0), temp.c_str());
00223     }
00224   else
00225     writeText(outb, Point2D<int>(0,0), itsNames[itsCount].c_str());
00226 
00227   Rectangle r(Point2D<int>(0,0), Dims(outb.getWidth(),outb.getHeight()));
00228   drawRectSquareCorners(outb ,r , (byte)255, 2);
00229   layout = vcat(outb, layout);
00230 
00231   if (itsCount == itsTotal-1)
00232     itsCount = -1;
00233 
00234   return layout;
00235 }
00236 
00237 // ######################################################################
00238 inline
00239 uint StructurePlot::countSubs(const StructureModule& structure)
00240 {
00241   uint child = 0;
00242   for (uint i = 0; i < structure.numSubs(); ++i)
00243     {
00244       const StructureModule& temp(structure.getSub(i));
00245       child += countSubs(temp); //recursion = depth first count 
00246     }
00247   child += structure.numSubs();
00248   itsNames.push_back(structure.getName());
00249 
00250   return child;
00251 }
00252 
00253 #endif
00254 
00255 // ######################################################################
00256 /* So things look consistent in everyone's emacs... */
00257 /* Local Variables: */
00258 /* indent-tabs-mode: nil */
00259 /* End: */

Generated on Sun Nov 22 13:42:40 2009 for iLab Neuromorphic Vision Toolkit by  doxygen 1.4.4