SemSegContextTree3D.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/classifier/fpclassifier/gphik/FPCGPHIK.h>
  13. // nice-segmentation includes
  14. #include <segmentation/RegionSegmentationMethod.h>
  15. // nice-semseg includes
  16. #include "SemanticSegmentation.h"
  17. #include "operations/SimpleOperationPool.h"
  18. #include "operations/RegionOperationPool.h"
  19. #include "operations/RectangleOperationPool.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. /** whether to use a particular feature type or not */
  31. bool useFeat0, useFeat1, useFeat2, useFeat3, useFeat4;
  32. /** array of usable feature types*/
  33. std::vector<int> featTypes;
  34. /** Number of trees used for the forest */
  35. int nbTrees;
  36. /** maximum samples for tree */
  37. int maxSamples;
  38. /** size for neighbourhood */
  39. int windowSize;
  40. /** how many feats should be considered for a split */
  41. int featsPerSplit;
  42. /** count samples per label */
  43. //std::map<int, int> labelcounter;
  44. /** map of labels */
  45. std::map<int, int> labelmap;
  46. /** map of labels inverse*/
  47. std::map<int, int> labelmapback;
  48. /** scalefactor for balancing for each class */
  49. std::vector<double> a;
  50. /** the minimum number of features allowed in a leaf */
  51. int minFeats;
  52. /** maximal depth of tree */
  53. int maxDepth;
  54. /** current depth for training */
  55. int depth;
  56. /** how many splittests */
  57. int randomTests;
  58. int labelIncrement;
  59. /** prototype operations for features */
  60. std::vector<OperationPool*> ops;
  61. /** use alternative calculation for information gain */
  62. bool useShannonEntropy;
  63. /** Classnames */
  64. ClassNames classnames;
  65. /** train selection */
  66. std::set<int> forbidden_classes;
  67. /** Configfile */
  68. const NICE::Config *conf;
  69. /** use pixelwise labeling or regionlabeling with additional segmenation */
  70. bool pixelWiseLabeling;
  71. /** whether to use alternative tristimulus for CIE_Lab that matches openCV or not */
  72. bool useAltTristimulus;
  73. /** use Gradient image or not */
  74. bool useGradient;
  75. /** use additional input layers or not */
  76. bool useAdditionalLayer;
  77. /** how many additional input layers */
  78. int numAdditionalLayer;
  79. /** use external image categorization to avoid some classes */
  80. bool useCategorization;
  81. /** categorization information for external categorization */
  82. std::string cndir;
  83. /** list of channels per feature type */
  84. std::vector<std::vector<int> > channelsPerType;
  85. /** save / load trained icf classifier */
  86. bool saveLoadData;
  87. /** file location of trained icf classifier */
  88. std::string fileLocation;
  89. /** first iteration or not */
  90. bool firstiteration;
  91. /** amount of grayvalue Channels */
  92. int rawChannels;
  93. /** classifier for categorization */
  94. OBJREC::FPCGPHIK *fasthik;
  95. /** unique numbers for nodes */
  96. int uniquenumber;
  97. /**
  98. * @brief initOperations initialize the operation types
  99. */
  100. void initOperations();
  101. /**
  102. * @brief updateProbabilityMaps computes probability maps for context features
  103. * @param nodeIndices matrix with current node for each feature
  104. * @param feats output MCI3D (must be initilized)
  105. * @param firstChannel index of the first channel
  106. */
  107. void updateProbabilityMaps ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  108. /**
  109. * @brief computeRayFeatImage computes ray feature images using canny filter
  110. * @param feats output MCI3D (must be initilized)
  111. * @param firstChannel index of the first channel
  112. */
  113. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  114. /**
  115. * @brief addFeatureMaps initializes the selected feature channels
  116. * @param imgData output MCI3D (must be initilized)
  117. * @param filelist a list of image file names representing slices of a stack
  118. * @param amountRegions the amount of regions created by the segmentation
  119. **/
  120. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  121. /**
  122. * @brief compute best split for current settings
  123. * @param feats features
  124. * @param nodeIndices matrix with current node for each feature
  125. * @param labels labels for each feature
  126. * @param node current node
  127. * @param splitfeat output selected feature dimension
  128. * @param splitval output threshold for selected feature
  129. * @return double best information gain value
  130. */
  131. 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 );
  132. /**
  133. * @brief computes the mean probability for a given class over all trees
  134. * @param x x position
  135. * @param y y position
  136. * @param z z position
  137. * @param channel current class
  138. * @param nodeIndices matrix with current node for each feature
  139. * @return double mean value
  140. **/
  141. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  142. public:
  143. /** simple constructor */
  144. SemSegContextTree3D ();
  145. /** constructor */
  146. SemSegContextTree3D ( const NICE::Config *conf,
  147. const ClassNames *classNames );
  148. /** simple destructor */
  149. virtual ~SemSegContextTree3D();
  150. /**
  151. * classify each voxel of a 3D image (image stack)
  152. * @param filelist filename list of images that represent slices of a stack
  153. * @param segresult segmentation results (output)
  154. * @param probabilities probabilities for each pixel (output)
  155. */
  156. void classify ( const std::vector<std::string> & filelist,
  157. NICE::MultiChannelImageT<int> & segresult,
  158. NICE::MultiChannelImage3DT<double> & probabilities );
  159. /**
  160. * @brief train the actual training method
  161. * @param trainp pointer to training data
  162. */
  163. void train ( const LabeledSet * trainp );
  164. /**
  165. * the training method with checking for already existing trained classifier from file
  166. * @param md training data
  167. */
  168. void train ( const MultiDataset *md );
  169. // deprecated stuff
  170. void semanticseg ( CachedExample *ce,
  171. NICE::ImageT<int> & segresult,
  172. NICE::MultiChannelImageT<double> & probabilities )
  173. {}
  174. void semanticseg ( CachedExample *ce,
  175. NICE::MultiChannelImageT<int> & segresult,
  176. NICE::MultiChannelImage3DT<double> & probabilities )
  177. {}
  178. void printFeatureStatistics();
  179. bool active3DMode ()
  180. {
  181. return run3Dseg;
  182. }
  183. /**
  184. * @brief load all data to is stream
  185. *
  186. * @param is input stream
  187. * @param format has no influence
  188. * @return void
  189. **/
  190. virtual void restore ( std::istream & is, int format = 0 );
  191. /**
  192. * @brief save all data to is stream
  193. *
  194. * @param os output stream
  195. * @param format has no influence
  196. * @return void
  197. **/
  198. virtual void store ( std::ostream & os, int format = 0 ) const;
  199. /**
  200. * @brief clean up
  201. *
  202. * @return void
  203. **/
  204. virtual void clear () {}
  205. };
  206. } // namespace
  207. #endif