SemanticSegmentation.h 8.1 KB

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