Orfeo Toolbox  4.2
otbSVMImageClassificationFilter.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 __otbSVMImageClassificationFilter_txx
19 #define __otbSVMImageClassificationFilter_txx
20 
22 #include "itkImageRegionIterator.h"
23 #include "itkProgressReporter.h"
24 
25 namespace otb
26 {
30 template <class TInputImage, class TOutputImage, class TMaskImage>
33 {
34  this->SetNumberOfRequiredInputs(2);
35  this->SetNumberOfRequiredInputs(1);
37 }
38 
39 template <class TInputImage, class TOutputImage, class TMaskImage>
40 void
43 {
44  this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType *>(mask));
45 }
46 
47 template <class TInputImage, class TOutputImage, class TMaskImage>
49 ::MaskImageType *
52 {
53  if (this->GetNumberOfInputs() < 2)
54  {
55  return 0;
56  }
57  return static_cast<const MaskImageType *>(this->itk::ProcessObject::GetInput(1));
58 }
59 
60 template <class TInputImage, class TOutputImage, class TMaskImage>
61 void
64 {
65  if (!m_Model)
66  {
67  itkGenericExceptionMacro(<< "No model for classification");
68  }
69 }
70 
71 template <class TInputImage, class TOutputImage, class TMaskImage>
72 void
74 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
75 {
76  // Get the input pointers
77  InputImageConstPointerType inputPtr = this->GetInput();
78  MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
79  OutputImagePointerType outputPtr = this->GetOutput();
80 
81  // Progress reporting
82  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
83 
84  // Define iterators
85  typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
86  typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
87  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
88 
89  InputIteratorType inIt(inputPtr, outputRegionForThread);
90  OutputIteratorType outIt(outputPtr, outputRegionForThread);
91 
92  // Eventually iterate on masks
93  MaskIteratorType maskIt;
94  if (inputMaskPtr)
95  {
96  maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
97  maskIt.GoToBegin();
98  }
99 
100  bool validPoint = true;
101 
102  // Walk the part of the image
103  for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd(); ++inIt, ++outIt)
104  {
105  // Check pixel validity
106  if (inputMaskPtr)
107  {
108  validPoint = maskIt.Get() > 0;
109  ++maskIt;
110  }
111  // If point is valid
112  if (validPoint)
113  {
114  // Classifify
115  typename ModelType::MeasurementType measure;
116  for (unsigned int i = 0; i < inIt.Get().Size(); ++i)
117  {
118  measure.push_back(inIt.Get()[i]);
119  }
120  outIt.Set(m_Model->EvaluateLabel(measure));
121  }
122  else
123  {
124  // else, set default value
125  outIt.Set(m_DefaultLabel);
126  }
127  progress.CompletedPixel();
128  }
129 
130 }
134 template <class TInputImage, class TOutputImage, class TMaskImage>
135 void
137 ::PrintSelf(std::ostream& os, itk::Indent indent) const
138 {
139  Superclass::PrintSelf(os, indent);
140 }
141 } // End namespace otb
142 #endif

Generated at Sat Aug 30 2014 16:29:06 for Orfeo Toolbox with doxygen 1.8.3.1