Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLabeledImageVolumeCalculator.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 #include "mitkImageAccessByItk.h"
19 
20 #include <itkImageRegionConstIteratorWithIndex.h>
21 
22 namespace mitk
23 {
25  {
27 
28  m_DummyPoint.Fill(0.0);
29  }
30 
32  double LabeledImageVolumeCalculator::GetVolume(unsigned int label) const
33  {
34  if (label < m_VolumeVector.size())
35  return m_VolumeVector[label];
36  else
37  return 0.0;
38  }
39 
40  const Point3D &LabeledImageVolumeCalculator::GetCentroid(unsigned int label) const
41  {
42  if (label < m_CentroidVector.size())
43  return m_CentroidVector[label];
44  else
45  return m_DummyPoint;
46  }
47 
49  {
50  return m_VolumeVector;
51  }
52 
54  {
55  return m_CentroidVector;
56  }
57 
59  {
60  if (m_Image.IsNull())
61  {
62  itkExceptionMacro(<< "Image not set!");
63  return;
64  }
65 
66  m_InputTimeSelector->SetInput(m_Image);
67 
68  m_InputTimeSelector->SetTimeNr(0);
69  m_InputTimeSelector->UpdateLargestPossibleRegion();
70 
71  AccessByItk_2(m_InputTimeSelector->GetOutput(), _InternalCalculateVolumes, this, m_Image->GetGeometry(0));
72  //}
73  }
74 
75  template <typename TPixel, unsigned int VImageDimension>
76  void LabeledImageVolumeCalculator::_InternalCalculateVolumes(itk::Image<TPixel, VImageDimension> *image,
77  LabeledImageVolumeCalculator * /*volumeCalculator*/,
78  BaseGeometry *geometry)
79  {
80  typedef itk::Image<TPixel, VImageDimension> ImageType;
81  typedef typename ImageType::IndexType IndexType;
82  typedef itk::ImageRegionConstIteratorWithIndex<ImageType> IteratorType;
83 
84  // Reset volume and centroid vectors
85  m_VolumeVector.clear();
86  m_CentroidVector.clear();
87 
88  // Iterate over image and determine number of voxels and centroid
89  // per label
90  IteratorType it(image, image->GetBufferedRegion());
91  for (it.GoToBegin(); !it.IsAtEnd(); ++it)
92  {
93  const IndexType &index = it.GetIndex();
94  unsigned int pixel = static_cast<unsigned int>(it.Get());
95 
96  if (m_VolumeVector.size() <= pixel)
97  {
98  m_VolumeVector.resize(pixel + 1);
99  m_CentroidVector.resize(pixel + 1);
100  }
101 
102  m_VolumeVector[pixel] += 1.0;
103 
104  m_CentroidVector[pixel][0] += index[0];
105  m_CentroidVector[pixel][1] += index[1];
106  m_CentroidVector[pixel][2] += index[2];
107  }
108 
109  // Calculate voxel volume from spacing
110  const Vector3D &spacing = geometry->GetSpacing();
111  double voxelVolume = spacing[0] * spacing[1] * spacing[2];
112 
113  // Calculate centroid (in world coordinates) and volumes for all labels
114  for (unsigned int i = 0; i < m_VolumeVector.size(); ++i)
115  {
116  if (m_VolumeVector[i] > 0.0)
117  {
118  m_CentroidVector[i][0] /= m_VolumeVector[i];
119  m_CentroidVector[i][1] /= m_VolumeVector[i];
120  m_CentroidVector[i][2] /= m_VolumeVector[i];
122 
123  m_VolumeVector[i] *= voxelVolume;
124  }
125  }
126  }
127 }
const Point3D & GetCentroid(unsigned int label) const
DataCollection - Class to facilitate loading/accessing structured data.
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
void _InternalCalculateVolumes(itk::Image< TPixel, VImageDimension > *image, LabeledImageVolumeCalculator *volumeCalculator, BaseGeometry *geometry)
map::core::discrete::Elements< 3 >::InternalImageType ImageType
Class for calculating the volume (or area) for each label in a labeled image.
void IndexToWorld(const mitk::Vector3D &vec_units, mitk::Vector3D &vec_mm) const
Convert (continuous or discrete) index coordinates of a vector vec_units to world coordinates (in mm)...
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
BaseGeometry Describes the geometry of a data object.
static Pointer New()