SemSegContextTree3D.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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-vislearning includes
  12. #include <vislearning/features/localfeatures/LocalFeatureColorWeijer.h>
  13. #include <vislearning/classifier/fpclassifier/gphik/FPCGPHIK.h>
  14. // nice-segmentation includes
  15. #include <segmentation/RegionSegmentationMethod.h>
  16. // nice-semseg includes
  17. #include "SemanticSegmentation.h"
  18. #include "operations/Operations3D.h"
  19. namespace OBJREC
  20. {
  21. /** Localization system */
  22. class SemSegContextTree3D : public SemanticSegmentation
  23. {
  24. private:
  25. /** Segmentation Method */
  26. RegionSegmentationMethod *segmentation;
  27. /** tree -> saved as vector of nodes */
  28. std::vector<std::vector<TreeNode> > forest;
  29. /** local features */
  30. LocalFeatureColorWeijer *lfcw;
  31. /** whether to run in 3D mode or not */
  32. bool run3Dseg;
  33. /** whether to use a particular feature type or not */
  34. bool useFeat0, useFeat1, useFeat2, useFeat3, useFeat4;
  35. /** array of usable feature types*/
  36. std::vector<int> featTypes;
  37. /** Number of trees used for the forest */
  38. int nbTrees;
  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. int labelIncrement;
  64. /** prototype operations for pairwise features */
  65. std::vector<std::vector<Operation3D*> > ops;
  66. /** use alternative calculation for information gain */
  67. bool useShannonEntropy;
  68. /** Classnames */
  69. ClassNames classnames;
  70. /** train selection */
  71. std::set<int> forbidden_classes;
  72. /** Configfile */
  73. const NICE::Config *conf;
  74. /** use pixelwise labeling or regionlabeling with additional segmenation */
  75. bool pixelWiseLabeling;
  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. /** 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. /** amount of grayvalue Channels */
  99. int rawChannels;
  100. /** classifier for categorization */
  101. OBJREC::FPCGPHIK *fasthik;
  102. /** unique numbers for nodes */
  103. int uniquenumber;
  104. /**
  105. * @brief initOperations initialize the operation types
  106. */
  107. void initOperations();
  108. /**
  109. * @brief updateProbabilityMaps computes probability maps for context features
  110. * @param nodeIndices matrix with current node for each feature
  111. * @param feats output MCI3D (must be initilized)
  112. * @param firstChannel index of the first channel
  113. */
  114. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  115. /**
  116. * @brief computeRayFeatImage computes ray feature images using canny filter
  117. * @param feats output MCI3D (must be initilized)
  118. * @param firstChannel index of the first channel
  119. */
  120. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  121. /**
  122. * @brief addFeatureMaps initializes the selected feature channels
  123. * @param imgData output MCI3D (must be initilized)
  124. * @param filelist a list of image file names representing slices of a stack
  125. * @param amountRegions the amount of regions created by the segmentation
  126. **/
  127. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  128. /**
  129. * @brief compute best split for current settings
  130. * @param feats features
  131. * @param nodeIndices matrix with current node for each feature
  132. * @param labels labels for each feature
  133. * @param node current node
  134. * @param splitfeat output selected feature dimension
  135. * @param splitval output threshold for selected feature
  136. * @return double best information gain value
  137. */
  138. 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 );
  139. /**
  140. * @brief computes the mean probability for a given class over all trees
  141. * @param x x position
  142. * @param y y position
  143. * @param z z position
  144. * @param channel current class
  145. * @param nodeIndices matrix with current node for each feature
  146. * @return double mean value
  147. **/
  148. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  149. public:
  150. /** simple constructor */
  151. SemSegContextTree3D ();
  152. /** constructor */
  153. SemSegContextTree3D ( const NICE::Config *conf,
  154. const ClassNames *classNames );
  155. /** simple destructor */
  156. virtual ~SemSegContextTree3D();
  157. /**
  158. * classify each voxel of a 3D image (image stack)
  159. * @param filelist filename list of images that represent slices of a stack
  160. * @param segresult segmentation results (output)
  161. * @param probabilities probabilities for each pixel (output)
  162. */
  163. void classify ( const std::vector<std::string> & filelist,
  164. NICE::MultiChannelImageT<int> & segresult,
  165. NICE::MultiChannelImage3DT<double> & probabilities );
  166. /**
  167. * @brief train the actual training method
  168. * @param trainp pointer to training data
  169. */
  170. void train ( const LabeledSet * trainp );
  171. /**
  172. * the training method with checking for already existing trained classifier from file
  173. * @param md training data
  174. */
  175. void train ( const MultiDataset *md );
  176. // deprecated stuff
  177. virtual void semanticseg ( OBJREC::CachedExample *ce,
  178. NICE::ImageT<int> & segresult,
  179. NICE::MultiChannelImageT<double> & probabilities )
  180. {}
  181. bool active3DMode ()
  182. {
  183. return run3Dseg;
  184. }
  185. /**
  186. * @brief load all data to is stream
  187. *
  188. * @param is input stream
  189. * @param format has no influence
  190. * @return void
  191. **/
  192. virtual void restore ( std::istream & is, int format = 0 );
  193. /**
  194. * @brief save all data to is stream
  195. *
  196. * @param os output stream
  197. * @param format has no influence
  198. * @return void
  199. **/
  200. virtual void store ( std::ostream & os, int format = 0 ) const;
  201. /**
  202. * @brief clean up
  203. *
  204. * @return void
  205. **/
  206. virtual void clear () {}
  207. };
  208. } // namespace
  209. #endif