SemanticSegmentation.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * @file SemanticSegmentation.h
  3. * @brief abstract interface for semantic segmentation algorithms
  4. * @author Erik Rodner, Alexander Freytag, Sven Sickert
  5. * @date 03/19/2009, latest update: 14-05-2014 (dd-mm-yyyy)
  6. */
  7. #ifndef SEMANTICSEGMENTATIONINCLUDE
  8. #define SEMANTICSEGMENTATIONINCLUDE
  9. // standard library includes
  10. #include <vector>
  11. // nice-core includes
  12. #include <core/basics/Persistent.h>
  13. #include "core/image/MultiChannelImage3DT.h"
  14. // nice-vislearning includes
  15. #include "vislearning/cbaselib/MultiDataset.h"
  16. #include "vislearning/cbaselib/LocalizationResult.h"
  17. #include "vislearning/cbaselib/CachedExample.h"
  18. #include "vislearning/cbaselib/Example.h"
  19. #define ROADWORKSADD fthrow(NICE::Exception, "addNewExample(const NICE::Vector & newExample, const int & newClassNo): not yet implemented!");
  20. #define ROADWORKSADDNOVEL fthrow(NICE::Exception, "addNovelExamples(): not yet implemented!");
  21. #define ROADWORKSGETNOVEL fthrow(NICE::Exception, "getNovelExamples(): not yet implemented!");
  22. namespace OBJREC
  23. {
  24. /** abstract interface for semantic segmentation algorithms */
  25. class SemanticSegmentation : public NICE::Persistent
  26. {
  27. protected:
  28. /////////////////////////
  29. /////////////////////////
  30. // PROTECTED VARIABLES //
  31. /////////////////////////
  32. /////////////////////////
  33. /** accessible class names and information about
  34. number of classes etc. */
  35. const ClassNames * classNames;
  36. /** enum type for imagetype */
  37. enum IMAGETYP
  38. {
  39. IMAGETYPE_RGB = 0,
  40. IMAGETYPE_GRAY
  41. };
  42. /** whether to load images with color information */
  43. IMAGETYP imagetype;
  44. int iterationCountSuffix;
  45. /////////////////////////
  46. /////////////////////////
  47. // PROTECTED METHODS //
  48. /////////////////////////
  49. /////////////////////////
  50. public:
  51. ///////////////////// ///////////////////// /////////////////////
  52. // CONSTRUCTORS / DESTRUCTORS
  53. ///////////////////// ///////////////////// /////////////////////
  54. /** default constructor
  55. * @author Alexander Freytag
  56. * @date 06-02-2014 ( dd-mm-yyy )
  57. */
  58. SemanticSegmentation ( );
  59. /** simple constructor
  60. @param conf global settings
  61. @param classNames this ClassNames object while be stored as a attribute
  62. */
  63. SemanticSegmentation ( const NICE::Config *conf,
  64. const ClassNames *classNames );
  65. /** simple destructor */
  66. virtual ~SemanticSegmentation();
  67. /**
  68. * @brief Setup internal variables and objects used
  69. * @author Alexander Freytag
  70. * @param conf Config file to specify variable settings
  71. * @param s_confSection
  72. */
  73. void initFromConfig(const NICE::Config *conf, const std::string & s_confSection = "SemanticSegmentation");
  74. ///////////////////// ///////////////////// /////////////////////
  75. // SEGMENTATION STUFF
  76. ///////////////////// ///////////////////// /////////////////////
  77. /** load img from file call localize(CachedExample *ce) etc. */
  78. void semanticseg ( const std::string & filename,
  79. NICE::Image & segresult,
  80. NICE::MultiChannelImageT<double> & probabilities );
  81. /** this function has to be overloaded by all subclasses
  82. @param ce image data
  83. @param segresult result of the semantic segmentation with a label for each
  84. pixel
  85. @param probabilities multi-channel image with one channel for each class and
  86. corresponding probabilities for each pixel
  87. */
  88. virtual void semanticseg ( OBJREC::CachedExample *ce,
  89. NICE::Image & segresult,
  90. NICE::MultiChannelImageT<double> & probabilities ) = 0;
  91. /**
  92. * @brief Classification function (has to be overloaded by all subclasses)
  93. * @author Sven Sickert
  94. * @param imgData image data
  95. * @param segresult result of the semantic segmentation with a label for each pixel
  96. * @param probabilities multi-channel image with one channel for each class and
  97. * corresponding probabilities for each pixel
  98. * @param filelist filename list of images that represent slices of a stack
  99. */
  100. virtual void classify ( NICE::MultiChannelImage3DT<double> & imgData,
  101. NICE::MultiChannelImageT<double> & segresult,
  102. NICE::MultiChannelImage3DT<double> & probabilities,
  103. const std::vector<std::string> & filelist ) = 0;
  104. /** training function (has to be overloaded by all subclasses)
  105. * @param md the data set
  106. */
  107. virtual void train ( const MultiDataset * md ) = 0;
  108. /**
  109. * @brief Load image slices into a MultiChannelImage3DT
  110. * @author Sven Sickert
  111. * @param filelist filename list of images that represent slices of a stack
  112. * @param imgData output
  113. */
  114. void make3DImage ( const std::vector<std::string> & filelist,
  115. NICE::MultiChannelImage3DT<double> & imgData );
  116. ///////////////////// ///////////////////// /////////////////////
  117. // DATA CONVERSION
  118. ///////////////////// ///////////////////// /////////////////////
  119. /**
  120. * convert different datatypes
  121. */
  122. void convertVVectorToExamples ( NICE::VVector &feats,OBJREC::Examples &examples, std::vector<int> &label );
  123. void convertExamplesToVVector ( NICE::VVector &feats,OBJREC::Examples &examples, std::vector<int> &label );
  124. void convertExamplesToLSet ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
  125. void convertLSetToExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec, const bool & removeOldDataPointer=false );
  126. void convertLSetToSparseExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
  127. ///////////////////// ///////////////////// /////////////////////
  128. // ONLINE LEARNING
  129. ///////////////////// ///////////////////// /////////////////////
  130. virtual void addNewExample(const NICE::Vector & newExample, const int & newClassNo)
  131. {
  132. ROADWORKSADD;
  133. }
  134. /**
  135. * @brief Add those examples, which belong to the most novel region seen so far
  136. *
  137. * @return void
  138. **/
  139. virtual void addNovelExamples()
  140. {
  141. ROADWORKSADDNOVEL;
  142. }
  143. ///////////////////// ///////////////////// /////////////////////
  144. // GET / SET
  145. ///////////////////// ///////////////////// /////////////////////
  146. /**
  147. * @brief Get a pointer to the examples extracted from the most novel region seen so far
  148. *
  149. * @return Examples *
  150. **/
  151. virtual const Examples * getNovelExamples() const
  152. {
  153. ROADWORKSGETNOVEL;
  154. }
  155. /**
  156. * @brief Collect information about the depth of 3d images
  157. * @author Sven Sickert
  158. * @param Files a labeled set of data
  159. * @param depthVec output of depth values
  160. * @param run3dseg whether slice counting is necessary or not
  161. */
  162. void getDepthVector ( const LabeledSet *Files, std::vector<int> & depthVec, const bool run3dseg );
  163. /**
  164. * @brief Save probability maps of all classes to iamge files
  165. * @author Sven Sickert
  166. * @param prob class probability maps
  167. */
  168. void getProbabilityMap( const NICE::MultiChannelImage3DT<double> & prob );
  169. /**
  170. * @author Alexander Freytag
  171. * @date 06-02-2014 ( dd-mm-yyyy )
  172. */
  173. void setClassNames ( const OBJREC::ClassNames * _classNames ) ;
  174. void setIterationCountSuffix( const int & _iterationCountSuffix);
  175. ///////////////////// INTERFACE PERSISTENT /////////////////////
  176. // interface specific methods for store and restore
  177. ///////////////////// INTERFACE PERSISTENT /////////////////////
  178. /**
  179. * @brief Load active-segmentation-object from external file (stream)
  180. * @author Alexander Freytag
  181. */
  182. virtual void restore ( std::istream & is, int format = 0 );
  183. /**
  184. * @brief Save active-segmentation-object to external file (stream)
  185. * @author Alexander Freytag
  186. */
  187. virtual void store( std::ostream & os, int format = 0 ) const;
  188. /**
  189. * @brief Clear active-segmentation-object object
  190. * @author Alexander Freytag
  191. */
  192. virtual void clear ();
  193. };
  194. } // namespace
  195. #endif