Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SegmentedColorGenerator.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SegmentedColorGenerator_h_
00003 #define INCLUDED_SegmentedColorGenerator_h_
00004 
00005 #include "Vision/FilterBankGenerator.h"
00006 #include "Vision/cmvision.h"
00007 #include "Vision/colors.h"
00008 #include <vector>
00009 
00010 //! Generates FilterBankEvents indexed color images based on a color threshold file
00011 /*! Pretty simple idea - use a big mapping of YUV values to lookup
00012  *  index values.
00013  *
00014  *  Threshold files are 16x64x64 = 64KB.  So each Y component is
00015  *  discretized into 16 levels, U and V into 64 each.  Then the
00016  *  appropriate element of the 3D matrix is looked up, which holds the
00017  *  desired index for that color.  The threshold files are generated
00018  *  offline. See http://www.tekkotsu.org/CameraSetup.html
00019  *
00020  *  The color information is shared for all threshold files in this
00021  *  object.
00022  *
00023  *  The row skip is always 0, and the row stride is always width.
00024  *  But it would be better to use the proper accessor functions to be
00025  *  more general.
00026  *
00027  *  Should receive FilterBankEvents from any standard format
00028  *  FilterBankGenerator (like RawCameraGenerator) <em>However</em>,
00029  *  images that use an increment!=1 will break.
00030  *
00031  *  The events which are produced are SegmentedColorFilterBankEvents,
00032  *  which will allow you to reference the color information later on.
00033  *  Keep in mind that the region and area statistic fields are not
00034  *  filled out at this stage... the RegionGenerator will complete the
00035  *  processing if you want that info as well.
00036  *
00037  *  Uses the CMVision library for main processing
00038  *
00039  *  The format used for serialization is: (code is in saveBuffer())
00040  *  - <@c FilterBankGenerator: superclass header> <i>(First saves the superclass's info)</i>
00041  *  - <@c string: "SegColorImage"> <i>(remember a 'string' is len+str+0; so this is the literal "\015\0\0\0SegColorImage\0"; also remember "\015" is octal for 13)</i>
00042  *  - <<tt>char[</tt>width<tt>*</tt>height<tt>]</tt>: image data> <i>(one byte per sample)</i>
00043  *  - <@c unsigned @c int: num_cols> <i>(number of different colors available)</i>
00044  *  - for each of num_col:
00045  *    - <@c char: red> <i>red color to use for display of this index</i>
00046  *    - <@c char: green> <i>green color to use for display of this index</i>
00047  *    - <@c char: blue> <i>blue color to use for display of this index</i>
00048  *
00049  *  For more information on serialization, see FilterBankGenerator
00050  *
00051  */
00052 class SegmentedColorGenerator : public FilterBankGenerator {
00053 public:
00054   typedef CMVision::uchar cmap_t; //!< type to use for color indexes
00055   typedef CMVision::color_class_state color_class_state; //!< use CMVision's color structure
00056   typedef CMVision::color_name_map color_name_map; //!< shorthand for CMVision's color name lookup data structure
00057 
00058   //! constructor
00059   SegmentedColorGenerator(unsigned int mysid, FilterBankGenerator* fbg, EventBase::EventTypeID_t tid);
00060   //! constructor, you can pass which channels to use as Y, U, & V channels
00061   SegmentedColorGenerator(unsigned int mysid, FilterBankGenerator* fbg, EventBase::EventTypeID_t tid, unsigned int syc, unsigned int suc, unsigned int svc);
00062   //! destructor
00063   virtual ~SegmentedColorGenerator();
00064 
00065   static std::string getClassDescription() { return "Converts a FilterBankGenerator's data into indexed color"; }
00066 
00067   //! should receive FilterBankEvents from any standard format FilterBankGenerator (like RawCameraGenerator)
00068   virtual void doEvent();
00069 
00070   //! loads a threshold map into memory from a file, returns -1U if failed, otherwise returns corresponding channel
00071   virtual unsigned int loadThresholdMap(const std::string& tm_file);
00072 
00073   //! loads color information from a file, returns false if failed, true otherwise
00074   virtual bool loadColorInfo(const std::string& col_file);
00075 
00076   //! returns the number of different colors available
00077   virtual unsigned int getNumColors() const { return numColors; }
00078 
00079   //! gives direct access to the color information
00080   virtual const color_class_state * getColors() const { return colors; }
00081 
00082   //! gives direct access to the color information
00083   virtual color_class_state * getColors() { return colors; }
00084 
00085   //! returns index of color corresponding to a string (uses a fast hash lookup), or -1U if not found
00086   unsigned int getColorIndex(const char * name) const {
00087     color_name_map::const_iterator i = colorNames.find(name);
00088     return (i==colorNames.end())?-1U:i->second;
00089   }
00090 
00091   //! returns index of color corresponding to a string (uses a fast hash lookup), or -1U if not found
00092   unsigned int getColorIndex(const std::string& name) const { return getColorIndex(name.c_str()); }
00093 
00094   //! returns index of color corresponding to a specific rgb color, or -1U if not found
00095   unsigned int getColorIndex(const rgb color) const {
00096     for(unsigned int index = 0; index < getNumColors(); index++)
00097       if(getColorRGB((int)index) == color)
00098         return index;
00099     return -1U;
00100   }
00101 
00102 
00103   //! returns rgb struct (from colors.h) corresponding to an int index.  Returns black if index is invalid.
00104   rgb getColorRGB(const unsigned int index) const {
00105     return (index>=numColors ? rgb() : getColors()[index].color);
00106   }
00107 
00108   //! returns rgb struct (from colors.h) corresponding to a string.  Returns black if index is invalid.
00109   rgb getColorRGB(const char * name) const {
00110     return getColorRGB(getColorIndex(name));
00111   }
00112 
00113   //! returns rgb struct (from colors.h) corresponding to a string.  Returns black if index is invalid.
00114   rgb getColorRGB(const std::string& name) const {
00115     return getColorRGB(name.c_str());
00116   }
00117 
00118         //! returns the name of a color given its index
00119        const char* getColorName(const unsigned int index) const {
00120                return (index>=numColors ? NULL : getColors()[index].name);
00121   }
00122 
00123   virtual unsigned int getBinSize() const;
00124   virtual unsigned int loadBuffer(const char buf[], unsigned int len, const char* filename=NULL);
00125   virtual unsigned int saveBuffer(char buf[], unsigned int len) const;
00126   virtual bool encodeColorsInc(char*& buf, unsigned int& len) const; //!< in case you want to only save the color info but not the image (this is binary - *not* the same format as what's read in loadColorInfo)
00127   virtual bool decodeColorsInc(const char*& buf, unsigned int& len); //!< in case you want to only load the color info but not the image (this is binary - *not* the same format as what's read in loadColorInfo)
00128 
00129 
00130 protected:
00131   //! thrown if no threshold maps are available
00132   class NoThresholdException : public std::exception {
00133   public:
00134     //! returns descriptive error string
00135     virtual const char * what() const throw() { return "SegmentedColorGenerator::calcImage(): can't segment image without any loaded threshold maps"; }
00136   };
00137 
00138   static const unsigned int BITS_Y = 4; //!< bits of discretization for Y channel in the threshold map
00139   static const unsigned int BITS_U = 6; //!< bits of discretization for U channel in the threshold map
00140   static const unsigned int BITS_V = 6; //!< bits of discretization for V channel in the threshold map
00141   static const unsigned int NUM_Y = 1 << BITS_Y; //!< levels of discretization for Y channel in the threshold map
00142   static const unsigned int NUM_U = 1 << BITS_U; //!< levels of discretization for U channel in the threshold map
00143   static const unsigned int NUM_V = 1 << BITS_V; //!< levels of discretization for V channel in the threshold map
00144   static const unsigned int MAX_COLORS = 20; //!< maximum number of different colors that can be segmented
00145 
00146   //! ignores @a nChannels - the number of channels is always the number of loaded threshold maps
00147   virtual void setNumImages(unsigned int nLayers, unsigned int nChannels);
00148   virtual void setDimensions(); //!< sets stride parameter to width (as set by FilterBankGenerator::setDimensions())
00149   //! creates the image cache width[layer]*height[layer] + 1 -- why plus one?  Because CMVision temporarily scribbles one-past end of each row
00150   virtual unsigned char * createImageCache(unsigned int layer, unsigned int chan) const;
00151   virtual void calcImage(unsigned int layer, unsigned int chan);
00152 
00153   unsigned int srcYChan; //!< the channel of the source's Y channel
00154   unsigned int srcUChan; //!< the channel of the source's U channel
00155   unsigned int srcVChan; //!< the channel of the source's V channel
00156 
00157   std::vector<cmap_t*> tmaps; //!< list of threshold maps so you can segment the same source different ways
00158   std::vector<std::string> tmapNames; //!< filename of each tmap;
00159 
00160   unsigned int numColors; //!< number of available colors
00161   color_class_state colors[MAX_COLORS]; //!< array of available colors
00162   color_name_map colorNames; //!< look up color indexes corresponding to names
00163 
00164 private:
00165   SegmentedColorGenerator(const SegmentedColorGenerator& fbk); //!< don't call
00166   const SegmentedColorGenerator& operator=(const SegmentedColorGenerator& fbk); //!< don't call
00167 };
00168 
00169 /*! @file
00170  * @brief Describes SegmentedColorGenerator, which generates FilterBankEvents indexed color images based on a color threshold file
00171  * @author alokl (Creator)
00172  * @author ejt (reorganized)
00173  */
00174 
00175 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:50 2016 by Doxygen 1.6.3