Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPartialVolumeAnalysisClusteringCalculator.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 
18 #ifndef _MITK_PartialVolumeAnalysisClusteringCalculator_H
19 #define _MITK_PartialVolumeAnalysisClusteringCalculator_H
20 
22 
23 #include "mitkCommon.h"
24 #include "mitkImage.h"
25 
26 #include <itkObject.h>
27 #include <itkObjectFactory.h>
28 #include <itkImage.h>
29 #include <itkRGBAPixel.h>
30 
31 #include <vnl/vnl_vector.h>
32 
33 namespace mitk
34 {
35 
37  {
38  public:
39 
40  typedef vnl_vector<double> VecType;
42 
43  class HistType
44  {
45  public:
46 
48  {
49  }
50 
51  HistType(const HistType& p)
52  {
53  this->xVals = p.xVals;
54  this->hVals = p.hVals;
55  }
56 
58  {
59  }
60 
61  HistType operator= (const HistType& p)
62  {
63  if (this != &p) // protect against invalid self-assignment
64  {
65  this->xVals = p.xVals;
66  this->hVals = p.hVals;
67  }
68  return *this;
69  }
70 
71  void InitByMitkHistogram(const MitkHistType* histogram)
72  {
73  xVals.set_size(histogram->GetSize()[0]);
74  hVals.set_size(histogram->GetSize()[0]);
75  double sum = histogram->GetTotalFrequency();
76  MitkHistType::ConstIterator endIt = histogram->End();
77  MitkHistType::ConstIterator it = histogram->Begin();
78  bool firstNonEmptyBinFound = false;
79  //++it;
80  int i=0;
81  while (it != endIt)
82  {
83  if(it.GetFrequency() || firstNonEmptyBinFound)
84  {
85  firstNonEmptyBinFound = true;
86  xVals(i) = it.GetMeasurementVector().GetElement(0);
87  hVals(i) = it.GetFrequency()/sum;
88  }
89  ++i;
90  ++it;
91  }
92 
93  }
94 
95  void SetXVals(VecType x)
96  {xVals = x;}
97 
98  void SetHVals(VecType h)
99  {hVals = h;}
100 
101  void SetXVals(std::vector<double> x)
102  {
103  int s = x.size();
104  xVals.set_size(s);
105  for(int i=0; i<s; i++)
106  {
107  xVals(i) = x[i];
108  }
109  }
110 
111  void SetHVals(std::vector<double> h)
112  {
113  int s = h.size();
114  hVals.set_size(s);
115  for(int i=0; i<s; i++)
116  {
117  hVals(i) = h[i];
118  }
119  }
120 
121  std::vector<double>* GetXVals()
122  {
123  int s = xVals.size();
124  std::vector<double>* retval = new std::vector<double>(s);
125  for(int i=0; i<s; i++)
126  {
127  (*retval)[i] = xVals(i);
128  }
129  return retval;
130  }
131 
132  std::vector<double>* GetHVals()
133  {
134  int s = hVals.size();
135  std::vector<double>* retval = new std::vector<double>(s);
136  for(int i=0; i<s; i++)
137  {
138  (*retval)[i] = hVals(i);
139  }
140  return retval;
141  }
142 
143  VecType xVals;
144  VecType hVals;
145  };
146 
148  {
149  public:
150 
151  // ClusterResultType()
152  // {
153  // vals.push_back(VecType());
154  // vals.push_back(VecType());
155  // mixedVals.push_back(VecType());
156  // }
157 
159  {
160  vals.push_back(VecType(sz));
161  vals.push_back(VecType(sz));
162  mixedVals.push_back(VecType(sz));
163  combiVals.set_size(sz);
164  }
165 
167  {
168  }
169 
170 // ClusterResultType operator= (const ClusterResultType *p)
171 // {
172 
173 // MITK_DEBUG << "AOJHIFAHFOF";
174 // if (this != &p) // protect against invalid self-assignment
175 // {
176 // MITK_DEBUG << "HRRRHRHRR";
177 // this->vals.clear();
178 // this->mixedVals.clear();
179 
180 // int s = p.vals.size();
181 // for(int i=0; i<s;i++)
182 // {
183 // VecType v = p.vals[i];
184 // this->vals.push_back(v);
185 // }
186 
187 // s = p.mixedVals.size();
188 // for(int i=0; i<s;i++)
189 // {
190 // VecType v = p.mixedVals[i];
191 // this->mixedVals.push_back(v);
192 // }
193 
194 // this->combiVals = p.combiVals;
195 // }
196 // return *this;
197 // }
198 
200  {
201  if (this != p) // protect against invalid self-assignment
202  {
203  this->vals.clear();
204  this->mixedVals.clear();
205 
206  int s = p->vals.size();
207  for(int i=0; i<s;i++)
208  {
209  VecType v = p->vals[i];
210  this->vals.push_back(v);
211  }
212 
213  s = p->mixedVals.size();
214  for(int i=0; i<s;i++)
215  {
216  VecType v = p->mixedVals[i];
217  this->mixedVals.push_back(v);
218  }
219 
220  this->combiVals = p->combiVals;
221  }
222  }
223 
224  std::vector<double> GetFiberVals()
225  {
226  if(vals.size()==2 && vals[1].data_block())
227  {
228  int s = vals[1].size();
229  std::vector<double> retval(s);
230  for(int i=0; i<s; i++)
231  {
232  retval[i] = vals[1](i);
233  }
234  return retval;
235  }
236  else
237  {
238  MITK_ERROR << "GetFiberVals() struct not initialized!!";
239  return std::vector<double>(0);
240  }
241  }
242 
243  std::vector<double> GetNonFiberVals()
244  {
245  if(vals.size()==2 && vals[0].data_block())
246  {
247  int s = vals[0].size();
248  std::vector<double> retval(s);
249  for(int i=0; i<s; i++)
250  {
251  retval[i] = vals[0](i);
252  }
253  return retval;
254  }
255  else
256  {
257  MITK_ERROR << "GetNonFiberVals() struct not initialized!!";
258  return std::vector<double>(0);
259  }
260  }
261 
262  std::vector<double> GetMixedVals()
263  {
264  if(mixedVals.size()==1 && mixedVals[0].data_block())
265  {
266  int s = mixedVals[0].size();
267  std::vector<double> retval(s);
268  for(int i=0; i<s; i++)
269  {
270  retval[i] = mixedVals[0](i);
271  }
272  return retval;
273  }
274  else
275  {
276  MITK_ERROR << "GetMixedVals() struct not initialized!!";
277  return std::vector<double>(0);
278  }
279  }
280 
281  std::vector<double> GetCombiVals()
282  {
283  if(combiVals.data_block())
284  {
285  int s = combiVals.size();
286  std::vector<double> retval(s);
287  for(int i=0; i<s; i++)
288  {
289  retval[i] = combiVals(i);
290  }
291  return retval;
292  }
293  else
294  {
295  MITK_ERROR << "GetCombiVals() struct not initialized!!";
296  return std::vector<double>(0);
297  }
298  }
299 
300 
301 // void Print(VecType vec, int nr=10)
302 // {
303 // int sz = vec.size();
304 // int incr = (int)((1.0*sz)/(1.0*nr));
305 // for(int i=0; i<sz; i = i+incr)
306 // {
307 // std::cout << vec(i) << " ";
308 // }
309 // std::cout << std::endl;
310 // }
311 
312 // void Print(int nr=10)
313 // {
314 // MITK_DEBUG << "CURVES" << std::endl;
315 // MITK_DEBUG << "Fiber Vals: ";
316 // Print(vals[0],nr);
317 // MITK_DEBUG << "Non-Fiber Vals: ";
318 // Print(vals[1],nr);
319 // MITK_DEBUG << "Mixed Vals: ";
320 // Print(mixedVals[0],nr);
321 // MITK_DEBUG << "Combined Vals: ";
322 // Print(combiVals,nr);
323 // }
324 
325  std::vector<VecType> vals;
326  std::vector<VecType> mixedVals;
327  VecType combiVals;
328 
329  };
330 
332  {
333  public:
335  {
336  }
337 
339  {
340  }
341 
342  void Initialize(const ParamsType *p)
343  {
344  if (this != p) // protect against invalid self-assignment
345  {
346  means[0] = p->means[0];
347  means[1] = p->means[1];
348  sigmas[0] = p->sigmas[0];
349  sigmas[1] = p->sigmas[1];
350  ps[0] = p->ps[0];
351  ps[1] = p->ps[1];
352  }
353  }
354 
355 // void Print()
356 // {
357 // MITK_DEBUG << "PARAMS" << std::endl;
358 // MITK_DEBUG << "Class 1: " << means[0] << " +- " << sqrt(sigmas[0]) << " (p=" << ps[0] << ")" << std::endl;
359 // MITK_DEBUG << "Class 2: " << means[1] << " +- " << sqrt(sigmas[1]) << " (p=" << ps[1] << ")" << std::endl;
360 // MITK_DEBUG << "Partial V: p=" << 1.0-ps[0]-ps[1] << std::endl;
361 // }
362 
363  double means[2];
364  double sigmas[2];
365  double ps[2];
366  };
367 
369  {
370  MitkHistType *interestingHist;
371  MitkHistType *totalHist;
373  };
374 
376  {
380 
382  {
383  r = nullptr;
384  g = nullptr;
385  b = nullptr;
386  }
387  };
388 
390  {
393 
397 
399  rgbChannels(nullptr), params(nullptr), result(nullptr), hist(nullptr)
400  {
401  }
402 
404  {
405  rgb = nullptr;
406  delete rgbChannels;
407  }
408  };
409 
411  {
414 
418 
420  clusteredImage(nullptr), displayImage(nullptr),
421  params(nullptr), result(nullptr), hist(nullptr)
422  {
423  }
424 
426  {
427  clusteredImage = nullptr;
428  displayImage = nullptr;
429  }
430  };
431 
433  itkFactorylessNewMacro(Self)
434  itkCloneMacro(Self)
435 
436  ParamsType *InitialGuess(HistType h) const;
437 
438  ParamsType *Cluster(const HistType h, ParamsType* initialGuess) const;
439 
440  ClusterResultType CalculateCurves(ParamsType params, VecType xVals) const;
441 
442  void Normalize(ParamsType params, ClusterResultType* curves) const;
443 
444  HelperStructPerformClusteringRetval* PerformClustering(mitk::Image::ConstPointer image, const MitkHistType *histogram, int classIdent, HelperStructPerformClusteringRetval* precResult = nullptr) const;
445 
446  HelperStructPerformRGBClusteringRetval* PerformRGBClustering(mitk::Image::ConstPointer image, const MitkHistType *histogram) const;
447 
448  HelperStructPerformClusteringRetval* PerformQuantiles(mitk::Image::ConstPointer image, const MitkHistType *histogram, double p1, double p2) const;
449 
450  HelperStructPerformRGBClusteringRetval* PerformRGBQuantiles(mitk::Image::ConstPointer image, const MitkHistType *histogram, double p1, double p2 ) const;
451 
452  double* PerformQuantification(mitk::Image::ConstPointer image, mitk::Image::Pointer clusteredImage, mitk::Image::Pointer mask = nullptr) const;
453 
454  mitk::Image::Pointer CaculateAngularErrorImage(
455  mitk::Image::Pointer comp1, mitk::Image::Pointer comp2, mitk::Image::Pointer probImg) const;
456 
457  template < unsigned int VImageDimension >
458  void InternalGenerateRGB( HelperStructRGBChannels *rgb, mitk::Image::Pointer retval ) const;
459 
460  template < typename TPixel, unsigned int VImageDimension >
461  void InternalGenerateProbabilityImage(
462  const itk::Image< TPixel, VImageDimension > *image,
463  const HelperStructClusteringResults clusterResults,
464  mitk::Image::Pointer outImage1, mitk::Image::Pointer outImage2 ) const;
465 
466  template < typename TPixel, unsigned int VImageDimension >
467  void InternalQuantify(
468  const itk::Image< TPixel, VImageDimension > *image,
469  mitk::Image::Pointer clusteredImage, double* retval, mitk::Image::Pointer mask ) const;
470 
471  template < typename TPixel, unsigned int VImageDimension >
472  void InternalGenerateQuantileImage(
473  const itk::Image< TPixel, VImageDimension > *image,
474  double* q,
475  mitk::Image::Pointer outImage1, mitk::Image::Pointer outImage2 ) const;
476 
477  ParamsType* Cluster(const HistType h) const
478  {return Cluster(h, InitialGuess(h));}
479 
480  void SetMaxIt(unsigned int it)
481  { m_MaxIt = it; }
482 
483  unsigned int GetMaxIt()
484  { return m_MaxIt; }
485 
486  void SetStepsNumIntegration(unsigned int n)
487  { m_StepsNumIntegration = n; }
488 
489  unsigned int GetStepsNumIntegration()
490  { return m_StepsNumIntegration; }
491 
492  protected:
493 
495 
497 
498  unsigned int m_MaxIt;
499  unsigned int m_StepsNumIntegration;
500 
501  };
502 
503 }
504 
505 #endif // #define _MITK_PartialVolumeAnalysisClusteringCalculator_H
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
#define MITKDIFFUSIONCORE_EXPORT
itk::Statistics::Histogram< double > HistogramType
Definition: mitkImage.h:94
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:53
Image class for storing images.
Definition: mitkImage.h:76
void Normalize(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer im2, mitk::Image::Pointer mask1, std::string output)
Definition: CLBrainMask.cpp:40