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
mitkCostFunctionBase.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 <mitkCostFunctionBase.h>
18 
19 //Itk Iterators
20 #include <itkNeighborhoodIterator.h>
21 #include <itkImageRegionIterator.h>
22 
23 //Set Functions
24 template <typename TPixel, unsigned int VImageDimension>
25 void mitk::CostFunctionBase<TPixel, VImageDimension>::SetRegion(std::pair<RegionType, int> regionPair)
26 {
27  m_RegionPair = regionPair;
28 }
29 
30 template <typename TPixel, unsigned int VImageDimension>
31 void mitk::CostFunctionBase<TPixel, VImageDimension>::SetImage(itk::Image<TPixel, VImageDimension> * image)
32 {
33  m_Image = image;
34 }
35 
36 //Other Functions
37 template < typename TPixel, unsigned int VImageDimension >
39 {
40  //Calculate the volume of the region cuboid
41  itk::Size<3> size = m_RegionPair.first.GetSize();
42  double volume = size[0] * size[1] * size[2];
43 
44  typedef itk::Image< int, VImageDimension > IntegerImageType;
45  itk::ImageRegionIterator<IntegerImageType> it_region(m_Image, m_RegionPair.first);
46 
47  double voxelsWithRegionValue(1); //Because the possible new voxel hasn't been set to the region value yet
48  double voxelsWithValueZero(0);
49 
50  for(it_region.GoToBegin(); !it_region.IsAtEnd(); ++it_region)
51  {
52  if (it_region.Value() == m_RegionPair.second)
53  {
54  voxelsWithRegionValue++;
55  }
56  if (it_region.Value() == 0)
57  {
58  voxelsWithValueZero++;
59  }
60  }
61 
62  double measurement = voxelsWithRegionValue / (volume - voxelsWithValueZero);
63 
64  if (measurement > 0.3)
65  {
66  return 1;
67  }
68  return 0;
69 }
70 
71 template < typename TPixel, unsigned int VImageDimension >
73 {
74  itk::Size<3> size = m_RegionPair.first.GetSize();
75 
76  if (size[0] < size[1] + size[2] + 1 && size[1] < size[0] + size[2] + 1 && size[2] < size[0] + size[1] + 1)
77  {
78  return 1;
79  }
80  return 0;
81 }
82 
83 template < typename TPixel, unsigned int VImageDimension >
85 {
86  std::pair<RegionType, int> chosenRegion = m_RegionPair;
87 
88  typedef itk::Image< int, VImageDimension > IntegerImageType;
89  itk::ImageRegionIterator<IntegerImageType> it_region(m_Image, chosenRegion.first);
90  std::vector<double> centerOfMass;
91  centerOfMass = this->GetCenterOfMass();
92  typename IntegerImageType::IndexType indexCenterOfMass;
93  indexCenterOfMass.SetElement(0, centerOfMass[0] + 0.5);
94  indexCenterOfMass.SetElement(1, centerOfMass[1] + 0.5);
95  indexCenterOfMass.SetElement(2, centerOfMass[2] + 0.5);
96  it_region.SetIndex(indexCenterOfMass);
97  int value = it_region.Value();
98 
99  if (it_region.Value() == chosenRegion.second || it_region.Value() == 0)
100  {
101  return 1;
102  }
103  return 0;
104 }
105 
106 template < typename TPixel, unsigned int VImageDimension >
108 {
109  int costFunctionValue(0);
110  std::vector<int> costVector;
111  costVector.push_back(this->RegionIsFullEnough());
112  costVector.push_back(this->SidesAreNotTooLong());
113  costVector.push_back(this->COMLiesWithinParcel());
114 
115  m_weight.clear();
116  m_weight.push_back(1); //weight for RegionIsFullEnough
117  m_weight.push_back(1); //weight for SidesAreNotTooLong
118  m_weight.push_back(0); //weight for COMLiesWithinParcel
119 
120  //Vector multiplication
121  for (int i = 0; i < costVector.size(); i++)
122  {
123  costFunctionValue += costVector[i]*m_weight[i];
124  }
125 
126  return costFunctionValue;
127 }
128 
129 template <typename TPixel, unsigned int VImageDimension>
131 {
132  //Count all tagged voxels in this region
133  itk::ImageRegionIterator<ImageType> it_region(m_Image, m_RegionPair.first);
134 
135  typedef itk::Image< TPixel, VImageDimension > ImageType;
136  int currentSizeOfRegion (0);
137  std::vector<typename ImageType::IndexType> indexVoxel;
138  std::vector<double> centerOfMass;
139  double xValue(0);
140  double yValue(0);
141  double zValue(0);
142 
143  for (it_region.GoToBegin(); !it_region.IsAtEnd(); ++it_region)
144  {
145  if(it_region.Value() == m_RegionPair.second)
146  {
147  indexVoxel.push_back(it_region.GetIndex());
148  currentSizeOfRegion++;
149  }
150  }
151 
152  xValue = 0;
153  yValue = 0;
154  zValue = 0;
155 
156  for (int i = 0; i < currentSizeOfRegion; i++)
157  {
158  xValue += indexVoxel[i][0];
159  yValue += indexVoxel[i][1];
160  zValue += indexVoxel[i][2];
161  }
162 
163  centerOfMass.push_back(xValue/currentSizeOfRegion);
164  centerOfMass.push_back(yValue/currentSizeOfRegion);
165  centerOfMass.push_back(zValue/currentSizeOfRegion);
166 
167  return centerOfMass;
168 }
169 
170 template <typename TPixel, unsigned int VImageDimension>
172 {
173  int maximalValue(0);
174  for (int i = 0; i < m_weight.size(); i++)
175  {
176  maximalValue += m_weight[i];
177  }
178  return maximalValue;
179 }
void SetImage(itk::Image< TPixel, VImageDimension > *)
void SetRegion(std::pair< RegionType, int >)
itk::Image< unsigned char, 3 > ImageType
int CalculateCost()
Checks if the functions RegionIsFullEnough, SidesAreNotTooLong and COMLiesWithinParcel all return val...
int MaximalValue()
Calculates the maximal possible value of the cost function.