SemSegContextTree3D.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /** list of channels per feature type */
  90. std::vector<std::vector<int> > channelsPerType;
  91. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  92. bool useHoiemFeatures;
  93. /** save / load trained icf classifier */
  94. bool saveLoadData;
  95. /** file location of trained icf classifier */
  96. std::string fileLocation;
  97. /** first iteration or not */
  98. bool firstiteration;
  99. /** amount of grayvalue Channels */
  100. int rawChannels;
  101. /** classifier for categorization */
  102. OBJREC::GPHIKClassifierNICE *fasthik;
  103. /** unique numbers for nodes */
  104. int uniquenumber;
  105. /**
  106. * @brief initOperations initialize the operation types
  107. */
  108. void initOperations();
  109. /**
  110. * @brief updateProbabilityMaps computes probability maps for context features
  111. * @param nodeIndices matrix with current node for each feature
  112. * @param feats output MCI3D (must be initilized)
  113. * @param firstChannel index of the first channel
  114. */
  115. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  116. /**
  117. * @brief computeRayFeatImage computes ray feature images using canny filter
  118. * @param feats output MCI3D (must be initilized)
  119. * @param firstChannel index of the first channel
  120. */
  121. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  122. /**
  123. * @brief addFeatureMaps initializes the selected feature channels
  124. * @param imgData output MCI3D (must be initilized)
  125. * @param filelist a list of image file names representing slices of a stack
  126. * @param amountRegions the amount of regions created by the segmentation
  127. **/
  128. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  129. /**
  130. * @brief compute best split for current settings
  131. * @param feats features
  132. * @param nodeIndices matrix with current node for each feature
  133. * @param labels labels for each feature
  134. * @param node current node
  135. * @param splitfeat output selected feature dimension
  136. * @param splitval output threshold for selected feature
  137. * @return double best information gain value
  138. */
  139. 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 );
  140. /**
  141. * @brief computes the mean probability for a given class over all trees
  142. * @param x x position
  143. * @param y y position
  144. * @param z z position
  145. * @param channel current class
  146. * @param nodeIndices matrix with current node for each feature
  147. * @return double mean value
  148. **/
  149. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  150. public:
  151. /** simple constructor */
  152. SemSegContextTree3D ();
  153. /** constructor */
  154. SemSegContextTree3D ( const NICE::Config *conf,
  155. const ClassNames *classNames );
  156. /** simple destructor */
  157. virtual ~SemSegContextTree3D();
  158. /**
  159. * classify each voxel of a 3D image (image stack)
  160. * @param filelist filename list of images that represent slices of a stack
  161. * @param segresult segmentation results (output)
  162. * @param probabilities probabilities for each pixel (output)
  163. */
  164. void classify ( const std::vector<std::string> & filelist,
  165. NICE::MultiChannelImageT<int> & segresult,
  166. NICE::MultiChannelImage3DT<double> & probabilities );
  167. /**
  168. * @brief train the actual training method
  169. * @param trainp pointer to training data
  170. */
  171. void train ( const LabeledSet * trainp );
  172. /**
  173. * the training method with checking for already existing trained classifier from file
  174. * @param md training data
  175. */
  176. void train ( const MultiDataset *md );
  177. // deprecated stuff
  178. virtual void semanticseg ( OBJREC::CachedExample *ce,
  179. NICE::ImageT<int> & segresult,
  180. NICE::MultiChannelImageT<double> & probabilities )
  181. {}
  182. bool active3DMode ()
  183. {
  184. return run3Dseg;
  185. }
  186. /**
  187. * @brief load all data to is stream
  188. *
  189. * @param is input stream
  190. * @param format has no influence
  191. * @return void
  192. **/
  193. virtual void restore ( std::istream & is, int format = 0 );
  194. /**
  195. * @brief save all data to is stream
  196. *
  197. * @param os output stream
  198. * @param format has no influence
  199. * @return void
  200. **/
  201. virtual void store ( std::ostream & os, int format = 0 ) const;
  202. /**
  203. * @brief clean up
  204. *
  205. * @return void
  206. **/
  207. virtual void clear () {}
  208. };
  209. } // namespace
  210. #endif