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