SemanticSegmentation.h 8.2 KB

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