SemSegRegionBased.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @file SemSegRegionBased.h
  3. * @brief new semantic segmentation method using regions
  4. * @author Björn Fröhlich
  5. * @date 01/29/2010
  6. */
  7. #ifndef SemSegRegionBasedINCLUDE
  8. #define SemSegRegionBasedINCLUDE
  9. #include "SemanticSegmentation.h"
  10. #include "objrec/classifier/classifierbase/FeaturePoolClassifier.h"
  11. #include "objrec/classifier/classifierbase/VecClassifier.h"
  12. #include "objrec/segmentation/RegionSegmentationMethod.h"
  13. #include "objrec/math/mathbase/Featuretype.h"
  14. #include "objrec/features/regionfeatures/RegionFeatures.h"
  15. #include "objrec/features/localfeatures/LocalFeature.h"
  16. #include "objrec/math/ftransform/PCA.h"
  17. #include "objrec-froehlichexp/semseg/postsegmentation/PPGraphCut.h"
  18. namespace OBJREC
  19. {
  20. class SemSegRegionBased : public SemanticSegmentation
  21. {
  22. protected:
  23. //! destination for saving intermediate steps
  24. bool save_cache, read_cache;
  25. std::string cache;
  26. string classifiercache;
  27. //! used ClassNames
  28. ClassNames cn;
  29. //! Classifier
  30. VecClassifier *vclassifier;
  31. FeaturePoolClassifier *fpc;
  32. //! Configuration File
  33. const Config *conf;
  34. //! Segmentation Method
  35. RegionSegmentationMethod *rsm;
  36. //! using color Weijer features or not
  37. RegionFeatures *rfc;
  38. //! using HoGFeatures or not
  39. RegionFeatures *rfhog;
  40. //! using BoV or not
  41. RegionFeatures *rfbov;
  42. //! Moosmann Codebook (alternative BoV approach)
  43. RegionFeatures *rfbovcrdf;
  44. //! old method like used in Csurka
  45. RegionFeatures *rfCsurka;
  46. //! features for BoV
  47. LocalFeature *siftFeats;
  48. //! using structure feature
  49. RegionFeatures *rfstruct;
  50. //! MRF optimization
  51. PPGraphCut *gcopt;
  52. public:
  53. /** constructor
  54. * @param conf needs a configfile
  55. * @param md and a MultiDataset (contains images and other things)
  56. */
  57. SemSegRegionBased(const Config *c, const MultiDataset *md);
  58. /** simple destructor */
  59. virtual ~SemSegRegionBased();
  60. /** The trainingstep
  61. * @param md and a MultiDataset (contains images and other things)
  62. */
  63. void train(const MultiDataset *md);
  64. /** The main procedure. Input: Image, Output: Segmented Image with pixelwise labeles and the probabilities
  65. * @param ce image data
  66. * @param segresult result of the semantic segmentation with a label for each pixel
  67. * @param probabilities multi-channel image with one channel for each class and corresponding probabilities for each pixel
  68. */
  69. void semanticseg(CachedExample *ce, NICE::Image & segresult, GenericImage<double> & probabilities);
  70. /**
  71. * get all features for an Image and save them in Examples
  72. * @param cimg input image
  73. * @param mask region mask
  74. * @param rg region graph
  75. * @param feats output features
  76. */
  77. void getFeats(const NICE::ColorImage &cimg, const NICE::Matrix &mask, const RegionGraph &rg, vector<vector< FeatureType> > &feats) const;
  78. /**
  79. * computes or reads features and corresponding labels for learnHighLevel()
  80. * @param perm input permutation
  81. * @param feats output features
  82. * @param label output label
  83. * @param examples output examples (including label)
  84. * @param mode mode 1 for examples, mode 0 for VVector
  85. */
  86. void computeLF(LabeledSet::Permutation perm, VVector &feats, vector<int> &label, Examples &examples, int mode);
  87. /**
  88. * Computes HighLevel Codebooks (i.e. GMM or PCA) if necessary
  89. * @param perm training examples
  90. */
  91. void learnHighLevel(LabeledSet::Permutation perm);
  92. /**
  93. * trains the classifier
  94. * @param feats features
  95. */
  96. void trainClassifier(vector<vector<FeatureType> > &feats, Examples &examples);
  97. /**
  98. * Convert features into examples
  99. * @param feats input features
  100. * @param examples features as examples
  101. */
  102. void getExample(const vector<vector<FeatureType> > &feats, Examples &examples);
  103. /**
  104. * create featurepool depending on used features
  105. * @param feats input features
  106. * @param fp feature pool
  107. */
  108. void getFeaturePool( const vector<vector<FeatureType> > &feats, FeaturePool &fp);
  109. /**
  110. * classify the given features
  111. * @param feats input features
  112. * @param examples examples
  113. * @param probs probability for each region
  114. */
  115. void classify(const vector<vector<FeatureType> > &feats, Examples &examples, vector<vector<double> > &probs);
  116. /**
  117. * set the label of each region the to most probable class
  118. * @param rg
  119. * @param probs
  120. */
  121. void labelRegions(RegionGraph &rg, vector<vector<double> > &probs);
  122. /**
  123. * set label of each pixel to label of corresponding region
  124. * @param segresult result image
  125. * @param mask region mask
  126. * @param rg region graph
  127. */
  128. void labelImage(NICE::Image &segresult, NICE::Matrix &mask,RegionGraph &rg);
  129. /**
  130. * get the label for each region from the groundtruth for learning step and save them in rg
  131. * @param mask region mask
  132. * @param rg region graph
  133. * @param pixelLabels Groundtruth images
  134. */
  135. void getRegionLabel(NICE::Matrix &mask, RegionGraph &rg, NICE::Image &pixelLabels);
  136. /**
  137. * train pca
  138. * @param feats input features
  139. * @param pca pca
  140. * @param dim new dimension
  141. * @param fn destination filename
  142. */
  143. void initializePCA ( const VVector &feats, PCA &pca, int dim, string &fn );
  144. /**
  145. * transform features using a given pca
  146. * @param feats input and output features
  147. * @param pca
  148. */
  149. void transformFeats(VVector &feats, PCA &pca);
  150. };
  151. } // namespace
  152. #endif