ANIMA  4.0
animaImageClassifierFilter.hxx
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace anima
6 {
7 
8 template <typename TInput, typename TMask, typename TOutput>
10 {
11  this->SetNthInput(0, const_cast<TInput*>(image));
12  m_ImagesVector.push_back(image);
13 }
14 
15 template <typename TInput, typename TMask, typename TOutput>
17 {
18  this->SetNthInput(1, const_cast<TMask*>(mask));
19 }
20 
21 template <typename TInput, typename TMask, typename TOutput>
23 {
24  this->SetNthInput(m_NbInputs, const_cast<TInput*>(image));
25  m_IndexImage2 = m_NbInputs;
26  m_ImagesVector.push_back(image);
27  m_NbInputs++;
28 }
29 
30 template <typename TInput, typename TMask, typename TOutput>
32 {
33  this->SetNthInput(m_NbInputs, const_cast<TInput*>(image));
34  m_IndexImage3 = m_NbInputs;
35  m_ImagesVector.push_back(image);
36  m_NbInputs++;
37 }
38 template <typename TInput, typename TMask, typename TOutput>
40 {
41  this->SetNthInput(m_NbInputs, const_cast<TInput*>(image));
42  m_IndexImage4 = m_NbInputs;
43  m_ImagesVector.push_back(image);
44  m_NbInputs++;
45 }
46 
47 template <typename TInput, typename TMask, typename TOutput>
49 {
50  this->SetNthInput(m_NbInputs, const_cast<TInput*>(image));
51  m_IndexImage5 = m_NbInputs;
52  m_ImagesVector.push_back(image);
53  m_NbInputs++;
54 }
55 
56 
57 
58 template <typename TInput, typename TMask, typename TOutput>
60 {
61  return static_cast< const TInput * >
62  ( this->itk::ProcessObject::GetInput(0) );
63 }
64 
65 template <typename TInput, typename TMask, typename TOutput>
67 {
68  return static_cast< const TMask * >
69  ( this->itk::ProcessObject::GetInput(1) );
70 }
71 
72 template <typename TInput, typename TMask, typename TOutput>
74 {
75  return static_cast< const TInput * >
76  ( this->itk::ProcessObject::GetInput(m_IndexImage2) );
77 }
78 
79 template <typename TInput, typename TMask, typename TOutput>
81 {
82  return static_cast< const TInput * >
83  ( this->itk::ProcessObject::GetInput(m_IndexImage3) );
84 }
85 
86 template <typename TInput, typename TMask, typename TOutput>
88 {
89  return static_cast< const TInput * >
90  ( this->itk::ProcessObject::GetInput(m_IndexImage4) );
91 }
92 
93 template <typename TInput, typename TMask, typename TOutput>
95 {
96  return static_cast< const TInput * >
97  ( this->itk::ProcessObject::GetInput(m_IndexImage5) );
98 }
99 
100 template <typename TInput, typename TMask, typename TOutput>
101 void
103 {
104  if( m_OutputFilename != "" )
105  {
106  std::cout << "Writing classification image to: " << m_OutputFilename << std::endl;
107  anima::writeImage<TOutput>(m_OutputFilename, this->GetOutput());
108  }
109 }
110 
111 template <typename TInput, typename TMask, typename TOutput>
112 void
115 {
116  OutputIteratorType classificationIt(this->GetOutput(), outputRegionForThread);
117  MaskConstIteratorType maskIt(this->GetMask(), outputRegionForThread);
118 
119  std::vector<InputConstIteratorType > inputVectorIt;
120  for ( unsigned int i = 0; i < m_ImagesVector.size(); i++ )
121  {
122  InputConstIteratorType It(m_ImagesVector[i], outputRegionForThread );
123  inputVectorIt.push_back(It);
124  }
125 
126  while(!classificationIt.IsAtEnd())
127  {
128  classificationIt.Set(0);
129  if ( maskIt.Get() != 0 )
130  {
131  // fill a vector with images information
132  DoubleVariableSizeMatrixType intensities(inputVectorIt.size(),1);
133  for ( unsigned int i = 0; i < inputVectorIt.size(); i++ )
134  {
135  intensities( i,0 ) = inputVectorIt[ i ].Get();
136  }
137 
138  double maxProbability = -1.0;
139  int maxProbaIndex = -1;
140  for ( unsigned int i = 0; i < m_GaussianModel.size(); i++ )
141  {
142  double proba = m_Alphas[i] * this->probability( intensities, m_GaussianModel[i] );
143  if ( maxProbability < proba )
144  {
145  maxProbability = proba;
146  maxProbaIndex = i+1;
147  }
148  }
149  if ( maxProbaIndex > 0 )
150  {
151  classificationIt.Set(maxProbaIndex);
152  }
153  else
154  {
155  classificationIt.Set(m_GaussianModel.size()+1);
156  }
157  }
158  ++classificationIt;
159  ++maskIt;
160  for ( unsigned int i = 0; i < m_ImagesVector.size(); i++ )
161  {
162  ++inputVectorIt[i];
163  }
164  }
165 
166 }
167 
168 
169 template <typename TInput, typename TMask, typename TOutput>
170 double
172 {
173  DoubleVariableSizeMatrixType distance, distanceT, value, sigma, mu;
174  sigma = model->GetCovariance();
175 
176  GaussianFunctionType::MeanVectorType muVect = model->GetMean();
177  mu.SetSize(muVect.Size(),1);
178  for(unsigned int j = 0; j < muVect.Size(); j++)
179  {
180  mu(j,0) = muVect[j];
181  }
182 
183  double down = static_cast<double>(std::sqrt( std::pow(2*M_PI,muVect.Size()) * std::abs( vnl_determinant(sigma.GetVnlMatrix())) ));
184  distance = point - mu;
185  distanceT = distance.GetTranspose();
186  value = distanceT * (sigma.GetInverse() * distance.GetVnlMatrix());
187 
188  if(value(0,0) != value(0,0))
189  return 0.0; //NB index auparavant a 1, verif if ok
190 
191  double exponent =- 0.5* value(0,0);
192  return exp(exponent)/down;
193 }
194 
195 } //end of namespace anima
itk::VariableSizeMatrix< NumericType > DoubleVariableSizeMatrixType
double probability(DoubleVariableSizeMatrixType &point, GaussianFunctionType::Pointer model)
void SetInputImage5(const TInput *image)
Superclass::OutputImageRegionType OutputImageRegionType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) ITK_OVERRIDE
void SetInputImage4(const TInput *image)
void SetInputImage2(const TInput *image)
itk::ImageRegionConstIterator< TMask > MaskConstIteratorType
itk::ImageRegionIterator< TOutput > OutputIteratorType
void SetInputImage1(const TInput *image)
void SetInputImage3(const TInput *image)
Classify each voxels into one of the given GMM classes.
itk::ImageRegionConstIterator< TInput > InputConstIteratorType