Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
old/mitkImageStatisticsCalculator.h
Go to the documentation of this file.
1 /*===================================================================
2 The Medical Imaging Interaction Toolkit (MITK)
3 
4 Copyright (c) German Cancer Research Center,
5 Division of Medical and Biological Informatics.
6 All rights reserved.
7 
8 This software is distributed WITHOUT ANY WARRANTY; without
9 even the implied warranty of MERCHANTABILITY or FITNESS FOR
10 A PARTICULAR PURPOSE.
11 
12 See LICENSE.txt or http://www.mitk.org for details.
13 
14 ===================================================================*/
15 
16 #ifndef _MITK_IMAGESTATISTICSCALCULATOR_H
17 #define _MITK_IMAGESTATISTICSCALCULATOR_H
18 
19 #include <itkObject.h>
21 #include <itkImage.h>
22 #include <itkTimeStamp.h>
23 #include "mitkImage.h"
24 #include "mitkPlanarFigure.h"
25 
26 #ifndef __itkHistogram_h
27 #include <itkHistogram.h>
28 #endif
29 
30 #include <itkImageRegionIteratorWithIndex.h>
31 
32 
33 #include <vtkSmartPointer.h>
34 
35 // just a helper to unclutter our code
36 // to be replaced with references to m_Member (when deprecated public members in Statistics are removed)
37 #define mitkSetGetConstMacro(name, type) \
38  virtual type Get##name() const \
39  { \
40  return this->name; \
41  } \
42  \
43  virtual void Set##name(const type _arg) \
44  { \
45  if ( this->name != _arg ) \
46  { \
47  this->name = _arg; \
48  } \
49  }
50 
51 namespace mitk
52 {
159  class MITKIMAGESTATISTICS_EXPORT ImageStatisticsCalculator : public itk::Object
160  {
161  public:
162 
164  enum
165  {
166  MASKING_MODE_NONE = 0,
167  MASKING_MODE_IMAGE = 1,
168  MASKING_MODE_PLANARFIGURE = 2
169  };
170 
171  typedef itk::Statistics::Histogram<double> HistogramType;
172  typedef HistogramType::ConstIterator HistogramConstIteratorType;
173 
176  {
177  public:
178 
179  Statistics(bool withHotspotStatistics = true);
180  Statistics(const Statistics& other);
181 
182  virtual ~Statistics();
183 
184  Statistics& operator=(Statistics const& stats);
185 
186  const Statistics& GetHotspotStatistics() const; // real statistics
187  Statistics& GetHotspotStatistics(); // real statistics
188  bool HasHotspotStatistics() const;
189  void SetHasHotspotStatistics(bool hasHotspotStatistics); // set a flag. if set, return empty hotspotstatistics object
190 
191  void Reset(unsigned int dimension = 2);
192 
194  mitkSetGetConstMacro(N, unsigned int)
197  mitkSetGetConstMacro(Mean, double)
198  mitkSetGetConstMacro(Median, double)
199 
200  double GetVariance() const;
210  void SetVariance( const double );
211 
212  double GetSigma() const;
223  void SetSigma( const double );
224 
225  mitkSetGetConstMacro(RMS, double)
226  mitkSetGetConstMacro(MinIndex, vnl_vector<int>)
227  mitkSetGetConstMacro(MaxIndex, vnl_vector<int>)
228  mitkSetGetConstMacro(HotspotIndex, vnl_vector<int>)
229 
230  mitkSetGetConstMacro(Skewness, double)
231  mitkSetGetConstMacro(Kurtosis, double)
232  mitkSetGetConstMacro(Uniformity, double)
233  mitkSetGetConstMacro(Entropy, double)
234  mitkSetGetConstMacro(UPP, double);
235  mitkSetGetConstMacro(MPP, double);
236 
237  private:
238  unsigned int Label;
239  unsigned int N;
240  double Min;
241  double Max;
242  double Mean;
243  double Median;
244  double Variance;
245  double Sigma;
246  double RMS;
247  vnl_vector<int> MinIndex;
248  vnl_vector<int> MaxIndex;
249 
250  bool m_SkewnessAndKurtosisSuccesfulCalculated;
251  bool m_ImageStatisticsSuccesfulCalculated;
252 
253 
254  Statistics* m_HotspotStatistics;
255 
256  double Skewness;
257  double Kurtosis;
258  double Uniformity;
259  double Entropy;
260  double UPP;
261  double MPP;
262 
263  bool m_HasHotspotStatistics;
264  vnl_vector<int> HotspotIndex; //< index of hotspotsphere origin
265  };
266 
267  typedef std::vector< HistogramType::ConstPointer > HistogramContainer;
268  typedef std::vector< Statistics > StatisticsContainer;
269 
270  mitkClassMacroItkParent( ImageStatisticsCalculator, itk::Object );
271  itkFactorylessNewMacro(Self)
272  itkCloneMacro(Self)
273 
275  void SetUseDefaultBinSize(bool useDefault);
276 
278  void SetImage( const mitk::Image *image );
279 
281  void SetImageMask( const mitk::Image *imageMask );
282 
284  void SetPlanarFigure( mitk::PlanarFigure *planarFigure );
285 
286 
288  void SetMaskingMode( unsigned int mode );
289 
291  itkGetMacro( MaskingMode, unsigned int );
292 
294  void SetMaskingModeToNone();
295 
297  void SetMaskingModeToImage();
298 
300  void SetMaskingModeToPlanarFigure();
301 
303  void SetIgnorePixelValue(double value);
304 
306  double GetIgnorePixelValue();
307 
309  void SetDoIgnorePixelValue(bool doit);
310 
312  bool GetDoIgnorePixelValue();
313 
315  void SetHistogramBinSize( double size);
316 
318  double GetHistogramBinSize();
319 
321  void SetHotspotRadiusInMM (double hotspotRadiusInMM);
322 
324  double GetHotspotRadiusInMM();
325 
327  void SetCalculateHotspot(bool calculateHotspot);
328 
330  bool IsHotspotCalculated();
331 
337  void SetHotspotMustBeCompletlyInsideImage(bool hotspotIsCompletlyInsideImage, bool warn = true);
338 
340  bool GetHotspotMustBeCompletlyInsideImage() const;
341 
347  virtual bool ComputeStatistics( unsigned int timeStep = 0 );
348 
349 
354  const HistogramType *GetHistogram( unsigned int timeStep = 0, unsigned int label = 0 ) const;
355 
357  const HistogramContainer &GetHistogramVector( unsigned int timeStep = 0 ) const;
358 
359  typedef std::map<int, double> BinFrequencyType;
360 
361  /* Returning a map including bin and Frequency*/
362  BinFrequencyType GetBinsAndFreuqencyForHistograms( unsigned int timeStep = 0, unsigned int label = 0) const;
363 
369  const Statistics &GetStatistics( unsigned int timeStep = 0, unsigned int label = 0 ) const;
370 
372  const StatisticsContainer &GetStatisticsVector( unsigned int timeStep = 0 ) const;
373 
374 
375 
376 
377  protected:
378  typedef std::vector< HistogramContainer > HistogramVector;
379  typedef std::vector< StatisticsContainer > StatisticsVector;
380 
381  typedef std::vector< itk::TimeStamp > TimeStampVectorType;
382  typedef std::vector< bool > BoolVectorType;
383 
384  typedef itk::Image< unsigned short, 3 > MaskImage3DType;
385  typedef itk::Image< unsigned short, 2 > MaskImage2DType;
386 
387  ImageStatisticsCalculator();
388 
389  virtual ~ImageStatisticsCalculator();
390 
399  void ExtractImageAndMask( unsigned int timeStep = 0 );
400 
401  /*calculate the min and max value, this is done because we need the min and max value before execution the statistics filter to have the wright range for the histogramm*/
402  template < typename TPixel, unsigned int VImageDimension >
403  void GetMinAndMaxValue(double &minimum, double &maximum, int &counter, double &sigma, const itk::Image< TPixel, VImageDimension > *InputImage,
404  itk::Image< unsigned short, VImageDimension > *MaskImageType);
405 
409  bool GetPrincipalAxis( const BaseGeometry *geometry, Vector3D vector,
410  unsigned int &axis );
411 
412  template < typename TPixel, unsigned int VImageDimension >
413  void InternalCalculateStatisticsUnmasked(
414  const itk::Image< TPixel, VImageDimension > *image,
415  StatisticsContainer* statisticsContainer,
416  HistogramContainer *histogramContainer );
417 
418  template < typename TPixel, unsigned int VImageDimension >
419  void InternalCalculateStatisticsMasked(
420  const itk::Image< TPixel, VImageDimension > *image,
421  itk::Image< unsigned short, VImageDimension > *maskImage,
422  StatisticsContainer* statisticsContainer,
423  HistogramContainer* histogramContainer );
424 
425  template < typename TPixel, unsigned int VImageDimension >
426  void InternalCalculateMaskFromPlanarFigure(
427  const itk::Image< TPixel, VImageDimension > *image, unsigned int axis );
428 
429  template < typename TPixel, unsigned int VImageDimension >
430  void InternalMaskIgnoredPixels(
431  const itk::Image< TPixel, VImageDimension > *image,
432  itk::Image< unsigned short, VImageDimension > *maskImage );
433 
435  {
436  public:
437  bool Defined;
438  double Max;
439  double Min;
440  vnl_vector<int> MaxIndex;
441  vnl_vector<int> MinIndex;
442 
444  :Defined(false)
445  ,Max(itk::NumericTraits<double>::min())
446  ,Min(itk::NumericTraits<double>::max())
447  {
448  }
449  };
450 
451 
455  template <typename TPixel, unsigned int VImageDimension >
456  ImageExtrema CalculateExtremaWorld(
457  const itk::Image<TPixel, VImageDimension> *inputImage,
458  itk::Image<unsigned short, VImageDimension> *maskImage,
459  double neccessaryDistanceToImageBorderInMM,
460  unsigned int label);
461 
462 
466  template < typename TPixel, unsigned int VImageDimension>
467  Statistics CalculateHotspotStatistics(
468  const itk::Image<TPixel, VImageDimension> *inputImage,
469  itk::Image<unsigned short, VImageDimension> *maskImage,
470  double radiusInMM,
471  bool& isHotspotDefined,
472  unsigned int label);
473 
475  template <typename ITK_Exporter, typename VTK_Importer>
476  void ConnectPipelines(ITK_Exporter exporter, vtkSmartPointer<VTK_Importer> importer)
477  {
478  importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
479 
480  importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
481  importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
482  importer->SetSpacingCallback(exporter->GetSpacingCallback());
483  importer->SetOriginCallback(exporter->GetOriginCallback());
484  importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
485 
486  importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
487 
488  importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
489  importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
490  importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
491  importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
492  importer->SetCallbackUserData(exporter->GetCallbackUserData());
493  }
494 
496  template <typename VTK_Exporter, typename ITK_Importer>
497  void ConnectPipelines(vtkSmartPointer<VTK_Exporter> exporter, ITK_Importer importer)
498  {
499  importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
500 
501  importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
502  importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
503  importer->SetSpacingCallback(exporter->GetSpacingCallback());
504  importer->SetOriginCallback(exporter->GetOriginCallback());
505  importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
506 
507  importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
508 
509  importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
510  importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
511  importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
512  importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
513  importer->SetCallbackUserData(exporter->GetCallbackUserData());
514  }
515 
516 
517  void UnmaskedStatisticsProgressUpdate();
518 
519  void MaskedStatisticsProgressUpdate();
520 
522  template <unsigned int VImageDimension>
523  itk::Size<VImageDimension>
524  CalculateConvolutionKernelSize(double spacing[VImageDimension], double radiusInMM);
525 
527  template <unsigned int VImageDimension>
529  GenerateHotspotSearchConvolutionKernel(double spacing[VImageDimension], double radiusInMM);
530 
532  template <typename TPixel, unsigned int VImageDimension>
534  GenerateConvolutionImage( const itk::Image<TPixel, VImageDimension>* inputImage );
535 
537  template < typename TPixel, unsigned int VImageDimension>
538  void
539  FillHotspotMaskPixels( itk::Image<TPixel, VImageDimension>* maskImage,
540  itk::Point<double, VImageDimension> sphereCenter,
541  double sphereRadiusInMM);
542 
545 
547 
549 
553 
556 
557 
562 
565 
566  unsigned int m_MaskingMode;
568 
573 
577 
581 
585 
586  unsigned int m_PlanarFigureAxis; // Normal axis for PlanarFigure
587  unsigned int m_PlanarFigureSlice; // Slice which contains PlanarFigure
588  int m_PlanarFigureCoordinate0; // First plane-axis for PlanarFigure
589  int m_PlanarFigureCoordinate1; // Second plane-axis for PlanarFigure
590 
598 
599 
600  private:
601 
602  unsigned int calcNumberOfBins(mitk::ScalarType min, mitk::ScalarType max);
603 
604 
605  };
606 
607 } // namespace
608 
609 #endif
itk::Image< unsigned short, 3 > MaskImage3DType
itk::SmartPointer< Self > Pointer
#define Max(x, y)
Definition: AnnotationP.h:41
double ScalarType
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
A data structure describing a label.
Definition: mitkLabel.h:35
bool m_UseDefaultBinSize
Bin size for histogram resoluion.
Class for common statistics, includig hotspot properties.
#define MITKIMAGESTATISTICS_EXPORT
itk::Statistics::Histogram< double > HistogramType
std::vector< StatisticsContainer > StatisticsVector
Class for calculating statistics and histogram for an (optionally masked) image.
std::vector< HistogramType::ConstPointer > HistogramContainer
void ConnectPipelines(vtkSmartPointer< VTK_Exporter > exporter, ITK_Importer importer)
Container class for storing the computed image statistics.
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:53
Image class for storing images.
Definition: mitkImage.h:76
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:36
static T max(T x, T y)
Definition: svm.cpp:70
void ConnectPipelines(ITK_Exporter exporter, vtkSmartPointer< VTK_Importer > importer)
static T min(T x, T y)
Definition: svm.cpp:67
itk::Image< unsigned short, 2 > MaskImage2DType
Base-class for geometric planar (2D) figures, such as lines, circles, rectangles, polygons...
#define mitkSetGetConstMacro(name, type)
std::vector< itk::TimeStamp > TimeStampVectorType
std::vector< HistogramContainer > HistogramVector
itk::SmartPointer< Self > Pointer
Definition: mitkBaseData.h:42
#define Min(x, y)
Definition: AnnotationP.h:38
HistogramType::ConstIterator HistogramConstIteratorType
BaseGeometry Describes the geometry of a data object.