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