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  */
00040 class SegmentedColorGenerator : public FilterBankGenerator {
00041 public:
00042   typedef uchar cmap_t; //!< type to use for color indexes
00043   typedef CMVision::color_class_state color_class_state; //!< use CMVision's color structure
00044   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
00045   
00046   //! constructor
00047   SegmentedColorGenerator(EventBase::EventGeneratorID_t gid, unsigned int sid, unsigned int mysid);
00048   //! constructor, you can pass which channels to use as Y, U, & V channels
00049   SegmentedColorGenerator(EventBase::EventGeneratorID_t gid, unsigned int sid, unsigned int mysid, unsigned int syc, unsigned int suc, unsigned int svc);
00050   //! destructor
00051   virtual ~SegmentedColorGenerator();
00052 
00053   static std::string getClassDescription() { return "Converts a FilterBankGenerator's data into indexed color"; }
00054 
00055   //! should receive FilterBankEvents from any standard format FilterBankGenerator (like RawCameraGenerator)
00056   virtual void processEvent(const EventBase& event);
00057 
00058   //! loads a threshold map into memory from a file, returns -1U if failed, otherwise returns corresponding channel
00059   virtual unsigned int loadThresholdMap(const std::string& tm_file);
00060 
00061   //! loads color information from a file, returns false if failed, true otherwise
00062   virtual bool loadColorInfo(const std::string& col_file);
00063   
00064   //! returns the number of different colors available
00065   virtual unsigned int getNumColors() const { return numColors; }
00066 
00067   //! gives direct access to the color information
00068   virtual const color_class_state * getColors() const { return colors; }
00069 
00070   //! gives direct access to the color information
00071   virtual color_class_state * getColors() { return colors; }
00072 
00073   //! returns index of color corresponding to a string (uses a fast hash lookup)
00074   unsigned int getColorIndex(const char * name) const { 
00075     hashmap::const_iterator i;
00076     i=colorNames.find(name);
00077     return (i==colorNames.end())?-1U:i->second;
00078   }
00079   
00080   //! returns index of color corresponding to a string (uses a fast hash lookup)
00081   unsigned int getColorIndex(const std::string& name) const { return getColorIndex(name.c_str()); }
00082   
00083   virtual unsigned int getBinSize() const;
00084   virtual unsigned int LoadBuffer(const char buf[], unsigned int len);
00085   virtual unsigned int SaveBuffer(char buf[], unsigned int len) const;
00086   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)
00087   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)
00088   
00089 
00090 protected:
00091   static const unsigned int BITS_Y = 4; //!< bits of discretization for Y channel in the threshold map
00092   static const unsigned int BITS_U = 6; //!< bits of discretization for U channel in the threshold map
00093   static const unsigned int BITS_V = 6; //!< bits of discretization for V channel in the threshold map
00094   static const unsigned int NUM_Y = 1 << BITS_Y; //!< levels of discretization for Y channel in the threshold map
00095   static const unsigned int NUM_U = 1 << BITS_U; //!< levels of discretization for U channel in the threshold map
00096   static const unsigned int NUM_V = 1 << BITS_V; //!< levels of discretization for V channel in the threshold map
00097   static const unsigned int MAX_COLORS = 20; //!< maximum number of different colors that can be segmented
00098   
00099   //! ignores @a nChannels - the number of channels is always the number of loaded threshold maps
00100   virtual void setNumImages(unsigned int nLayers, unsigned int nChannels);
00101   virtual void setDimensions(); //!< sets width, height, skip, and stride parameters
00102   virtual unsigned char * createImageCache(unsigned int layer, unsigned int chan) const;
00103   virtual void calcImage(unsigned int layer, unsigned int chan) const;
00104 
00105   const FilterBankGenerator * src; //!< the generator of the last FilterBankEvent received
00106   unsigned int srcYChan; //!< the channel of the source's Y channel
00107   unsigned int srcUChan; //!< the channel of the source's U channel
00108   unsigned int srcVChan; //!< the channel of the source's V channel
00109 
00110   
00111   std::vector<cmap_t*> tmaps; //!< list of threshold maps so you can segment the same source different ways
00112   std::vector<std::string> tmapNames; //!< filename of each tmap;
00113 
00114   unsigned int numColors; //!< number of available colors
00115   color_class_state colors[MAX_COLORS]; //!< array of available colors
00116   hashmap colorNames; //!< look up color indexes corresponding to names
00117 
00118 private:
00119   SegmentedColorGenerator(const SegmentedColorGenerator& fbk); //!< don't call
00120   const SegmentedColorGenerator& operator=(const SegmentedColorGenerator& fbk); //!< don't call
00121 };
00122 
00123 /*! @file 
00124  * @brief Describes SegmentedColorGenerator, which generates FilterBankEvents indexed color images based on a color threshold file
00125  * @author alokl (Creator)
00126  * @author ejt (reorganized)
00127  *
00128  * $Author: ejt $
00129  * $Name: tekkotsu-2_0 $
00130  * $Revision: 1.4 $
00131  * $State: Exp $
00132  * $Date: 2004/01/18 10:16:59 $
00133  */
00134 
00135 #endif

Tekkotsu v2.0
Generated Wed Jan 21 03:20:29 2004 by Doxygen 1.3.4