EMBase.h
1 /*
2  * Developing brain Region Annotation With Expectation-Maximization (Draw-EM)
3  *
4  * Copyright 2013-2016 Imperial College London
5  * Copyright 2013-2016 Christian Ledig
6  * Copyright 2013-2016 Antonios Makropoulos
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef _MIRTKEMCLASSIFICATIONNEO_H
22 #define _MIRTKEMCLASSIFICATIONNEO_H
23 
24 #include "mirtk/Image.h"
25 #include "mirtk/Object.h"
26 #include "mirtk/HashProbabilisticAtlas.h"
27 #include "mirtk/Gaussian.h"
28 #include "mirtk/Histogram1D.h"
29 #include "mirtk/MeanShift.h"
30 
31 /*
32 
33 Expectation Maximisation Algorithm
34 
35  */
36 namespace mirtk {
37 
38 class EMBase : public Object
39 {
40 
41 protected:
42  typedef GenericImage<int> IntegerImage;
43 
44  /// Input image
45  RealImage _input;
46 
47  /// Brain tissue
48  RealImage _brain;
49 
50  /// weights image
51  RealImage _weights;
52 
53  /// image estimate
54  RealImage _estimate;
55 
56  /// Posterior probability maps - segmentation
57  HashProbabilisticAtlas _output;
58 
59  /// Partial volume soft segmentation
60  HashProbabilisticAtlas _pv_output;
61 
62  /// Probability maps (atlas)
63  HashProbabilisticAtlas _atlas;
64 
65  /// image segmentation
66  IntegerImage _segmentation;
67 
68  /// posterior penalty
69  RealImage _postpenalty;
70  bool _postpen;
71 
72  /// Number of tissues
73  int _number_of_tissues;
74 
75  /// Number of voxels
76  int _number_of_voxels;
77 
78  /// Gaussian distribution parameters for each tissue type
79  Gaussian *_G;
80  double *_mi;
81  double *_sigma;
82  /// mixing coefficients for GMM
83  double *_c;
84 
85  /// Likelihood
86  double _f;
87 
88  /// Padding value
89  RealPixel _padding;
90 
91  /// whether we have additional background tissue
92  bool _has_background;
93 
94  /// superlabels
95  int *_super;
96  bool _superlabels;
97 
98  /// whether a mask is set
99  bool _mask_set;
100 
101  /// whether initial posteriors is set
102  bool _posteriors_set;
103 
104 public:
105  /// Input mask
106  ByteImage _mask;
107 
108  /// Empty constructor
109  EMBase();
110 
111  /// Constructor when adding background
112  template <class ImageType>
113  EMBase(int noTissues, ImageType **atlas, ImageType *background);
114 
115  /// Constructor without adding background
116  template <class ImageType>
117  EMBase(int noTissues, ImageType **atlas);
118 
119  /// Constructor without adding background + initialise posteriors
120  template <class ImageType>
121  EMBase(int noTissues, ImageType **atlas, ImageType **initposteriors ) ;
122 
123  /// Destructor
124  virtual ~EMBase();
125 
126  // add a probability map
127  template <class ImageType>
128  void addProbabilityMap(ImageType image);
129 
130  // add background
131  void addBackground();
132  template <class ImageType>
133  void addBackground(ImageType image);
134 
135  // normalise the atlas
136  void NormalizeAtlas();
137 
138  /// Initialize filter
139  virtual void Initialise();
140 
141  /// Initialize parameters
142  void InitialiseParameters();
143 
144  /// Initialize filter
145  virtual void InitialiseGMM();
146 
147  /// Initialize Gaussian Mixture Parameters and calculate initial posteriors
148  void InitialiseGMMParameters(int n, double *m, double *s, double *c);
149 
150  /// Returns the name of the class
151  virtual const char* NameOfClass() const;
152 
153  /// Estimates posterior probabilities
154  virtual void EStep();
155 
156  /// Estimates parameters
157  virtual void MStep();
158 
159  /// Estimates posterior probabilities
160  virtual void EStepGMM(bool uniform_prior = false);
161 
162  /// Estimates parameters in GMM
163  virtual void MStepGMM(bool uniform_prior = false);
164 
165  /// Estimates parameters in GMM with equal variance
166  virtual void MStepVarGMM(bool uniform_prior = false);
167 
168  /// Computes log likelihood for current parameters
169  virtual double LogLikelihood();
170 
171  /// Computes log likelihood for GMM for current parameters
172  virtual double LogLikelihoodGMM();
173 
174  /// Get ProbMap
175  virtual void GetProbMap(int i,RealImage& image);
176 
177  /// Print parameters
178  virtual void Print();
179 
180  /// Print parameters
181  virtual void PrintGMM();
182 
183  /// Calculate weights
184  virtual void WStep();
185 
186  /// Set image
187  virtual void SetInput(const RealImage &);
188 
189  /// set mask
190  void SetMask(ByteImage &mask);
191 
192  /// Set padding value
193  virtual void SetPadding(RealPixel);
194 
195  /// Set posterior penalty
196  void setPostPenalty(RealImage &postpenalty);
197  /// Set superlabels
198  void setSuperlabels(int *superlabels);
199 
200  /// Execute one iteration and return log likelihood
201  virtual double Iterate(int iteration);
202 
203  /// Execute one iteration and return log likelihood
204  virtual double IterateGMM(int iteration, bool equal_var = false, bool uniform_prior = false);
205 
206  /// Get GMM memberships
207  virtual void GetProportions(double *);
208 
209  /// Construct segmentation based on current posterior probabilities
210  virtual void ConstructSegmentation(IntegerImage &);
211  /// Construct segmentation based on current posterior probabilities
212  virtual void ConstructSegmentation();
213 
214  /// Write probability map into a file
215  void WriteProbMap(int i, const char *filename);
216  /// Write Gaussian parameters into a file
217  void WriteGaussianParameters(const char *file_name, int flag = 0);
218  /// Write image estimate
219  void WriteEstimate(const char *filename);
220  /// Write weights
221  void WriteWeights(const char *filename);
222  /// Write input
223  void WriteInput(const char *filename);
224  /// Write segmentation
225  void WriteSegmentation(const char *filename);
226 
227 
228  /// Returns log likelihood for given intensity value
229  double PointLogLikelihoodGMM(double x);
230  /// Initialize gaussians with current GMM parameters
231  void GInit();
232  /// create mask taking into account priors and padding
233  void CreateMask();
234  /// return means
235  virtual void GetMean(double *);
236  /// return variances
237  virtual void GetVariance(double *);
238 
239  /// initialise GMM parameters
240  void InitialiseGMMParameters(int n);
241  /// set uniform prior
242  void UniformPrior();
243 };
244 
245 
246 inline void EMBase::addBackground(){
247  _atlas.AddBackground();
248  _has_background = true;
249  _number_of_tissues = _atlas.GetNumberOfMaps();
250 }
251 
252 template <class ImageType>
253 inline void EMBase::addBackground(ImageType background){
254  _atlas.AddBackground(background);
255  _has_background = true;
256  _number_of_tissues = _atlas.GetNumberOfMaps();
257 }
258 
259 
260 template <class ImageType>
261 inline void EMBase::addProbabilityMap(ImageType image){
262  _atlas.AddImage(image);
263  _number_of_tissues = _atlas.GetNumberOfMaps();
264 }
265 
266 inline void EMBase::NormalizeAtlas(){
267  _atlas.NormalizeAtlas();
268 }
269 
270 inline void EMBase::SetPadding(RealPixel padding)
271 {
272  _padding = padding;
273 }
274 
275 inline void EMBase::WriteEstimate(const char *filename)
276 {
277  _estimate.Write(filename);
278 }
279 
280 inline void EMBase::WriteInput(const char *filename)
281 {
282  _input.Write(filename);
283 }
284 
285 inline void EMBase::WriteSegmentation(const char *filename)
286 {
287  _segmentation.Write(filename);
288 }
289 
290 inline const char* EMBase::NameOfClass() const { return "EMBase"; }
291 
292 }
293 
294 #endif
Definition: IOConfig.h:41
std::ostream & Print(std::ostream &os, T value)
Print single argument to output stream.
Definition: String.h:259