SemSegContextTree.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @file SemSegContextTree.h
  3. * @brief Context Trees -> Combination of decision tree and context information
  4. * @author Björn Fröhlich
  5. * @date 29.11.2011
  6. */
  7. #ifndef SemSegContextTreeINCLUDE
  8. #define SemSegContextTreeINCLUDE
  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/Operations.h"
  21. namespace OBJREC
  22. {
  23. /** Localization system */
  24. class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
  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<Operation*> > 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. /** use Gradient image or not */
  78. bool useGradient;
  79. /** use Color features from van de Weijer or not */
  80. bool useWeijer;
  81. /** use additional input Layer or not */
  82. bool useAdditionalLayer;
  83. /** use external image categorization to avoid some classes */
  84. bool useCategorization;
  85. /** categorization information for external categorization */
  86. std::string cndir;
  87. /** how to handle each channel
  88. * 0: simple grayvalue features
  89. * 1: which pixel belongs to which region
  90. * 2: grayvalue integral images
  91. * 3: context integral images
  92. * 4: simple context features
  93. */
  94. std::vector<int> channelType;
  95. /** list of channels per feature type */
  96. std::vector<std::vector<int> > channelsPerType;
  97. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  98. bool useHoiemFeatures;
  99. /** save / load trained icf classifier */
  100. bool saveLoadData;
  101. /** file location of trained icf classifier */
  102. std::string fileLocation;
  103. /** first iteration or not */
  104. bool firstiteration;
  105. /** amount of grayvalue Channels */
  106. int rawChannels;
  107. /** classifier for categorization */
  108. OBJREC::GPHIKClassifierNICE *fasthik;
  109. /** unique numbers for nodes */
  110. int uniquenumber;
  111. /**
  112. * @brief initOperations initialize the operation types
  113. */
  114. void initOperations();
  115. /**
  116. * @brief train the actual training method
  117. * @param trainp pointer to training data
  118. */
  119. void train ( const LabeledSet * trainp );
  120. /**
  121. * @brief updateProbabilityMaps computes probability maps for context features
  122. * @param nodeIndices matrix with current node for each feature
  123. * @param feats output MCI3D (must be initilized)
  124. * @param firstChannel index of the first channel
  125. */
  126. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  127. /**
  128. * @brief computeRayFeatImage computes ray feature images using canny filter
  129. * @param feats output MCI3D (must be initilized)
  130. * @param firstChannel index of the first channel
  131. */
  132. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  133. /**
  134. * @brief addFeatureMaps initializes the selected feature channels
  135. * @param imgData output MCI3D (must be initilized)
  136. * @param filelist a list of image file names representing slices of a stack
  137. * @param amountRegions the amount of regions created by the segmentation
  138. **/
  139. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  140. /**
  141. * compute best split for current settings
  142. * @param feats features
  143. * @param nodeIndices matrix with current node for each feature
  144. * @param labels labels for each feature
  145. * @param node current node
  146. * @param splitfeat output selected feature dimension
  147. * @param splitval output threshold for selected feature
  148. * @return double best information gain value
  149. */
  150. 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, Operation *&splitop, double &splitval, const int &tree, std::vector<std::vector<std::vector<double> > > &regionProbs );
  151. /**
  152. * @brief computes the mean probability for a given class over all trees
  153. * @param x x position
  154. * @param y y position
  155. * @param z z position
  156. * @param channel current class
  157. * @param nodeIndices matrix with current node for each feature
  158. * @return double mean value
  159. **/
  160. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  161. public:
  162. /** simple constructor */
  163. SemSegContextTree ();
  164. /** constructor */
  165. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  166. /** simple destructor */
  167. virtual ~SemSegContextTree();
  168. /**
  169. * classify each pixel of a single 3d image
  170. * @param imgData input data
  171. * @param segresult segmentation results
  172. * @param probabilities probabilities for each pixel
  173. */
  174. void classify ( NICE::MultiChannelImage3DT<double> &imgData,
  175. NICE::MultiChannelImageT<double> & segresult,
  176. NICE::MultiChannelImage3DT<double> & probabilities,
  177. const std::vector<std::string> & filelist );
  178. /**
  179. * the training method with checking for already existing trained classifier from file
  180. * @param md training data
  181. */
  182. void train ( const MultiDataset *md );
  183. bool active3DMode ()
  184. {
  185. return run3Dseg;
  186. }
  187. /**
  188. * @brief load all data to is stream
  189. *
  190. * @param is input stream
  191. * @param format has no influence
  192. * @return void
  193. **/
  194. virtual void restore ( std::istream & is, int format = 0 );
  195. /**
  196. * @brief save all data to is stream
  197. *
  198. * @param os output stream
  199. * @param format has no influence
  200. * @return void
  201. **/
  202. virtual void store ( std::ostream & os, int format = 0 ) const;
  203. /**
  204. * @brief clean up
  205. *
  206. * @return void
  207. **/
  208. virtual void clear () {}
  209. };
  210. } // namespace
  211. #endif