Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 <mitkImageAccessByItk.h>
18 
19 #include "itkImageRegionConstIteratorWithIndex.h"
21 
22 #include <limits>
23 
24 namespace mitk
25 {
28  template <typename TPixel, unsigned int VImageDimension>
29  void CalculateSegmentationVolume::ItkImageProcessing(itk::Image<TPixel, VImageDimension> *itkImage,
30  TPixel *itkNotUsed(dummy))
31  {
32  itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>> iterBinaryImage(
33  itkImage, itkImage->GetLargestPossibleRegion());
34  typename itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>>::IndexType currentIndex;
35  typename itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>>::IndexType minIndex;
36  for (unsigned int i = 0; i < VImageDimension; ++i)
37  minIndex[i] = std::numeric_limits<long int>::max();
38 
39  typename itk::ImageRegionConstIteratorWithIndex<itk::Image<TPixel, VImageDimension>>::IndexType maxIndex;
40  for (unsigned int i = 0; i < VImageDimension; ++i)
41  maxIndex[i] = std::numeric_limits<long int>::min();
42 
43  m_CenterOfMass.Fill(0.0);
44 
45  m_Volume = 0;
46  while (!iterBinaryImage.IsAtEnd())
47  {
48  if (iterBinaryImage.Get() > static_cast<TPixel>(0.0))
49  {
50  // update center of mass
51  currentIndex = iterBinaryImage.GetIndex();
52  itk::Vector<ScalarType, VImageDimension> currentPoint;
53  for (unsigned int i = 0; i < VImageDimension; ++i)
54  currentPoint[i] = currentIndex[i];
55 
56  m_CenterOfMass =
57  (m_CenterOfMass *
58  (static_cast<ScalarType>(m_Volume) /
59  static_cast<ScalarType>(m_Volume + 1))) // e.g. 3 points: old center * 2/3 + currentPoint * 1/3;
60  + currentPoint / static_cast<ScalarType>(m_Volume + 1);
61 
62  // update number of voxels
63  ++m_Volume;
64 
65  // update bounding box
66  for (unsigned int i = 0; i < VImageDimension; ++i)
67  {
68  if (currentIndex[i] < minIndex[i])
69  minIndex[i] = currentIndex[i];
70  if (currentIndex[i] > maxIndex[i])
71  maxIndex[i] = currentIndex[i];
72  }
73  }
74 
75  ++iterBinaryImage;
76  }
77 
78  m_MinIndexOfBoundingBox[2] = 0.0;
79  m_MaxIndexOfBoundingBox[2] = 0.0;
80  for (unsigned int i = 0; i < VImageDimension; ++i)
81  {
82  m_MinIndexOfBoundingBox[i] = minIndex[i];
83  m_MaxIndexOfBoundingBox[i] = maxIndex[i];
84  }
85  }
86 
88  {
89  Image::Pointer image;
90  GetPointerParameter("Input", image);
91 
92  return image.IsNotNull() && GetGroupNode();
93  }
94 
96  {
97  // get image
98  Image::Pointer image;
99  GetPointerParameter("Input", image);
100 
101  AccessFixedDimensionByItk(image.GetPointer(),
103  3); // some magic to call the correctly templated function (we only do 3D images here!)
104 
105  // consider single voxel volume
106  Vector3D spacing = image->GetSlicedGeometry()->GetSpacing(); // spacing in mm
107  float volumeML = (ScalarType)m_Volume * spacing[0] * spacing[1] * spacing[2] / 1000.0; // convert to ml
108 
109  DataNode *groupNode = GetGroupNode();
110  if (groupNode)
111  {
112  groupNode->SetProperty("volume", FloatProperty::New(volumeML));
113  groupNode->SetProperty("centerOfMass", Vector3DProperty::New(m_CenterOfMass));
114  groupNode->SetProperty("boundingBoxMinimum", Vector3DProperty::New(m_MinIndexOfBoundingBox));
115  groupNode->SetProperty("boundingBoxMaximum", Vector3DProperty::New(m_MaxIndexOfBoundingBox));
116  groupNode->SetProperty("showVolume", BoolProperty::New(true));
117  }
118 
119  return true;
120  }
121 
122 } // 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()
static Pointer New()
static T max(T x, T y)
Definition: svm.cpp:70
static Pointer New()
static T min(T x, T y)
Definition: svm.cpp:67
void SetProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr)
Set the property (instance of BaseProperty) with key propertyKey in the PropertyList of the renderer ...
void ItkImageProcessing(itk::Image< TPixel, VImageDimension > *itkImage, TPixel *dummy=NULL)
void GetPointerParameter(const char *parameter, itk::SmartPointer< T > &value) const
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66