Orfeo Toolbox  4.0
otbSVMPointSetModelEstimator.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 __otbSVMPointSetModelEstimator_txx
19 #define __otbSVMPointSetModelEstimator_txx
20 
22 #include "itkCommand.h"
23 #include "otbMacro.h"
24 
25 namespace otb
26 {
27 template<class TInputPointSet,
28  class TTrainingPointSet>
30 ::SVMPointSetModelEstimator(void) : SVMModelEstimator<typename TInputPointSet::PixelType::value_type,
31  typename TTrainingPointSet::PixelType>()
32 {
33  this->SetNumberOfRequiredInputs(2);
34 }
35 
36 template<class TInputPointSet,
37  class TTrainingPointSet>
40 {}
41 
42 /*
43  * PrintSelf
44  */
45 template<class TInputPointSet,
46  class TTrainingPointSet>
47 void
49 ::PrintSelf(std::ostream& os, itk::Indent indent) const
50 {
51  Superclass::PrintSelf(os, indent);
52 } // end PrintSelf
53 
54 template<class TInputPointSet, class TTrainingPointSet>
55 void
57 ::SetInputPointSet(const TInputPointSet * inputPointSet)
58 {
59  this->itk::ProcessObject::SetNthInput(0, const_cast<TInputPointSet *>(inputPointSet));
60 }
61 
62 template<class TInputPointSet, class TTrainingPointSet>
63 void
65 ::SetTrainingPointSet(const TTrainingPointSet * trainingPointSet)
66 {
67  this->itk::ProcessObject::SetNthInput(1, const_cast<TTrainingPointSet*>(trainingPointSet));
68 }
69 
70 template<class TInputPointSet, class TTrainingPointSet>
71 const TInputPointSet *
74 {
75  if (this->GetNumberOfInputs() < 1)
76  {
77  return 0;
78  }
79  return static_cast<TInputPointSet *>(this->itk::ProcessObject::GetInput(0));
80 }
81 
82 template<class TInputPointSet, class TTrainingPointSet>
83 const TTrainingPointSet *
86 {
87  if (this->GetNumberOfInputs() < 2)
88  {
89  return 0;
90  }
91  return static_cast<TTrainingPointSet *>(this->itk::ProcessObject::GetInput(1));
92 }
93 
97 template<class TInputPointSet,
98  class TTrainingPointSet>
99 void
102 {
103 
104  //Do some error checking
105  const TInputPointSet * inputPointSet = this->GetInputPointSet();
106  const TTrainingPointSet * trainingPointSet = this->GetTrainingPointSet();
107  typename Superclass::ModelType * model = this->GetModel();
108 
109  int inputPointSetSize = inputPointSet->GetNumberOfPoints();
110  int trainingPointSetSize = trainingPointSet->GetNumberOfPoints();
111 
112  // Check if size of the two inputs are same
113  if (inputPointSetSize != trainingPointSetSize)
114  throw itk::ExceptionObject(
115  __FILE__,
116  __LINE__,
117  "Input pointset size is not the same as the training pointset size.",
118  ITK_LOCATION);
119 
120  // Declaration of the iterators on the input and training images
121  InputPointSetIteratorType inIt = inputPointSet->GetPoints()->Begin();
122  TrainingPointSetIteratorType trIt = trainingPointSet->GetPoints()->Begin();
123 
124  InputPointSetIteratorType inEnd = inputPointSet->GetPoints()->End();
125  TrainingPointSetIteratorType trEnd = trainingPointSet->GetPoints()->End();
126 
127  // Erase previous samples
128  model->ClearSamples();
129 
130  otbMsgDevMacro(<< " Input nb points " << inputPointSetSize);
131  otbMsgDevMacro(<< " Training nb points " << trainingPointSetSize);
132 
133  unsigned int dataId = 0;
134  while (inIt != inEnd && trIt != trEnd)
135  {
136  typename TTrainingPointSet::PixelType label = itk::NumericTraits<typename TTrainingPointSet::PixelType>::Zero;
137  trainingPointSet->GetPointData(dataId, &label);
138 
139  otbMsgDevMacro(<< " Label " << label);
140 
141  typename TInputPointSet::PixelType value;
142  inputPointSet->GetPointData(dataId, &value);
143 
144  typename Superclass::ModelType::MeasurementType v;
145 
146  typename TInputPointSet::PixelType::iterator pIt = value.begin();
147  typename TInputPointSet::PixelType::iterator pEnd = value.end();
148 
149  while (pIt != pEnd)
150  {
151  v.push_back(*pIt);
152  ++pIt;
153  }
154 
155  model->AddSample(v, label);
156 
157  ++inIt;
158  ++trIt;
159  ++dataId;
160  }
161 }
162 } //End namespace OTB
163 #endif

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