SemanticSegmentation.h 9.1 KB

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