Homepage Demos Overview Downloads Tutorials 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 <ext/hash_map>
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 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 __gnu_cxx::hash_map<const char*, unsigned int, __gnu_cxx::hash<const char*>, hashcmp_eqstr> hashmap; //!< a shorthand for the hash structure that CMVision expects for the color lookups
00057   
00058   //! constructor
00059   SegmentedColorGenerator(EventBase::EventGeneratorID_t gid, unsigned int sid, unsigned int mysid);
00060   //! constructor, you can pass which channels to use as Y, U, & V channels
00061   SegmentedColorGenerator(EventBase::EventGeneratorID_t gid, unsigned int sid, unsigned int mysid, 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 processEvent(const EventBase& event);
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)
00086   unsigned int getColorIndex(const char * name) const { 
00087     hashmap::const_iterator i;
00088     i=colorNames.find(name);
00089     return (i==colorNames.end())?-1U:i->second;
00090   }
00091   
00092   //! returns index of color corresponding to a string (uses a fast hash lookup)
00093   unsigned int getColorIndex(const std::string& name) const { return getColorIndex(name.c_str()); }
00094   
00095   virtual unsigned int getBinSize() const;
00096   virtual unsigned int LoadBuffer(const char buf[], unsigned int len);
00097   virtual unsigned int SaveBuffer(char buf[], unsigned int len) const;
00098   virtual unsigned int encodeColors(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)
00099   virtual unsigned int decodeColors(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)
00100   
00101 
00102 protected:
00103   static const unsigned int BITS_Y = 4; //!< bits of discretization for Y channel in the threshold map
00104   static const unsigned int BITS_U = 6; //!< bits of discretization for U channel in the threshold map
00105   static const unsigned int BITS_V = 6; //!< bits of discretization for V channel in the threshold map
00106   static const unsigned int NUM_Y = 1 << BITS_Y; //!< levels of discretization for Y channel in the threshold map
00107   static const unsigned int NUM_U = 1 << BITS_U; //!< levels of discretization for U channel in the threshold map
00108   static const unsigned int NUM_V = 1 << BITS_V; //!< levels of discretization for V channel in the threshold map
00109   static const unsigned int MAX_COLORS = 20; //!< maximum number of different colors that can be segmented
00110   
00111   //! ignores @a nChannels - the number of channels is always the number of loaded threshold maps
00112   virtual void setNumImages(unsigned int nLayers, unsigned int nChannels);
00113   virtual void setDimensions(); //!< sets stride parameter to width (as set by FilterBankGenerator::setDimensions())
00114   virtual unsigned char * createImageCache(unsigned int layer, unsigned int chan) const;
00115   virtual void calcImage(unsigned int layer, unsigned int chan) const;
00116 
00117   unsigned int srcYChan; //!< the channel of the source's Y channel
00118   unsigned int srcUChan; //!< the channel of the source's U channel
00119   unsigned int srcVChan; //!< the channel of the source's V channel
00120   
00121   std::vector<cmap_t*> tmaps; //!< list of threshold maps so you can segment the same source different ways
00122   std::vector<std::string> tmapNames; //!< filename of each tmap;
00123 
00124   unsigned int numColors; //!< number of available colors
00125   color_class_state colors[MAX_COLORS]; //!< array of available colors
00126   hashmap colorNames; //!< look up color indexes corresponding to names
00127 
00128 private:
00129   SegmentedColorGenerator(const SegmentedColorGenerator& fbk); //!< don't call
00130   const SegmentedColorGenerator& operator=(const SegmentedColorGenerator& fbk); //!< don't call
00131 };
00132 
00133 /*! @file 
00134  * @brief Describes SegmentedColorGenerator, which generates FilterBankEvents indexed color images based on a color threshold file
00135  * @author alokl (Creator)
00136  * @author ejt (reorganized)
00137  *
00138  * $Author: ejt $
00139  * $Name: tekkotsu-2_1 $
00140  * $Revision: 1.6 $
00141  * $State: Exp $
00142  * $Date: 2004/02/18 21:13:32 $
00143  */
00144 
00145 #endif

Tekkotsu v2.1
Generated Tue Mar 16 23:19:15 2004 by Doxygen 1.3.5