SemanticSegmentation.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * @brief Pre-processing and classification of a 3D image
  91. * @param filelist filename list of images that represent slices of a stack
  92. * @param segresult segmentation results (output)
  93. * @param probabilities probabilities for each pixel (output)
  94. */
  95. void semanticseg ( const std::vector<std::string> & filelist,
  96. NICE::MultiChannelImageT<int> & segresult,
  97. NICE::MultiChannelImage3DT<double> & probabilities );
  98. /**
  99. * Classify each voxel of a 3D image (image stack)
  100. * @author Sven Sickert
  101. * @param filelist filename list of images that represent slices of a stack
  102. * @param segresult segmentation results (output)
  103. * @param probabilities probabilities for each pixel (output)
  104. */
  105. virtual void classify ( const std::vector<std::string> & filelist,
  106. NICE::MultiChannelImageT<int> & segresult,
  107. NICE::MultiChannelImage3DT<double> & probabilities );
  108. /** this function has to be overloaded by all subclasses
  109. @param ce image data
  110. @param segresult result of the semantic segmentation with a label for each
  111. pixel
  112. @param probabilities multi-channel image with one channel for each class and
  113. corresponding probabilities for each pixel
  114. */
  115. virtual void semanticseg ( OBJREC::CachedExample *ce,
  116. NICE::ImageT<int> & segresult,
  117. NICE::MultiChannelImageT<double> & probabilities ) = 0;
  118. /**
  119. * Classify each voxel of a 3D image (image stack)
  120. * @author Sven Sickert
  121. * @param ce 3d image data
  122. * @param segresult segmentation results (output)
  123. * @param probabilities probabilities for each pixel (output)
  124. */
  125. virtual void semanticseg ( OBJREC::CachedExample *ce,
  126. NICE::MultiChannelImageT<int> & segresult,
  127. NICE::MultiChannelImage3DT<double> & probabilities ) = 0;
  128. /** training function (has to be overloaded by all subclasses)
  129. * @param md the data set
  130. */
  131. virtual void train ( const MultiDataset * md ) = 0;
  132. /**
  133. * @brief train the actual training method
  134. * @param trainp pointer to training data
  135. */
  136. virtual void train ( const LabeledSet * trainp ){}
  137. /**
  138. * @brief Load image slices into a MultiChannelImage3DT
  139. * @author Sven Sickert
  140. * @param filelist filename list of images that represent slices of a stack
  141. * @param imgData output
  142. */
  143. template<class SrcP>
  144. void make3DImage ( const std::vector<std::string> & filelist,
  145. NICE::MultiChannelImage3DT<SrcP> & imgData );
  146. ///////////////////// ///////////////////// /////////////////////
  147. // DATA CONVERSION
  148. ///////////////////// ///////////////////// /////////////////////
  149. /**
  150. * convert different datatypes
  151. */
  152. void convertVVectorToExamples ( NICE::VVector &feats,OBJREC::Examples &examples, std::vector<int> &label );
  153. void convertExamplesToVVector ( NICE::VVector &feats,OBJREC::Examples &examples, std::vector<int> &label );
  154. void convertExamplesToLSet ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
  155. void convertLSetToExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec, const bool & removeOldDataPointer=false );
  156. void convertLSetToSparseExamples ( OBJREC::Examples &examples, OBJREC::LabeledSetVector &lvec );
  157. ///////////////////// ///////////////////// /////////////////////
  158. // ONLINE LEARNING
  159. ///////////////////// ///////////////////// /////////////////////
  160. virtual void addNewExample(const NICE::Vector & newExample, const int & newClassNo)
  161. {
  162. ROADWORKSADD;
  163. }
  164. /**
  165. * @brief Add those examples, which belong to the most novel region seen so far
  166. *
  167. * @return void
  168. **/
  169. virtual void addNovelExamples()
  170. {
  171. ROADWORKSADDNOVEL;
  172. }
  173. ///////////////////// ///////////////////// /////////////////////
  174. // GET / SET
  175. ///////////////////// ///////////////////// /////////////////////
  176. /**
  177. * @brief Get a pointer to the examples extracted from the most novel region seen so far
  178. *
  179. * @return Examples *
  180. **/
  181. virtual const Examples * getNovelExamples() const
  182. {
  183. ROADWORKSGETNOVEL;
  184. }
  185. /**
  186. * @brief Save probability maps of all classes to iamge files
  187. * @author Sven Sickert
  188. * @param prob class probability maps
  189. */
  190. void getProbabilityMap( const NICE::MultiChannelImage3DT<double> & prob );
  191. /**
  192. * @author Alexander Freytag
  193. * @date 06-02-2014 ( dd-mm-yyyy )
  194. */
  195. void setClassNames ( const OBJREC::ClassNames * _classNames ) ;
  196. void setIterationCountSuffix( const int & _iterationCountSuffix);
  197. ///////////////////// INTERFACE PERSISTENT /////////////////////
  198. // interface specific methods for store and restore
  199. ///////////////////// INTERFACE PERSISTENT /////////////////////
  200. /**
  201. * @brief Load active-segmentation-object from external file (stream)
  202. * @author Alexander Freytag
  203. */
  204. virtual void restore ( std::istream & is, int format = 0 );
  205. /**
  206. * @brief Save active-segmentation-object to external file (stream)
  207. * @author Alexander Freytag
  208. */
  209. virtual void store( std::ostream & os, int format = 0 ) const;
  210. /**
  211. * @brief Clear active-segmentation-object object
  212. * @author Alexander Freytag
  213. */
  214. virtual void clear ();
  215. };
  216. } // namespace
  217. #include "SemanticSegmentation.tcc"
  218. #endif