Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkHistogramGenerator.cpp
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 #if (_MSC_VER == 1200)
18 #include <itkFixedCenterOfRotationAffineTransform.h>
19 #endif
20 
21 #include "mitkHistogramGenerator.h"
22 #include "mitkImageAccessByItk.h"
23 #include "mitkImageTimeSelector.h"
24 
25 //
26 // The new ITK Statistics framework has
27 // a class with the same functionality as
28 // MITKScalarImageToHistogramGenerator.h, but
29 // no longer has the classis the MITK class depends on.
30 #if !defined(ITK_USE_REVIEW_STATISTICS)
32 #else
33 #include "itkScalarImageToHistogramGenerator.h"
34 #endif
35 
36 mitk::HistogramGenerator::HistogramGenerator() : m_Image(nullptr), m_Size(256), m_Histogram(nullptr)
37 {
38 }
39 
41 {
42 }
43 
44 template <typename TPixel, unsigned int VImageDimension>
45 void InternalCompute(itk::Image<TPixel, VImageDimension> *itkImage,
46  const mitk::HistogramGenerator *mitkHistoGenerator,
48 {
49 #if !defined(ITK_USE_REVIEW_STATISTICS)
51  HistogramGeneratorType;
52 #else
53  typedef itk::Statistics::ScalarImageToHistogramGenerator<itk::Image<TPixel, VImageDimension>> HistogramGeneratorType;
54 
55 #endif
56  typename HistogramGeneratorType::Pointer histogramGenerator = HistogramGeneratorType::New();
57 
58  histogramGenerator->SetInput(itkImage);
59 
60  histogramGenerator->SetNumberOfBins(mitkHistoGenerator->GetSize());
61  // histogramGenerator->SetMarginalScale( 10.0 );
62  histogramGenerator->Compute();
63 
64  histogram = histogramGenerator->GetOutput();
65 }
66 
68 {
69  if ((m_Histogram.IsNull()) || (m_Histogram->GetMTime() < m_Image->GetMTime()))
70  {
71  const_cast<mitk::Image *>(m_Image.GetPointer())->SetRequestedRegionToLargestPossibleRegion(); //@todo without this,
72  // Image::GetScalarMin
73  // does not work for
74  // dim==3 (including
75  // sliceselector!)
76  const_cast<mitk::Image *>(m_Image.GetPointer())->Update();
78  timeSelector->SetInput(m_Image);
79  timeSelector->SetTimeNr(0);
80  timeSelector->UpdateLargestPossibleRegion();
81  AccessByItk_n(timeSelector->GetOutput(), InternalCompute, (this, m_Histogram));
82  }
83 
84  // debug code
85  /*
86  MITK_INFO << "Histogram modfied 1" << m_Histogram->GetMTime() << std::endl;
87  m_Histogram->Modified();
88  MITK_INFO << "Histogram modfied 2" << m_Histogram->GetMTime() << std::endl;
89  MITK_INFO << "Image modfied" << m_Image->GetMTime() << std::endl;
90  const unsigned int histogramSize = m_Histogram->Size();
91 
92  MITK_INFO << "Histogram size " << histogramSize << std::endl;
93 
94  HistogramType::ConstIterator itr = GetHistogram()->Begin();
95  HistogramType::ConstIterator end = GetHistogram()->End();
96 
97  int bin = 0;
98  while( itr != end )
99  {
100  MITK_INFO << "bin = " << GetHistogram()->GetBinMin(0,bin) << "--" << GetHistogram()->GetBinMax(0,bin) << " frequency
101  = ";
102  MITK_INFO << itr.GetFrequency() << std::endl;
103  ++itr;
104  ++bin;
105  }
106  */
107 }
108 
110 {
111  return CalculateMaximumFrequency(this->m_Histogram);
112 };
113 
115 {
116  HistogramType::ConstIterator itr = histogram->Begin();
117  HistogramType::ConstIterator end = histogram->End();
118 
119  float maxFreq = 0;
120  while (itr != end)
121  {
122  maxFreq = vnl_math_max(maxFreq,
123  // get rid of ambiguity with type signature
124  // for vnl_math_max
125  static_cast<float>(itr.GetFrequency()));
126  ++itr;
127  }
128  return maxFreq;
129 };
virtual int GetSize() const
itk::SmartPointer< Self > Pointer
static float CalculateMaximumFrequency(const HistogramType *histogram)
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:35
#define AccessByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a MITK image by an ITK image with one or more parameters.
itk::SmartPointer< const Self > ConstPointer
void InternalCompute(itk::Image< TPixel, VImageDimension > *itkImage, const mitk::HistogramGenerator *mitkHistoGenerator, mitk::HistogramGenerator::HistogramType::ConstPointer &histogram)
Image class for storing images.
Definition: mitkImage.h:76
itk::Statistics::Histogram< double > HistogramType
Provides an easy way to calculate an itk::Histogram for a mitk::Image.
static Pointer New()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.