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