Orfeo Toolbox  3.16
itkImageClassifierBase.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkImageClassifierBase.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-24 20:02:54 $
7  Version: $Revision: 1.14 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkImageClassifierBase_txx
18 #define __itkImageClassifierBase_txx
19 #include "itkImageClassifierBase.h"
20 
21 namespace itk
22 {
23 
24 template<class TInputImage,
25  class TClassifiedImage>
28 {
29 
30 }
31 
32 template<class TInputImage,
33  class TClassifiedImage>
36 {
37 
38 }
39 
43 template <class TInputImage,
44  class TClassifiedImage>
45 void
47 ::PrintSelf( std::ostream& os, Indent indent ) const
48 {
49  Superclass::PrintSelf(os,indent);
50 
51  os << indent << "General Image Classifier / Clusterer" << std::endl;
52  os << indent << "ClassifiedImage: ";
53  os << m_ClassifiedImage.GetPointer() << std::endl;
54  os << indent << "InputImage: ";
55  os << m_InputImage.GetPointer() << std::endl;
56 
57  signed int i;
58  const unsigned int length = static_cast<unsigned int>( m_PixelMembershipValue.size() );
59  const signed int last = static_cast<int>( length ) - 1;
60 
61  os << indent << "Pixel membership: [";
62  for ( i = 0; i < last; i++)
63  {
64  os << m_PixelMembershipValue[i] << ", ";
65  }
66  if ( length >= 1 )
67  {
68  os << m_PixelMembershipValue[last];
69  }
70  os << "]" << std::endl;
71 
72 }// end PrintSelf
73 
77 template <class TInputImage,
78  class TClassifiedImage>
79 void
82 {
83  this->Classify();
84 
85 }// end Generate data
86 
87 //------------------------------------------------------------------
88 // The core function where classification is carried out
89 //------------------------------------------------------------------
90 template<class TInputImage,
91  class TClassifiedImage>
92 void
95 {
96 
97  ClassifiedImagePointer classifiedImage = this->GetClassifiedImage();
98  //Check if the an output buffer has been allocated
99  if( !classifiedImage )
100  {
101  this->Allocate();
102 
103  //To trigger the pipeline process
104  this->Modified();
105  }
106 
107  //--------------------------------------------------------------------
108  // Set the iterators and the pixel type definition for the input image
109  //-------------------------------------------------------------------
110  InputImageConstPointer inputImage = this->GetInputImage();
111  InputImageConstIterator inIt( inputImage, inputImage->GetBufferedRegion() );
112 
113  //--------------------------------------------------------------------
114  // Set the iterators and the pixel type definition for the classified image
115  //--------------------------------------------------------------------
116  classifiedImage = this->GetClassifiedImage();
117 
119  classifiedIt( classifiedImage, classifiedImage->GetBufferedRegion() );
120 
121  //--------------------------------------------------------------------
122  //Set up the vector to store the image data
123 
124  InputImagePixelType inputImagePixel;
125  ClassifiedImagePixelType outputClassifiedLabel;
126 
127  //Set up the storage containers to record the probability
128  //measures for each class.
129  unsigned int numberOfClasses = this->GetNumberOfMembershipFunctions();
130 
131  std::vector< double > discriminantScores;
132  discriminantScores.resize( numberOfClasses );
133  unsigned int classLabel;
134  unsigned int classIndex;
135 
136  // support progress methods/callbacks
137  unsigned long totalPixels =
138  inputImage->GetBufferedRegion().GetNumberOfPixels();
139  unsigned long updateVisits = totalPixels / 10;
140  if( updateVisits < 1 )
141  {
142  updateVisits = 1;
143  }
144  int k = 0;
145 
146  for ( inIt.GoToBegin(); ! inIt.IsAtEnd(); ++inIt, ++classifiedIt, ++k )
147  {
148 
149  if ( !( k % updateVisits ) )
150  {
151  this->UpdateProgress((float)k / (float)totalPixels);
152  }
153 
154  //Read the input vector
155  inputImagePixel = inIt.Get();
156  for (classIndex = 0; classIndex < numberOfClasses; classIndex++)
157  {
158  discriminantScores[classIndex] =
159  (this->GetMembershipFunction(classIndex))->Evaluate(inputImagePixel);
160  }
161 
162 
163  classLabel = this->GetDecisionRule()->Evaluate(discriminantScores);
164 
165  outputClassifiedLabel = ClassifiedImagePixelType ( classLabel );
166  classifiedIt.Set( outputClassifiedLabel );
167  }// end for (looping throught the dataset)
168 
169 }// end Classify
170 
174 template<class TInputImage,
175  class TClassifiedImage>
176 void
179 {
180  InputImageConstPointer inputImage = this->GetInputImage();
181 
182  InputImageSizeType inputImageSize = inputImage->GetBufferedRegion().GetSize();
183 
184  ClassifiedImagePointer classifiedImage = TClassifiedImage::New();
185  this->SetClassifiedImage(classifiedImage);
186 
187  typedef typename TClassifiedImage::IndexType myIndex;
188  typename TClassifiedImage::IndexType classifiedImageIndex;
189  classifiedImageIndex.Fill(0);
190 
191  typename TClassifiedImage::RegionType classifiedImageRegion;
192 
193  classifiedImageRegion.SetSize( inputImageSize );
194  classifiedImageRegion.SetIndex( classifiedImageIndex );
195 
196  classifiedImage->SetLargestPossibleRegion( classifiedImageRegion );
197  classifiedImage->SetBufferedRegion( classifiedImageRegion );
198  classifiedImage->Allocate();
199 
200 }
201 
202 template<class TInputImage,
203  class TClassifiedImage>
204 const std::vector< double > &
207 {
208 
209  unsigned int numberOfClasses = this->GetNumberOfClasses();
210  if( m_PixelMembershipValue.size() != numberOfClasses )
211  {
212  m_PixelMembershipValue.resize( numberOfClasses );
213  }
214 
215  for (unsigned int classIndex = 0; classIndex < numberOfClasses; classIndex++)
216  {
217  m_PixelMembershipValue[classIndex] =
218  (this->GetMembershipFunction(classIndex))->Evaluate(inputImagePixel);
219  }
220 
221  //Return the membership value of the
222  return m_PixelMembershipValue;
223 
224 }
225 
226 
227 } // namespace itk
228 
229 #endif

Generated at Sat Feb 2 2013 23:42:41 for Orfeo Toolbox with doxygen 1.8.1.1