SemSegContextTree.h 7.9 KB

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