Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkCalculateSegmentationVolume.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 
13 #include <mitkImageAccessByItk.h>
14 
15 #include "itkImageRegionConstIteratorWithIndex.h"
17 
18 #include <limits>
19 
20 namespace mitk
21 {
24  template <typename TPixel, unsigned int VImageDimension>
25  void CalculateSegmentationVolume::ItkImageProcessing(itk::Image<TPixel, VImageDimension> *itkImage,
26  TPixel *itkNotUsed(dummy))
27  {
28  itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>> iterBinaryImage(
29  itkImage, itkImage->GetLargestPossibleRegion());
30  typename itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>>::IndexType currentIndex;
31  typename itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>>::IndexType minIndex;
32  for (unsigned int i = 0; i < VImageDimension; ++i)
33  minIndex[i] = std::numeric_limits<long int>::max();
34 
35  typename itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>>::IndexType maxIndex;
36  for (unsigned int i = 0; i < VImageDimension; ++i)
37  maxIndex[i] = std::numeric_limits<long int>::min();
38 
39  m_CenterOfMass.Fill(0.0);
40 
41  m_Volume = 0;
42  while (!iterBinaryImage.IsAtEnd())
43  {
44  if (iterBinaryImage.Get() > static_cast<TPixel>(0.0))
45  {
46  // update center of mass
47  currentIndex = iterBinaryImage.GetIndex();
48  itk::Vector<ScalarType, VImageDimension> currentPoint;
49  for (unsigned int i = 0; i < VImageDimension; ++i)
50  currentPoint[i] = currentIndex[i];
51 
52  m_CenterOfMass =
53  (m_CenterOfMass *
54  (static_cast<ScalarType>(m_Volume) /
55  static_cast<ScalarType>(m_Volume + 1))) // e.g. 3 points: old center * 2/3 + currentPoint * 1/3;
56  + currentPoint / static_cast<ScalarType>(m_Volume + 1);
57 
58  // update number of voxels
59  ++m_Volume;
60 
61  // update bounding box
62  for (unsigned int i = 0; i < VImageDimension; ++i)
63  {
64  if (currentIndex[i] < minIndex[i])
65  minIndex[i] = currentIndex[i];
66  if (currentIndex[i] > maxIndex[i])
67  maxIndex[i] = currentIndex[i];
68  }
69  }
70 
71  ++iterBinaryImage;
72  }
73 
74  m_MinIndexOfBoundingBox[2] = 0.0;
75  m_MaxIndexOfBoundingBox[2] = 0.0;
76  for (unsigned int i = 0; i < VImageDimension; ++i)
77  {
78  m_MinIndexOfBoundingBox[i] = minIndex[i];
79  m_MaxIndexOfBoundingBox[i] = maxIndex[i];
80  }
81  }
82 
84  {
86  GetPointerParameter("Input", image);
87 
88  return image.IsNotNull() && GetGroupNode();
89  }
90 
92  {
93  // get image
95  GetPointerParameter("Input", image);
96 
97  AccessFixedDimensionByItk(image.GetPointer(),
99  3); // some magic to call the correctly templated function (we only do 3D images here!)
100 
101  // consider single voxel volume
102  Vector3D spacing = image->GetSlicedGeometry()->GetSpacing(); // spacing in mm
103  float volumeML = (ScalarType)m_Volume * spacing[0] * spacing[1] * spacing[2] / 1000.0; // convert to ml
104 
105  DataNode *groupNode = GetGroupNode();
106  if (groupNode)
107  {
108  groupNode->SetProperty("volume", FloatProperty::New(volumeML));
109  groupNode->SetProperty("centerOfMass", Vector3DProperty::New(m_CenterOfMass));
110  groupNode->SetProperty("boundingBoxMinimum", Vector3DProperty::New(m_MinIndexOfBoundingBox));
111  groupNode->SetProperty("boundingBoxMaximum", Vector3DProperty::New(m_MaxIndexOfBoundingBox));
112  groupNode->SetProperty("showVolume", BoolProperty::New(true));
113  }
114 
115  return true;
116  }
117 
118 } // namespace
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
static Pointer New()
void SetProperty(const std::string &propertyKey, BaseProperty *property, const std::string &contextName="", bool fallBackOnDefaultContext=false) override
Add new or change existent property.
static Pointer New()
static T max(T x, T y)
Definition: svm.cpp:56
mitk::Image::Pointer image
static Pointer New()
static T min(T x, T y)
Definition: svm.cpp:53
void ItkImageProcessing(itk::Image< TPixel, VImageDimension > *itkImage, TPixel *dummy=nullptr)
void GetPointerParameter(const char *parameter, itk::SmartPointer< T > &value) const
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57