Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVolumeCalculator.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 #include "mitkVolumeCalculator.h"
18 #include "mitkImageAccessByItk.h"
19 #include <itkImageRegionConstIterator.h>
20 
22 
23 template <typename TPixel, unsigned int VImageDimension>
24 void mitk::VolumeCalculator::InternalCompute(const itk::Image<TPixel, VImageDimension> *itkImage)
25 {
26  itk::ImageRegionConstIterator<itk::Image<TPixel, VImageDimension>> imageIt(itkImage,
27  itkImage->GetLargestPossibleRegion());
28  unsigned long int count = 0;
29 
30  for (imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt)
31  {
32  if ((int)(imageIt.Get()) >= m_Threshold)
33  {
34  count++;
35  }
36  }
37  if (itkImage->GetLargestPossibleRegion().GetImageDimension() == 3)
38  {
39  m_Volume = count / 1000.0 * itkImage->GetSpacing()[0] * itkImage->GetSpacing()[1] * itkImage->GetSpacing()[2];
40  }
41  else if (itkImage->GetLargestPossibleRegion().GetImageDimension() == 2)
42  {
43  m_Volume = count / 100.0 * itkImage->GetSpacing()[0] * itkImage->GetSpacing()[1];
44  }
45  m_VoxelCount = count;
46 }
47 
48 mitk::VolumeCalculator::VolumeCalculator() : m_Image(nullptr), m_Threshold(0), m_Volume(0)
49 {
51 }
52 
54 {
55 }
56 
58 {
59  return m_Volumes;
60 }
61 
63 {
64  const_cast<Image *>(m_Image.GetPointer())->SetRequestedRegionToLargestPossibleRegion();
65  if (m_Image->GetDimension() == 4)
66  {
67  m_TimeSelector->SetInput(m_Image);
68  m_Volumes.resize(m_Image->GetDimension(3));
69  for (unsigned int timeStep = 0; timeStep < m_Image->GetDimension(3); ++timeStep)
70  {
71  m_TimeSelector->SetTimeNr(timeStep);
72  m_TimeSelector->Update();
73  AccessFixedDimensionByItk(m_TimeSelector->GetOutput(), InternalCompute, 3);
74  m_Volumes[timeStep] = m_Volume;
75  }
76  }
77  else if (m_Image->GetDimension() == 3)
78  {
79  const_cast<Image *>(m_Image.GetPointer())->Update();
81  }
82  else if (m_Image->GetDimension() == 2)
83  {
84  const_cast<Image *>(m_Image.GetPointer())->Update();
86  }
87 }
88 
90 {
91  unsigned int dim = m_Image->GetDimension();
92 
93  if (dim == 4)
94  {
95  m_Volumes.resize(m_Image->GetDimension(3), 0);
96  Vector3D spacing = m_Image->GetSlicedGeometry()->GetSpacing();
97 
98  for (unsigned int t = 0; t < m_Image->GetDimension(3); ++t)
99  {
100  m_Volumes[t] =
101  m_Image->GetStatistics()->GetCountOfMaxValuedVoxels(t) / 1000.0 * spacing[0] * spacing[1] * spacing[2];
102  }
103  }
104  else if (dim == 3)
105  {
106  Vector3D spacing = m_Image->GetSlicedGeometry()->GetSpacing();
107  m_Volume = m_Image->GetStatistics()->GetCountOfMaxValuedVoxels() / 1000.0 * spacing[0] * spacing[1] * spacing[2];
108  }
109  else if (dim == 2)
110  {
111  Vector3D spacing = m_Image->GetGeometry()->GetSpacing();
112  m_Volume = m_Image->GetStatistics()->GetCountOfMaxValuedVoxels() / 100.0 * spacing[0] * spacing[1];
113  }
114  else
115  itkExceptionMacro(<< "Wrong image dimension...");
116 }
117 
118 float mitk::VolumeCalculator::ComputeVolume(Vector3D spacing, unsigned int voxelCount)
119 {
120  return (voxelCount / 1000.0 * spacing[0] * spacing[1] * spacing[2]);
121 }
ImageTimeSelector::Pointer m_TimeSelector
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:35
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
void InternalCompute(const itk::Image< TPixel, VImageDimension > *itkImage)
unsigned long int m_VoxelCount
std::vector< float > GetVolumes()
static Pointer New()