Orfeo Toolbox  4.0
otbSVMImageModelEstimator.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbSVMImageModelEstimator_txx
19 #define __otbSVMImageModelEstimator_txx
20 
22 #include "otbMacro.h"
23 
24 #include "itkCommand.h"
26 
27 namespace otb
28 {
29 
30 template<class TInputImage, class TTrainingImage>
33 {
34  this->SetNumberOfRequiredInputs(2);
35 }
36 
37 template<class TInputImage,
38  class TTrainingImage>
41 {}
42 
43 /*
44  * PrintSelf
45  */
46 template<class TInputImage,
47  class TTrainingImage>
48 void
50 ::PrintSelf(std::ostream& os, itk::Indent indent) const
51 {
52  Superclass::PrintSelf(os, indent);
53 }
54 
55 template<class TInputImage, class TTrainingImage>
56 void
58 ::SetInputImage(const TInputImage * inputImage)
59 {
60  this->itk::ProcessObject::SetNthInput(0, const_cast<TInputImage*>(inputImage));
61 }
62 
63 template<class TInputImage, class TTrainingImage>
64 void
66 ::SetTrainingImage(const TTrainingImage * trainingImage)
67 {
68  this->itk::ProcessObject::SetNthInput(1, const_cast<TTrainingImage*>(trainingImage));
69 }
70 
71 template<class TInputImage, class TTrainingImage>
72 const TInputImage *
75 {
76  if (this->GetNumberOfInputs() < 1)
77  {
78  return 0;
79  }
80  return static_cast<TInputImage *>(this->itk::ProcessObject::GetInput(0));
81 }
82 
83 template<class TInputImage, class TTrainingImage>
84 const TTrainingImage *
87 {
88  if (this->GetNumberOfInputs() < 2)
89  {
90  return 0;
91  }
92  return static_cast<TTrainingImage *>(this->itk::ProcessObject::GetInput(1));
93 }
94 
98 template<class TInputImage,
99  class TTrainingImage>
100 void
103 {
104  // Get input and output pointers
105  const TInputImage * inputImage = this->GetInputImage();
106  const TTrainingImage * trainingImage = this->GetTrainingImage();
107  typename Superclass::ModelType * model = this->GetModel();
108 
109  // Do some error checking
110  typename TInputImage::SizeType
111  inputImageSize = inputImage->GetBufferedRegion().GetSize();
112  typename TTrainingImage::SizeType
113  trainingImageSize = trainingImage->GetBufferedRegion().GetSize();
114 
115  // Check if size of the two inputs are same
116  for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
117  {
118  if (inputImageSize[i] != trainingImageSize[i])
119  throw itk::ExceptionObject(
120  __FILE__,
121  __LINE__,
122  "Input image size is not the same as the training image size.",
123  ITK_LOCATION);
124  }
125 
126  // Declaration of the iterators on the input and training images
127  typedef itk::ImageRegionConstIterator<TInputImage> InputIteratorType;
128  typedef itk::ImageRegionConstIterator<TTrainingImage> TrainingIteratorType;
129 
130  InputIteratorType inIt(inputImage, inputImage->GetBufferedRegion());
131  TrainingIteratorType trIt(trainingImage, trainingImage->GetBufferedRegion());
132 
133  inIt.GoToBegin();
134  trIt.GoToBegin();
135 
136  // Erase previous samples
137  model->ClearSamples();
138 
139  //This works with Image< itk::Vector > and with VectorImage< scalar >.
140  unsigned int numberOfComponents = inIt.Get().Size();
141 
142  while (!inIt.IsAtEnd() && !trIt.IsAtEnd())
143  {
144  if (trIt.Get() != 0)
145  {
146  typename Superclass::ModelType::MeasurementType v;
147 
148  for (unsigned int k = 0; k < numberOfComponents; ++k)
149  {
150  v.push_back(inIt.Get()[k]);
151  }
152 
153  model->AddSample(v, trIt.Get());
154  }
155  ++inIt;
156  ++trIt;
157  }
158 }
159 } //End namespace OTB
160 #endif

Generated at Sat Mar 8 2014 16:22:05 for Orfeo Toolbox with doxygen 1.8.3.1