ClassNames.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file ClassNames.h
  3. * @brief simple interface for class name confusion
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/08/2008, latest update 29-01-2014 (dd-mm-yyyy)
  6. */
  7. #ifndef CLASSNAMESINCLUDE
  8. #define CLASSNAMESINCLUDE
  9. #include "core/image/ImageT.h"
  10. #include "core/vector/VectorT.h"
  11. #include "core/vector/MatrixT.h"
  12. #include <string>
  13. #include <map>
  14. // in future we should replace this with
  15. // unordered_map, but it is still experimental but standard c++
  16. #ifdef WIN32
  17. #ifdef NICE_USELIB_BOOST
  18. #include <boost/unordered_map.hpp>
  19. #endif
  20. #else
  21. //#include <ext/hash_map>
  22. #ifdef __clang__
  23. #include <unordered_map>
  24. #else
  25. #include <tr1/unordered_map>
  26. #endif
  27. #endif
  28. #include "core/basics/Config.h"
  29. #include "core/basics/Persistent.h"
  30. namespace OBJREC {
  31. /** simple interface for class name confusion */
  32. class ClassNames : public NICE::Persistent
  33. {
  34. protected:
  35. std::map<std::string, std::string> tbl_code_text;
  36. std::map<std::string, std::string> tbl_text_code;
  37. std::map<int, std::string> tbl_classno_code;
  38. std::map<std::string, int> tbl_code_classno;
  39. #if defined WIN32 && defined NICE_USELIB_BOOST
  40. boost::unordered_map<long, int> tbl_color_classno;
  41. #else
  42. // __gnu_cxx::hash_map<long, int> tbl_color_classno;
  43. #ifdef __clang__
  44. std::unordered_map<long, int> tbl_color_classno;
  45. #else
  46. std::tr1::unordered_map<long, int> tbl_color_classno;
  47. #endif
  48. #endif
  49. std::map<int, long> tbl_classno_color;
  50. int maxClassNo;
  51. public:
  52. /** simple constructor */
  53. ClassNames();
  54. /** copy constructor */
  55. ClassNames ( const ClassNames & cn );
  56. /** create sub selection */
  57. ClassNames ( const ClassNames & cn,
  58. const std::string & classselection );
  59. /** simple destructor */
  60. virtual ~ClassNames();
  61. /** get the readable class number of a class number */
  62. std::string text ( int classno ) const;
  63. /** get the class code (e.g. ascii code) of a class number */
  64. std::string code ( int classno ) const;
  65. /** get the class number corresponding to the code */
  66. int classno ( std::string code ) const;
  67. /** get the class number corresponding to the name */
  68. int classnoFromText ( std::string text ) const;
  69. /** number of classes */
  70. int numClasses () const;
  71. void getSelection ( const std::string & classselection,
  72. std::set<int> & classnos ) const;
  73. /** add new class information
  74. * @param classno class number
  75. * @param code class code (such as ascii number or just the name)
  76. * @param text class name as readable ascii representation
  77. */
  78. void addClass ( int classno, const std::string & code,
  79. const std::string & text );
  80. bool readFromConfig ( const NICE::Config & datasetconf,
  81. const std::string & classselection );
  82. /** is there any class with this class number */
  83. bool existsClassno ( int classno ) const;
  84. /** is there any class with this class code */
  85. bool existsClassCode ( const std::string & classcode ) const;
  86. int getMaxClassno () const;
  87. void getRGBColor ( int classno, int & r, int & g, int & b ) const;
  88. void getClassnoFromColor ( int & classno, int r, int g, int b ) const;
  89. /** colorize a labeled image using color information given in the class
  90. * information file */
  91. void labelToRGB ( const NICE::Image & img, NICE::ColorImage & rgb ) const;
  92. void labelToRGB ( const NICE::ImageT<int> & img, NICE::ColorImage & rgb ) const;
  93. int getBackgroundClass () const;
  94. /** restore classnames in the format <classtext> <classcode> <classno> */
  95. void restore ( std::istream & is, int format = 0 );
  96. /** store classnames in the format <classtext> <classcode> <classno> */
  97. void store ( std::ostream & os, int format = 0 ) const;
  98. /** clear class information */
  99. void clear ();
  100. };
  101. } // namespace
  102. #endif