Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkImageStatisticsCalculationJob.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 
14 
16 #include <mitkImageMaskGenerator.h>
19 
21  : QThread()
22  , m_StatisticsImage(nullptr)
23  , m_BinaryMask(nullptr)
24  , m_PlanarFigureMask(nullptr)
25  , m_IgnoreZeros(false)
26  , m_HistogramNBins(100)
27  , m_CalculationSuccessful(false)
28 {
29 }
30 
32 {
33 }
34 
36  const mitk::Image *binaryImage,
37  const mitk::PlanarFigure *planarFig)
38 {
39  this->m_StatisticsImage = image;
40  this->m_BinaryMask = binaryImage;
41  this->m_PlanarFigureMask = planarFig;
42 }
43 
45 {
46  return this->m_StatisticsContainer.GetPointer();
47 }
48 
50 {
51  return this->m_StatisticsImage.GetPointer();
52 }
53 
55 {
56  return this->m_BinaryMask.GetPointer();
57 }
58 
60 {
61  return this->m_PlanarFigureMask.GetPointer();
62 }
63 
65 {
66  this->m_IgnoreZeros = _arg;
67 }
68 
70 {
71  return this->m_IgnoreZeros;
72 }
73 
75 {
76  this->m_HistogramNBins = nbins;
77 }
78 
80 {
81  return this->m_HistogramNBins;
82 }
83 
85 {
86  return m_message;
87 }
88 
91 {
92  if (t >= this->m_HistogramVector.size())
93  return nullptr;
94 
95  return this->m_HistogramVector.at(t).GetPointer();
96 }
97 
99 {
100  return m_CalculationSuccessful;
101 }
102 
104 {
105  bool statisticCalculationSuccessful = true;
107 
108  if(this->m_StatisticsImage.IsNotNull())
109  {
110  calculator->SetInputImage(m_StatisticsImage);
111  }
112  else
113  {
114  statisticCalculationSuccessful = false;
115  }
116 
117  // Bug 13416 : The ImageStatistics::SetImageMask() method can throw exceptions, i.e. when the dimensionality
118  // of the masked and input image differ, we need to catch them and mark the calculation as failed
119  // the same holds for the ::SetPlanarFigure()
120  try
121  {
122  if(this->m_BinaryMask.IsNotNull())
123  {
125  imgMask->SetImageMask(m_BinaryMask->Clone());
126  calculator->SetMask(imgMask.GetPointer());
127  }
128  if(this->m_PlanarFigureMask.IsNotNull())
129  {
131  pfMaskGen->SetInputImage(m_StatisticsImage);
132  pfMaskGen->SetPlanarFigure(m_PlanarFigureMask->Clone());
133  calculator->SetMask(pfMaskGen.GetPointer());
134  }
135  }
136  catch (const mitk::Exception& e)
137  {
138  MITK_ERROR << "MITK Exception: " << e.what();
139  m_message = e.what();
140  statisticCalculationSuccessful = false;
141  }
142  catch( const itk::ExceptionObject& e)
143  {
144  MITK_ERROR << "ITK Exception:" << e.what();
145  m_message = e.what();
146  statisticCalculationSuccessful = false;
147  }
148  catch ( const std::runtime_error &e )
149  {
150  MITK_ERROR<< "Runtime Exception: " << e.what();
151  m_message = e.what();
152  statisticCalculationSuccessful = false;
153  }
154  catch ( const std::exception &e )
155  {
156  MITK_ERROR<< "Standard Exception: " << e.what();
157  m_message = e.what();
158  statisticCalculationSuccessful = false;
159  }
160 
161  if (this->m_IgnoreZeros)
162  {
164  ignorePixelValueMaskGen->SetIgnoredPixelValue(0);
165  ignorePixelValueMaskGen->SetInputImage(m_StatisticsImage);
166  calculator->SetSecondaryMask(ignorePixelValueMaskGen.GetPointer());
167  }
168  else
169  {
170  calculator->SetSecondaryMask(nullptr);
171  }
172 
173  calculator->SetNBinsForHistogramStatistics(m_HistogramNBins);
174 
175  try
176  {
177  calculator->GetStatistics();
178  }
179  catch ( mitk::Exception& e)
180  {
181  m_message = e.GetDescription();
182  MITK_ERROR<< "MITK Exception: " << e.what();
183  statisticCalculationSuccessful = false;
184  }
185  catch ( const std::runtime_error &e )
186  {
187  m_message = "Failure: " + std::string(e.what());
188  MITK_ERROR<< "Runtime Exception: " << e.what();
189  statisticCalculationSuccessful = false;
190  }
191  catch ( const std::exception &e )
192  {
193  m_message = "Failure: " + std::string(e.what());
194  MITK_ERROR<< "Standard Exception: " << e.what();
195  statisticCalculationSuccessful = false;
196  }
197 
198  this->m_CalculationSuccessful = statisticCalculationSuccessful;
199 
200  if(statisticCalculationSuccessful)
201  {
202  m_StatisticsContainer = calculator->GetStatistics();
203  this->m_HistogramVector.clear();
204 
205  for (unsigned int i = 0; i < m_StatisticsImage->GetTimeSteps(); i++)
206  {
207  HistogramType::ConstPointer tempHistogram;
208  try {
209  if(calculator->GetStatistics()->TimeStepExists(i))
210  {
211  tempHistogram = calculator->GetStatistics()->GetStatisticsForTimeStep(i).m_Histogram;
212  this->m_HistogramVector.push_back(tempHistogram);
213  }
214  } catch (mitk::Exception&) {
215  MITK_WARN << ":-(";
216  }
217 
218  }
219 
220  }
221 }
#define MITK_ERROR
Definition: mitkLogMacros.h:20
const mitk::PlanarFigure * GetPlanarFigure() const
static Pointer New()
mitk::ImageStatisticsContainer * GetStatisticsData() const
#define MITK_WARN
Definition: mitkLogMacros.h:19
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
Image class for storing images.
Definition: mitkImage.h:72
void Initialize(const mitk::Image *image, const mitk::Image *binaryImage, const mitk::PlanarFigure *planarFig)
mitk::Image::Pointer image
itk::Statistics::Histogram< double > HistogramType
Base-class for geometric planar (2D) figures, such as lines, circles, rectangles, polygons...
const HistogramType * GetTimeStepHistogram(unsigned int t=0) const
Container class for storing a StatisticsObject for each timestep.