brainrat-private  5.1.2
biovision.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2016 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef BRAINRAT_CLASSIFICATION_BIOVISION_H
12 #define BRAINRAT_CLASSIFICATION_BIOVISION_H
13 
14 //--- brainrat ----------------------------------------------------------
15 #include <brainrat/data/classes.h>
16 //--- aims -------------------------------------------------------------------
17 #include <aims/data/data_g.h>
18 #include <aims/utility/channel.h>
19 //--- cartobase --------------------------------------------------------------
20 #include <cartobase/smart/rcptr.h>
21 //----------------------------------------------------------------------------
22 #include <cartodata/volume/volume.h>
23 
24 namespace biovision {
25 
26 //============================================================================
27 // U T I L I T I E S
28 //============================================================================
29 
30  template< typename T1, typename T2>
31  inline bool pair_key_comparer( const std::pair<T1, T2>& p1,
32  const std::pair<T1, T2>& p2 )
33  {
34  return p1.first < p2.first;
35  }
36 
37 //============================================================================
38 // L E A R N I N G
39 //============================================================================
40 
42  {
43  public:
44 
45  LearningAlgorithm( int sizeT_im,
46  double clustering_sigma_min,
47  double clustering_sigma_ratio,
48  int32_t clustering_number_max,
49  double clustering_size_min,
50  double clustering_threshold,
51  int32_t clustering_new_policy );
52 
53  virtual ~LearningAlgorithm() {}
54 
55  // Undestand the image and gather informations for the learning
56  template <typename T> int32_t
57  processOneImage( carto::VolumeRef<T> & inputImage,
58  const std::string & inputGraph,
59  const std::string & fileOutMeasure,
60  std::vector<carto::Object > & classDesc,
61  std::vector<std::vector<std::vector<double> > > & classVectors );
62 
63  // Builds the model based on computed vectors.
64  virtual int32_t
65  learnClustering( std::list<double> & inputListOfPrior,
66  std::vector<carto::Object > & classDesc,
67  std::vector<std::vector<std::vector<double> > > & classVectors );
68 
69  // Writes the model as a Dpy file.
70  virtual void saveModel( std::string & fileOutData );
71 
72  private:
73 
74 //=== Methods ================================================================
75  // Compute determinant and inverse I of a square matrix M using
76  // Gaussian elimination.
77  double det(carto::VolumeRef<double > M, carto::VolumeRef<double > & I);
78  // Process the euclidian distance between two vectors of double
79  double distance( std::vector<double> aa, std::vector<double> bb );
80  // Processes centroid of new cluster using histogram.
81  int32_t HistogramCandidateCentroid( const std::vector<std::vector<double> > & mv,
82  const std::vector<std::pair<double, int> > & vcandidates,
83  const int32_t bins = 256 );
84  // Clustering a set of MeasureVectors with respect to entropy
85  // minimization, and returning centroids C contains cluster assignment
86  // for each MeasureVector threshold is used when testing whether a
87  // measure vector fits poorly in clusters : if so, creates a new
88  // cluster process stops when sigmamin or nbclustermax is reached.
89  std::vector<std::vector<double> >
90  Clustering ( const std::vector<std::vector<double> > &mv,
91  double threshold, double sigmamin,
92  double sigmaratio, int nbclustermax,
93  int newclusterpolicy, double clustersizemin,
94  carto::VolumeRef< int > &C );
95 
96 //=== Fields =================================================================
97  int32_t _measuresize;
98 
99 //=== Clustering =============================================================
100  double _sigmamin;
101  double _sigmaratio;
102  int32_t _nbclustermax;
103  double _clustersizemin;
104  double _threshold;
105  int32_t _newclusterpolicy;
106 
107  carto::Object _learningmodel;
108 
109  int _loopcount;
110  };
111 
112 //============================================================================
113 // C L A S S I F I C A T I O N
114 //============================================================================
115 
117  {
118  public:
119 
120  ClassificationAlgorithm( const std::string &fileInData, const int numbfeat );
121 
123 
124  // Classify an image
125  template <typename T>
126  int32_t classify( carto::VolumeRef<T> &image,
127  const std::vector<int32_t> &labels,
128  const std::vector<std::string> &classesorder,
129  const std::string &maskFile = "",
130  const bool keepClasses = true,
131  const bool keepProba = false,
132  const bool keepDensity = false);
133 
134  // Write classification results
135  // Corresponding "keep" options must have been set at true
136  // in classify function.
137  int32_t writeResults( const std::string &outputFile = "",
138  const std::string &outputProbaFile = "",
139  const std::string &outputDensityFile = "");
140 
141  // Classify a point
142  template <typename T>
143  int32_t classify( const Point3dl &t, carto::VolumeRef<T> &image );
144 
145  // Accessors
146  carto::VolumeRef<int16_t> getClassesImage() { return _Cimage.copy(); }
147  carto::VolumeRef<double> getProbaImage() { return _Pimage.copy(); }
148  carto::VolumeRef<double> getDensityImage() { return _Dimage.copy(); }
149 
150  private:
151 //=== Methods ================================================================
152  void InitializeClasses( const std::vector<carto::Object > &c );
153 
154  double Density( const int32_t ic, const std::vector<double> &w );
155 
156  int32_t Classifier( const std::vector<double> &w,
157  const Point3dl &p,
158  const bool storeproba = false,
159  const bool storedensity = false,
160  const bool display = false );
161 
162 //=== Fields =================================================================
163  std::string _pixelmeasure; // Channels used for measure vectors
164  int32_t _measuresize; // Measure vector size.
165 
166 //=== Classification =========================================================
167  int32_t _nbclasses; // Number of classes
168  std::vector<double> _prior; // Prior probabilities (optional)
169 
170 //=== Variables declared for acceleration purpose ============================
171  carto::VolumeRef<double> _v;
172  std::vector<std::vector<carto::VolumeRef<double> > > _modelcov; // covariance matrices for each cluster
173  carto::rc_ptr<std::vector<double> > _mu;
174  carto::VolumeRef<double> _Dimage; // density image
175  carto::VolumeRef<double> _Pimage; // probability image
176  carto::VolumeRef<int16_t> _Cimage; // class label image
177 
178  carto::Object _learningmodel;
179  std::vector<carto::Object > _classdesc; // Model info
180  std::vector<std::vector<carto::Object> > _density;
181  };
182 
183 }// namespace biovision
184 
185 #endif // BRAINRAT_CLASSIFICATION_BIOVISION_H
ClassificationAlgorithm(const std::string &fileInData, const int numbfeat)
carto::VolumeRef< double > getProbaImage()
Definition: biovision.h:147
carto::VolumeRef< int16_t > getClassesImage()
Definition: biovision.h:146
int32_t writeResults(const std::string &outputFile="", const std::string &outputProbaFile="", const std::string &outputDensityFile="")
Definition: biovision_d.h:466
carto::VolumeRef< double > getDensityImage()
Definition: biovision.h:148
int32_t classify(carto::VolumeRef< T > &image, const std::vector< int32_t > &labels, const std::vector< std::string > &classesorder, const std::string &maskFile="", const bool keepClasses=true, const bool keepProba=false, const bool keepDensity=false)
Definition: biovision_d.h:326
virtual void saveModel(std::string &fileOutData)
virtual int32_t learnClustering(std::list< double > &inputListOfPrior, std::vector< carto::Object > &classDesc, std::vector< std::vector< std::vector< double > > > &classVectors)
Definition: biovision_d.h:136
LearningAlgorithm(int sizeT_im, double clustering_sigma_min, double clustering_sigma_ratio, int32_t clustering_number_max, double clustering_size_min, double clustering_threshold, int32_t clustering_new_policy)
int32_t processOneImage(carto::VolumeRef< T > &inputImage, const std::string &inputGraph, const std::string &fileOutMeasure, std::vector< carto::Object > &classDesc, std::vector< std::vector< std::vector< double > > > &classVectors)
Definition: biovision_d.h:49
bool pair_key_comparer(const std::pair< T1, T2 > &p1, const std::pair< T1, T2 > &p2)
Definition: biovision.h:31