SemSegContextTree3D.h 7.6 KB

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