SemSegContextTree3D.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "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;
  36. /** array of usable feature types*/
  37. std::vector<int> featTypes;
  38. /** Number of trees used for the forest */
  39. int nbTrees;
  40. /** maximum samples for tree */
  41. int maxSamples;
  42. /** size for neighbourhood */
  43. int windowSize;
  44. /** multiplier for window size if context feature */
  45. int contextMultiplier;
  46. /** how many feats should be considered for a split */
  47. int featsPerSplit;
  48. /** count samples per label */
  49. //std::map<int, int> labelcounter;
  50. /** map of labels */
  51. std::map<int, int> labelmap;
  52. /** map of labels inverse*/
  53. std::map<int, int> labelmapback;
  54. /** scalefactor for balancing for each class */
  55. std::vector<double> a;
  56. /** the minimum number of features allowed in a leaf */
  57. int minFeats;
  58. /** maximal depth of tree */
  59. int maxDepth;
  60. /** current depth for training */
  61. int depth;
  62. /** how many splittests */
  63. int randomTests;
  64. int labelIncrement;
  65. /** prototype operations for pairwise features */
  66. std::vector<std::vector<Operation3D*> > ops;
  67. /** use alternative calculation for information gain */
  68. bool useShannonEntropy;
  69. /** Classnames */
  70. ClassNames classnames;
  71. /** train selection */
  72. std::set<int> forbidden_classes;
  73. /** Configfile */
  74. const NICE::Config *conf;
  75. /** use pixelwise labeling or regionlabeling with additional segmenation */
  76. bool pixelWiseLabeling;
  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 updateProbabilityMaps computes probability maps for context features
  119. * @param nodeIndices matrix with current node for each feature
  120. * @param feats output MCI3D (must be initilized)
  121. * @param firstChannel index of the first channel
  122. */
  123. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  124. /**
  125. * @brief computeRayFeatImage computes ray feature images using canny filter
  126. * @param feats output MCI3D (must be initilized)
  127. * @param firstChannel index of the first channel
  128. */
  129. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  130. /**
  131. * @brief addFeatureMaps initializes the selected feature channels
  132. * @param imgData output MCI3D (must be initilized)
  133. * @param filelist a list of image file names representing slices of a stack
  134. * @param amountRegions the amount of regions created by the segmentation
  135. **/
  136. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  137. /**
  138. * @brief compute best split for current settings
  139. * @param feats features
  140. * @param nodeIndices matrix with current node for each feature
  141. * @param labels labels for each feature
  142. * @param node current node
  143. * @param splitfeat output selected feature dimension
  144. * @param splitval output threshold for selected feature
  145. * @return double best information gain value
  146. */
  147. 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 );
  148. /**
  149. * @brief computes the mean probability for a given class over all trees
  150. * @param x x position
  151. * @param y y position
  152. * @param z z position
  153. * @param channel current class
  154. * @param nodeIndices matrix with current node for each feature
  155. * @return double mean value
  156. **/
  157. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  158. public:
  159. /** simple constructor */
  160. SemSegContextTree3D ();
  161. /** constructor */
  162. SemSegContextTree3D ( const NICE::Config *conf,
  163. const ClassNames *classNames );
  164. /** simple destructor */
  165. virtual ~SemSegContextTree3D();
  166. /**
  167. * classify each voxel of a 3D image (image stack)
  168. * @param filelist filename list of images that represent slices of a stack
  169. * @param segresult segmentation results (output)
  170. * @param probabilities probabilities for each pixel (output)
  171. */
  172. void classify ( const std::vector<std::string> & filelist,
  173. NICE::MultiChannelImageT<int> & segresult,
  174. NICE::MultiChannelImage3DT<double> & probabilities );
  175. /**
  176. * @brief train the actual training method
  177. * @param trainp pointer to training data
  178. */
  179. void train ( const LabeledSet * trainp );
  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::ImageT<int> & segresult,
  188. NICE::MultiChannelImageT<double> & probabilities )
  189. {}
  190. bool active3DMode ()
  191. {
  192. return run3Dseg;
  193. }
  194. /**
  195. * @brief load all data to is stream
  196. *
  197. * @param is input stream
  198. * @param format has no influence
  199. * @return void
  200. **/
  201. virtual void restore ( std::istream & is, int format = 0 );
  202. /**
  203. * @brief save all data to is stream
  204. *
  205. * @param os output stream
  206. * @param format has no influence
  207. * @return void
  208. **/
  209. virtual void store ( std::ostream & os, int format = 0 ) const;
  210. /**
  211. * @brief clean up
  212. *
  213. * @return void
  214. **/
  215. virtual void clear () {}
  216. };
  217. } // namespace
  218. #endif