Orfeo Toolbox  4.0
otbSOMImageClassificationFilter.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 __otbSOMImageClassificationFilter_txx
19 #define __otbSOMImageClassificationFilter_txx
20 
22 #include "itkImageRegionIterator.h"
23 #include "itkNumericTraits.h"
24 
25 namespace otb
26 {
30 template <class TInputImage, class TOutputImage, class TSOMMap, class TMaskImage>
33 {
34  this->SetNumberOfRequiredInputs(2);
35  this->SetNumberOfRequiredInputs(1);
37 }
38 
39 template <class TInputImage, class TOutputImage, class TSOMMap, class TMaskImage>
40 void
43 {
44  this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType *>(mask));
45 }
46 
47 template <class TInputImage, class TOutputImage, class TSOMMap, 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 TSOMMap, class TMaskImage>
61 void
64 {
65  if (!m_Map)
66  {
67  itkGenericExceptionMacro(<< "No model for classification");
68  }
69 }
70 
71 template <class TInputImage, class TOutputImage, class TSOMMap, class TMaskImage>
72 void
74 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
75 {
76  InputImageConstPointerType inputPtr = this->GetInput();
77  MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
78  OutputImagePointerType outputPtr = this->GetOutput();
79 
80  typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
81  typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
82  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
83 
84  ListSamplePointerType listSample = ListSampleType::New();
85  listSample->SetMeasurementVectorSize(inputPtr->GetNumberOfComponentsPerPixel());
86 
87  InputIteratorType inIt(inputPtr, outputRegionForThread);
88 
89  MaskIteratorType maskIt;
90  if (inputMaskPtr)
91  {
92  maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
93  maskIt.GoToBegin();
94  }
95  unsigned int maxDimension = m_Map->GetNumberOfComponentsPerPixel();
96  unsigned int sampleSize = std::min(inputPtr->GetNumberOfComponentsPerPixel(),
97  maxDimension);
98  bool validPoint = true;
99 
100  for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
101  {
102  if (inputMaskPtr)
103  {
104  validPoint = maskIt.Get() > 0;
105  ++maskIt;
106  }
107  if (validPoint)
108  {
109  SampleType sample;
110  sample.SetSize(sampleSize);
112  for (unsigned int i = 0; i < sampleSize; ++i)
113  {
114  sample[i] = inIt.Get()[i];
115  }
116  listSample->PushBack(sample);
117  }
118  }
119  ClassifierPointerType classifier = ClassifierType::New();
120  classifier->SetMap(m_Map);
121  classifier->SetSample(listSample);
122  classifier->Update();
123 
124  typename ClassifierType::OutputType::Pointer membershipSample = classifier->GetOutput();
125  typename ClassifierType::OutputType::ConstIterator sampleIter = membershipSample->Begin();
126  typename ClassifierType::OutputType::ConstIterator sampleLast = membershipSample->End();
127 
128  OutputIteratorType outIt(outputPtr, outputRegionForThread);
129 
130  outIt.GoToBegin();
131 
132  while (!outIt.IsAtEnd() && (sampleIter != sampleLast))
133  {
134  outIt.Set(m_DefaultLabel);
135  ++outIt;
136  }
137 
138  outIt.GoToBegin();
139 
140  if (inputMaskPtr)
141  {
142  maskIt.GoToBegin();
143  }
144  validPoint = true;
145 
146  while (!outIt.IsAtEnd() && (sampleIter != sampleLast))
147  {
148  if (inputMaskPtr)
149  {
150  validPoint = maskIt.Get() > 0;
151  ++maskIt;
152  }
153  if (validPoint)
154  {
155  outIt.Set(sampleIter.GetClassLabel());
156  ++sampleIter;
157  }
158  ++outIt;
159  }
160 }
164 template <class TInputImage, class TOutputImage, class TSOMMap, class TMaskImage>
165 void
167 ::PrintSelf(std::ostream& os, itk::Indent indent) const
168 {
169  Superclass::PrintSelf(os, indent);
170 }
171 } // End namespace otb
172 #endif

Generated at Sat Mar 8 2014 16:18:38 for Orfeo Toolbox with doxygen 1.8.3.1