SemSegContextTree3D.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/SimpleOperationPool.h"
  19. #include "operations/RegionOperationPool.h"
  20. #include "operations/RectangleOperationPool.h"
  21. namespace OBJREC
  22. {
  23. /** Localization system */
  24. class SemSegContextTree3D : public SemanticSegmentation
  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 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. /** how many feats should be considered for a split */
  44. int featsPerSplit;
  45. /** count samples per label */
  46. //std::map<int, int> labelcounter;
  47. /** map of labels */
  48. std::map<int, int> labelmap;
  49. /** map of labels inverse*/
  50. std::map<int, int> labelmapback;
  51. /** scalefactor for balancing for each class */
  52. std::vector<double> a;
  53. /** the minimum number of features allowed in a leaf */
  54. int minFeats;
  55. /** maximal depth of tree */
  56. int maxDepth;
  57. /** current depth for training */
  58. int depth;
  59. /** how many splittests */
  60. int randomTests;
  61. int labelIncrement;
  62. /** prototype operations for features */
  63. std::vector<OperationPool*> ops;
  64. /** use alternative calculation for information gain */
  65. bool useShannonEntropy;
  66. /** Classnames */
  67. ClassNames classnames;
  68. /** train selection */
  69. std::set<int> forbidden_classes;
  70. /** Configfile */
  71. const NICE::Config *conf;
  72. /** use pixelwise labeling or regionlabeling with additional segmenation */
  73. bool pixelWiseLabeling;
  74. /** whether to use alternative tristimulus for CIE_Lab that matches openCV or not */
  75. bool useAltTristimulus;
  76. /** use Gradient image or not */
  77. bool useGradient;
  78. /** use Color features from van de Weijer or not */
  79. bool useWeijer;
  80. /** use additional input Layer or not */
  81. bool useAdditionalLayer;
  82. /** use external image categorization to avoid some classes */
  83. bool useCategorization;
  84. /** categorization information for external categorization */
  85. std::string cndir;
  86. /** list of channels per feature type */
  87. std::vector<std::vector<int> > channelsPerType;
  88. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  89. bool useHoiemFeatures;
  90. /** save / load trained icf classifier */
  91. bool saveLoadData;
  92. /** file location of trained icf classifier */
  93. std::string fileLocation;
  94. /** first iteration or not */
  95. bool firstiteration;
  96. /** amount of grayvalue Channels */
  97. int rawChannels;
  98. /** classifier for categorization */
  99. OBJREC::FPCGPHIK *fasthik;
  100. /** unique numbers for nodes */
  101. int uniquenumber;
  102. /**
  103. * @brief initOperations initialize the operation types
  104. */
  105. void initOperations();
  106. /**
  107. * @brief updateProbabilityMaps computes probability maps for context features
  108. * @param nodeIndices matrix with current node for each feature
  109. * @param feats output MCI3D (must be initilized)
  110. * @param firstChannel index of the first channel
  111. */
  112. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  113. /**
  114. * @brief computeRayFeatImage computes ray feature images using canny filter
  115. * @param feats output MCI3D (must be initilized)
  116. * @param firstChannel index of the first channel
  117. */
  118. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  119. /**
  120. * @brief addFeatureMaps initializes the selected feature channels
  121. * @param imgData output MCI3D (must be initilized)
  122. * @param filelist a list of image file names representing slices of a stack
  123. * @param amountRegions the amount of regions created by the segmentation
  124. **/
  125. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  126. /**
  127. * @brief compute best split for current settings
  128. * @param feats features
  129. * @param nodeIndices matrix with current node for each feature
  130. * @param labels labels for each feature
  131. * @param node current node
  132. * @param splitfeat output selected feature dimension
  133. * @param splitval output threshold for selected feature
  134. * @return double best information gain value
  135. */
  136. 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 );
  137. /**
  138. * @brief computes the mean probability for a given class over all trees
  139. * @param x x position
  140. * @param y y position
  141. * @param z z position
  142. * @param channel current class
  143. * @param nodeIndices matrix with current node for each feature
  144. * @return double mean value
  145. **/
  146. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  147. public:
  148. /** simple constructor */
  149. SemSegContextTree3D ();
  150. /** constructor */
  151. SemSegContextTree3D ( const NICE::Config *conf,
  152. const ClassNames *classNames );
  153. /** simple destructor */
  154. virtual ~SemSegContextTree3D();
  155. /**
  156. * classify each voxel of a 3D image (image stack)
  157. * @param filelist filename list of images that represent slices of a stack
  158. * @param segresult segmentation results (output)
  159. * @param probabilities probabilities for each pixel (output)
  160. */
  161. void classify ( const std::vector<std::string> & filelist,
  162. NICE::MultiChannelImageT<int> & segresult,
  163. NICE::MultiChannelImage3DT<double> & probabilities );
  164. /**
  165. * @brief train the actual training method
  166. * @param trainp pointer to training data
  167. */
  168. void train ( const LabeledSet * trainp );
  169. /**
  170. * the training method with checking for already existing trained classifier from file
  171. * @param md training data
  172. */
  173. void train ( const MultiDataset *md );
  174. // deprecated stuff
  175. void semanticseg ( CachedExample *ce,
  176. NICE::ImageT<int> & segresult,
  177. NICE::MultiChannelImageT<double> & probabilities )
  178. {}
  179. void semanticseg ( CachedExample *ce,
  180. NICE::MultiChannelImageT<int> & segresult,
  181. NICE::MultiChannelImage3DT<double> & probabilities )
  182. {}
  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