SemSegContextTree3D.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @file SemSegContextTree3D.h
  3. * @brief Context Trees -> Combination of decision tree and context information
  4. * @author Björn Fröhlich, Sven Sickert
  5. * @date 29.11.2011
  6. */
  7. #ifndef SemSegContextTree3DINCLUDE
  8. #define SemSegContextTree3DINCLUDE
  9. // nice-core includes
  10. #include <core/vector/VVector.h>
  11. #include <core/image/MultiChannelImage3DT.h>
  12. // nice-gphik-exp includes
  13. #include <gp-hik-exp/GPHIKClassifierNICE.h>
  14. // nice-vislearning includes
  15. #include <vislearning/features/localfeatures/LocalFeatureColorWeijer.h>
  16. // nice-segmentation includes
  17. #include <segmentation/RegionSegmentationMethod.h>
  18. // nice-semseg includes
  19. #include "SemanticSegmentation.h"
  20. #include "semseg3d/semseg/operations/Operations3D.h"
  21. namespace OBJREC
  22. {
  23. /** Localization system */
  24. class SemSegContextTree3D : public SemanticSegmentation
  25. {
  26. private:
  27. /** Segmentation Method */
  28. RegionSegmentationMethod *segmentation;
  29. /** tree -> saved as vector of nodes */
  30. std::vector<std::vector<TreeNode> > forest;
  31. /** local features */
  32. LocalFeatureColorWeijer *lfcw;
  33. /** whether to run in 3D mode or not */
  34. bool run3Dseg;
  35. /** whether to use a particular feature type or not */
  36. bool useFeat0, useFeat1, useFeat2, useFeat3, useFeat4, useFeat5;
  37. /** array of usable feature types*/
  38. std::vector<int> featTypes;
  39. /** maximum samples for tree */
  40. int maxSamples;
  41. /** size for neighbourhood */
  42. int windowSize;
  43. /** multiplier for window size if context feature */
  44. int contextMultiplier;
  45. /** how many feats should be considered for a split */
  46. int featsPerSplit;
  47. /** count samples per label */
  48. std::map<int, int> labelcounter;
  49. /** map of labels */
  50. std::map<int, int> labelmap;
  51. /** map of labels inverse*/
  52. std::map<int, int> labelmapback;
  53. /** scalefactor for balancing for each class */
  54. std::vector<double> a;
  55. /** the minimum number of features allowed in a leaf */
  56. int minFeats;
  57. /** maximal depth of tree */
  58. int maxDepth;
  59. /** current depth for training */
  60. int depth;
  61. /** how many splittests */
  62. int randomTests;
  63. /** prototype operations for pairwise features */
  64. std::vector<std::vector<Operation3D*> > ops;
  65. /** use alternative calculation for information gain */
  66. bool useShannonEntropy;
  67. /** Classnames */
  68. ClassNames classnames;
  69. /** train selection */
  70. std::set<int> forbidden_classes;
  71. /** Configfile */
  72. const NICE::Config *conf;
  73. /** use pixelwise labeling or regionlabeling with additional segmenation */
  74. bool pixelWiseLabeling;
  75. /** Number of trees used for the forest */
  76. int nbTrees;
  77. /** whether to use alternative tristimulus for CIE_Lab that matches openCV or not */
  78. bool useAltTristimulus;
  79. /** use Gradient image or not */
  80. bool useGradient;
  81. /** use Color features from van de Weijer or not */
  82. bool useWeijer;
  83. /** use additional input Layer or not */
  84. bool useAdditionalLayer;
  85. /** use external image categorization to avoid some classes */
  86. bool useCategorization;
  87. /** categorization information for external categorization */
  88. std::string cndir;
  89. /** how to handle each channel
  90. * 0: simple grayvalue features
  91. * 1: which pixel belongs to which region
  92. * 2: grayvalue integral images
  93. * 3: context integral images
  94. * 4: simple context features
  95. */
  96. std::vector<int> channelType;
  97. /** list of channels per feature type */
  98. std::vector<std::vector<int> > channelsPerType;
  99. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  100. bool useHoiemFeatures;
  101. /** save / load trained icf classifier */
  102. bool saveLoadData;
  103. /** file location of trained icf classifier */
  104. std::string fileLocation;
  105. /** first iteration or not */
  106. bool firstiteration;
  107. /** amount of grayvalue Channels */
  108. int rawChannels;
  109. /** classifier for categorization */
  110. OBJREC::GPHIKClassifierNICE *fasthik;
  111. /** unique numbers for nodes */
  112. int uniquenumber;
  113. /**
  114. * @brief initOperations initialize the operation types
  115. */
  116. void initOperations();
  117. /**
  118. * @brief train the actual training method
  119. * @param trainp pointer to training data
  120. */
  121. void train ( const LabeledSet * trainp );
  122. /**
  123. * @brief updateProbabilityMaps computes probability maps for context features
  124. * @param nodeIndices matrix with current node for each feature
  125. * @param feats output MCI3D (must be initilized)
  126. * @param firstChannel index of the first channel
  127. */
  128. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  129. /**
  130. * @brief computeRayFeatImage computes ray feature images using canny filter
  131. * @param feats output MCI3D (must be initilized)
  132. * @param firstChannel index of the first channel
  133. */
  134. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  135. /**
  136. * @brief addFeatureMaps initializes the selected feature channels
  137. * @param imgData output MCI3D (must be initilized)
  138. * @param filelist a list of image file names representing slices of a stack
  139. * @param amountRegions the amount of regions created by the segmentation
  140. **/
  141. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  142. /**
  143. * compute best split for current settings
  144. * @param feats features
  145. * @param nodeIndices matrix with current node for each feature
  146. * @param labels labels for each feature
  147. * @param node current node
  148. * @param splitfeat output selected feature dimension
  149. * @param splitval output threshold for selected feature
  150. * @return double best information gain value
  151. */
  152. double getBestSplit ( std::vector<NICE::MultiChannelImage3DT<double> > &feats, std::vector<NICE::MultiChannelImage3DT<unsigned short int> > &nodeIndices, const std::vector<NICE::MultiChannelImageT<int> > &labels, int node, Operation3D *&splitop, double &splitval, const int &tree, std::vector<std::vector<std::vector<double> > > &regionProbs );
  153. /**
  154. * @brief computes the mean probability for a given class over all trees
  155. * @param x x position
  156. * @param y y position
  157. * @param z z position
  158. * @param channel current class
  159. * @param nodeIndices matrix with current node for each feature
  160. * @return double mean value
  161. **/
  162. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  163. public:
  164. /** simple constructor */
  165. SemSegContextTree3D ();
  166. /** constructor */
  167. SemSegContextTree3D ( const NICE::Config *conf, const MultiDataset *md );
  168. /** simple destructor */
  169. virtual ~SemSegContextTree3D();
  170. /**
  171. * classify each pixel of a single 3d image
  172. * @param imgData input data
  173. * @param segresult segmentation results
  174. * @param probabilities probabilities for each pixel
  175. */
  176. void classify ( NICE::MultiChannelImage3DT<double> &imgData,
  177. NICE::MultiChannelImageT<double> & segresult,
  178. NICE::MultiChannelImage3DT<double> & probabilities,
  179. const std::vector<std::string> & filelist );
  180. /**
  181. * the training method with checking for already existing trained classifier from file
  182. * @param md training data
  183. */
  184. void train ( const MultiDataset *md );
  185. // deprecated stuff
  186. virtual void semanticseg ( OBJREC::CachedExample *ce,
  187. NICE::Image & segresult,
  188. NICE::MultiChannelImageT<double> & probabilities );
  189. bool active3DMode ()
  190. {
  191. return run3Dseg;
  192. }
  193. /**
  194. * @brief load all data to is stream
  195. *
  196. * @param is input stream
  197. * @param format has no influence
  198. * @return void
  199. **/
  200. virtual void restore ( std::istream & is, int format = 0 );
  201. /**
  202. * @brief save all data to is stream
  203. *
  204. * @param os output stream
  205. * @param format has no influence
  206. * @return void
  207. **/
  208. virtual void store ( std::ostream & os, int format = 0 ) const;
  209. /**
  210. * @brief clean up
  211. *
  212. * @return void
  213. **/
  214. virtual void clear () {}
  215. };
  216. } // namespace
  217. #endif