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