Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkCostingStatistic.cpp
Go to the documentation of this file.
1 #include <mitkCostingStatistic.h>
2 #include <mitkDataCollection.h>
3 
5 #include <sstream>
6 
7 // DataCollection Stuff
9 //stl stuff
10 #include <sstream>
11 
12 static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
13 {
14  typedef itk::Image<double, 3> FeatureImage;
15  typedef itk::Image<unsigned char, 3> LabelImage;
16 
17  if (collection->HasElement(origin))
18  {
19  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(collection->GetData(origin).GetPointer());
20  MITK_INFO << "Creating new Element";
21  if (!collection->HasElement(target) && originImage.IsNotNull())
22  {
23  MITK_INFO << "New image necessary";
25  image->SetRegions(originImage->GetLargestPossibleRegion());
26  image->SetSpacing(originImage->GetSpacing());
27  image->SetOrigin(originImage->GetOrigin());
28  image->SetDirection(originImage->GetDirection());
29  image->Allocate();
30 
31  collection->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),target,"");
32  }
33  }
34  for (std::size_t i = 0; i < collection->Size();++i)
35  {
36  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(collection->GetData(i).GetPointer());
37  if (newCol != 0)
38  {
39  EnsureDataImageInCollection(newCol, origin, target);
40  }
41  }
42 }
43 
44 static void EnsureLabelImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
45 {
46  typedef itk::Image<unsigned char, 3> FeatureImage;
47  typedef itk::Image<unsigned char, 3> LabelImage;
48 
49  if (collection->HasElement(origin))
50  {
51  LabelImage::Pointer originImage = dynamic_cast<LabelImage*>(collection->GetData(origin).GetPointer());
52  MITK_INFO << "Creating new Element";
53  if (!collection->HasElement(target) && originImage.IsNotNull())
54  {
55  MITK_INFO << "New image necessary";
57  image->SetRegions(originImage->GetLargestPossibleRegion());
58  image->SetSpacing(originImage->GetSpacing());
59  image->SetOrigin(originImage->GetOrigin());
60  image->SetDirection(originImage->GetDirection());
61  image->Allocate();
62 
63  collection->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),target,"");
64  }
65  }
66  for (std::size_t i = 0; i < collection->Size();++i)
67  {
68  mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(collection->GetData(i).GetPointer());
69  if (newCol != 0)
70  {
71  EnsureLabelImageInCollection(newCol, origin, target);
72  }
73  }
74 }
75 
77 {
78  m_Collection = collection;
79 }
80 
82 {
83  return m_Collection;
84 }
85 
87 {
88  EnsureDataImageInCollection(m_Collection, m_MaskName, m_CombinedProbabilityA);
89  EnsureDataImageInCollection(m_Collection, m_MaskName, m_CombinedProbabilityB);
90  EnsureLabelImageInCollection(m_Collection, m_MaskName, m_CombinedLabelName);
91 
92  std::vector<DataCollectionImageIterator<double, 3> > iterProbA;
93  std::vector<DataCollectionImageIterator<double, 3> > iterProbB;
94  for (int i = 0; i < m_ProbabilityClassA.size(); ++i)
95  {
96  DataCollectionImageIterator<double, 3> iter(m_Collection, m_ProbabilityClassA[i]);
97  iterProbA.push_back(iter);
98  }
99  for (int i = 0; i < m_ProbabilityClassB.size(); ++i)
100  {
101  DataCollectionImageIterator<double, 3> iter(m_Collection, m_ProbabilityClassB[i]);
102  iterProbB.push_back(iter);
103  }
104  DataCollectionImageIterator<double, 3> iterCombineA(m_Collection, m_CombinedProbabilityA);
105  DataCollectionImageIterator<double, 3> iterCombineB(m_Collection, m_CombinedProbabilityB);
106  DataCollectionImageIterator<unsigned char, 3> iterMask(m_Collection, m_MaskName);
107 
108  while (!iterMask.IsAtEnd())
109  {
110  if (iterMask.GetVoxel() > 0)
111  {
112  double probA = 0;
113  double probB = 0;
114  for (int i = 0; i < iterProbA.size(); ++i)
115  {
116  probA += iterProbA[i].GetVoxel();
117  }
118  for (int i = 0; i < iterProbB.size(); ++i)
119  {
120  probB += iterProbB[i].GetVoxel();
121  }
122  iterCombineA.SetVoxel(probA * 100);
123  iterCombineB.SetVoxel(probB * 100);
124  }
125  else
126  {
127  iterCombineA.SetVoxel(0.0);
128  iterCombineB.SetVoxel(0.0);
129  }
130 
131  ++iterCombineA;
132  ++iterCombineB;
133  ++iterMask;
134  for (int i = 0; i < iterProbA.size(); ++i)
135  {
136  ++(iterProbA[i]);
137  }
138  for (int i = 0; i < iterProbB.size(); ++i)
139  {
140  ++(iterProbB[i]);
141  }
142  }
143  return 0;
144 }
145 
147 {
148  DataCollectionImageIterator<unsigned char, 3> iterMask(m_Collection, m_MaskName);
149  DataCollectionImageIterator<unsigned char, 3> iterLabel(m_Collection, m_CombinedLabelName);
150  DataCollectionImageIterator<double, 3> iterCombineA(m_Collection, m_CombinedProbabilityA);
151  DataCollectionImageIterator<double, 3> iterCombineB(m_Collection, m_CombinedProbabilityB);
152 
153  while (!iterMask.IsAtEnd())
154  {
155  if (iterMask.GetVoxel() > 0)
156  {
157  double probA = iterCombineA.GetVoxel() / (iterCombineA.GetVoxel() + iterCombineB.GetVoxel());
158  probA *= 100;
159  iterLabel.SetVoxel(probA >= threshold ? 1 : 2);
160  }
161  else
162  {
163  iterLabel.SetVoxel(0);
164  }
165 
166  ++iterMask;
167  ++iterLabel;
168  ++iterCombineA;
169  ++iterCombineB;
170  }
171 
172  return true;
173 }
174 
175 bool mitk::CostingStatistic::WriteStatistic(std::ostream &out,std::ostream &sout, double stepSize, std::string shortLabel)
176 {
177  UpdateCollection();
178  for (double threshold = 0 ; threshold <= 100; threshold += stepSize)
179  {
180  CalculateClass(threshold);
181 
182  std::stringstream ss;
183  ss << shortLabel << ";" << threshold;
184 
186  stat.SetCollection(m_Collection);
187  stat.SetClassCount(2);
188  stat.SetGoldName("GTV");
189  stat.SetTestName(m_CombinedLabelName);
190  stat.SetMaskName(m_MaskName);
191  stat.Update();
192 
193  stat.Print(out, sout,false, ss.str());
194  }
195  return true;
196 }
itk::Image< mitk::ScalarType, 3 > FeatureImage
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
DataCollection::Pointer GetCollection()
void Print(std::ostream &out, std::ostream &sout=std::cout, bool withHeader=false, std::string label="None")
bool CalculateClass(double threshold)
void SetTestName(std::string name)
void SetMaskName(std::string name)
void SetGoldName(std::string name)
static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
void SetCollection(DataCollection::Pointer collection)
void SetClassCount(vcl_size_t count)
static void EnsureLabelImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
void SetCollection(DataCollection::Pointer collection)
bool WriteStatistic(std::ostream &out, std::ostream &sout, double stepSize, std::string shortLabel)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.