Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPASpectralUnmixingSO2.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 
14 
15 // ImageAccessor
16 #include <mitkImageReadAccessor.h>
17 #include <mitkImageWriteAccessor.h>
18 
19 
21 {
22  this->SetNumberOfIndexedInputs(2);
23  this->SetNumberOfIndexedOutputs(2);
24  this->SetNthOutput(0, mitk::Image::New());
25  this->SetNthOutput(1, mitk::Image::New());
26 
27 }
28 
30 {
31 
32 }
33 
35 {
37 }
38 
39 void mitk::pa::SpectralUnmixingSO2::GenerateData()
40 {
41  MITK_INFO(m_Verbose) << "GENERATING DATA..";
42 
43  // Get input image
44  mitk::Image::Pointer inputHbO2 = GetInput(0);
45  mitk::Image::Pointer inputHb = GetInput(1);
46 
47  CheckPreConditions(inputHbO2, inputHb);
48 
49  unsigned int xDim = inputHbO2->GetDimensions()[0];
50  unsigned int yDim = inputHbO2->GetDimensions()[1];
51  unsigned int zDim = inputHbO2->GetDimensions()[2];
52 
53  InitializeOutputs();
54 
55  mitk::ImageReadAccessor readAccessHbO2(inputHbO2);
56  mitk::ImageReadAccessor readAccessHb(inputHb);
57 
58  const float* inputDataArrayHbO2 = ((const float*)readAccessHbO2.GetData());
59  const float* inputDataArrayHb = ((const float*)readAccessHb.GetData());
60 
61  auto output = GetOutput(0);
62  auto output1 = GetOutput(1);
63 
64  mitk::ImageWriteAccessor writeOutput(output);
65  float* writeBuffer = (float *)writeOutput.GetData();
66 
67  mitk::ImageWriteAccessor writeOutput1(output1);
68  float* writeBuffer1 = (float *)writeOutput1.GetData();
69 
70  for (unsigned int x = 0; x < xDim; x++)
71  {
72  for (unsigned int y = 0; y < yDim; y++)
73  {
74  for (unsigned int z = 0;z < zDim; z++)
75  {
76  unsigned int pixelNumber = (xDim*yDim * z) + x * yDim + y;
77  float pixelHb = inputDataArrayHb[pixelNumber];
78  float pixelHbO2 = inputDataArrayHbO2[pixelNumber];
79  float resultSO2 = CalculateSO2(pixelHb, pixelHbO2);
80  writeBuffer[(xDim*yDim * z) + x * yDim + y] = resultSO2;
81  float resultTHb = CalculateTHb(pixelHb, pixelHbO2);
82  writeBuffer1[(xDim*yDim * z) + x * yDim + y] = resultTHb;
83  }
84  }
85  }
86  MITK_INFO(m_Verbose) << "GENERATING DATA...[DONE]";
87 }
88 
89 void mitk::pa::SpectralUnmixingSO2::CheckPreConditions(mitk::Image::Pointer inputHbO2, mitk::Image::Pointer inputHb)
90 {
91  unsigned int xDimHb = inputHb->GetDimensions()[0];
92  unsigned int yDimHb = inputHb->GetDimensions()[1];
93  unsigned int zDimHb = inputHb->GetDimensions()[2];
94 
95  unsigned int xDimHbO2 = inputHbO2->GetDimensions()[0];
96  unsigned int yDimHbO2 = inputHbO2->GetDimensions()[1];
97  unsigned int zDimHbO2 = inputHbO2->GetDimensions()[2];
98 
99  if (xDimHb != xDimHbO2 || yDimHb != yDimHbO2 || zDimHb != zDimHbO2)
100  mitkThrow() << "DIMENTIONALITY ERROR!";
101 
102  if (inputHbO2->GetPixelType() != mitk::MakeScalarPixelType<float>())
103  mitkThrow() << "PIXELTYPE ERROR! FLOAT REQUIRED";
104 
105  if (inputHb->GetPixelType() != mitk::MakeScalarPixelType<float>())
106  mitkThrow() << "PIXELTYPE ERROR! FLOAT REQUIRED";
107 
108  MITK_INFO(m_Verbose) << "CHECK PRECONDITIONS ...[DONE]";
109 }
110 
111 void mitk::pa::SpectralUnmixingSO2::InitializeOutputs()
112 {
113  // UNUSED unsigned int numberOfInputs = GetNumberOfIndexedInputs();
114  unsigned int numberOfOutputs = GetNumberOfIndexedOutputs();
115 
116  mitk::PixelType pixelType = mitk::MakeScalarPixelType<float>();
117  const int NUMBER_OF_SPATIAL_DIMENSIONS = 3;
118  auto* dimensions = new unsigned int[NUMBER_OF_SPATIAL_DIMENSIONS];
119  for(unsigned int dimIdx=0; dimIdx<NUMBER_OF_SPATIAL_DIMENSIONS; dimIdx++)
120  {
121  dimensions[dimIdx] = GetInput()->GetDimensions()[dimIdx];
122  }
123 
124  for (unsigned int outputIdx = 0; outputIdx < numberOfOutputs; outputIdx++)
125  {
126  GetOutput(outputIdx)->Initialize(pixelType, NUMBER_OF_SPATIAL_DIMENSIONS, dimensions);
127  }
128 }
129 
130 float mitk::pa::SpectralUnmixingSO2::CalculateSO2(float Hb, float HbO2)
131 {
132  float result = HbO2 / (Hb + HbO2);
133 
134  if (result != result)
135  {
136  MITK_WARN(m_Verbose) << "SO2 VALUE NAN! WILL BE SET TO ZERO!";
137  return 0;
138  }
139  else
140  {
141  if (SO2ValueNotSiginificant(Hb, HbO2, result))
142  {
143  return 0;
144  }
145  else return result;
146  }
147 }
148 
149 float mitk::pa::SpectralUnmixingSO2::CalculateTHb(float Hb, float HbO2)
150 {
151  float result = (Hb + HbO2);
152 
153  if (result != result)
154  {
155  MITK_WARN(m_Verbose) << "SO2 VALUE NAN! WILL BE SET TO ZERO!";
156  return 0;
157  }
158  else
159  {
160  return result;
161  }
162 }
163 
165 {
166  m_SO2Settings.push_back(value);
167 }
168 
169 bool mitk::pa::SpectralUnmixingSO2::SO2ValueNotSiginificant(float Hb, float HbO2, float result)
170 {
171  std::vector<float> significant;
172  significant.push_back(HbO2);
173  significant.push_back(Hb);
174  significant.push_back(HbO2 + Hb);
175  significant.push_back(100*(result));
176 
177  for (unsigned int i = 0; i < m_SO2Settings.size(); ++i)
178  {
179  if (m_SO2Settings[i] != 0 && m_SO2Settings[i] > significant[i] && (std::abs(m_SO2Settings[i] - significant[i]) > 1e-7))
180  {
181  return true;
182  }
183  }
184  return false;
185 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
SpectralUnmixingSO2()
Constructor sets number of input images to two and number of output images to one, respectively.
unsigned int * GetDimensions() const
Get the sizes of all dimensions as an integer-array.
Definition: mitkImage.cpp:1309
void * GetData()
Gives full data access.
virtual void AddSO2Settings(int value)
AddSO2Settings takes integers and writes them at the end of the m_SO2Settings vector.
#define MITK_WARN
Definition: mitkLogMacros.h:19
#define mitkThrow()
bool verbose(false)
static Pointer New()
InputImageType * GetInput(void)
OutputType * GetOutput()
Get the output data of this image source object.
ImageWriteAccessor class to get locked write-access for a particular image part.
ImageReadAccessor class to get locked read access for a particular image part.
const void * GetData() const
Gives const access to the data.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
virtual void Verbose(bool verbose)
Verbose gives more information to the console. Default value is false.