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