Orfeo Toolbox  3.16
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->SetNumberOfInputs(2);
35  this->SetNumberOfRequiredInputs(1);
36  m_DefaultLabel = itk::NumericTraits<LabelType>::ZeroValue();
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, int 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 
86  InputIteratorType inIt(inputPtr, outputRegionForThread);
87 
88  MaskIteratorType maskIt;
89  if (inputMaskPtr)
90  {
91  maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
92  maskIt.GoToBegin();
93  }
94  unsigned int maxDimension = m_Map->GetNumberOfComponentsPerPixel();
95  unsigned int sampleSize = std::min(inputPtr->GetNumberOfComponentsPerPixel(),
96  maxDimension);
97  bool validPoint = true;
98 
99  for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
100  {
101  if (inputMaskPtr)
102  {
103  validPoint = maskIt.Get() > 0;
104  ++maskIt;
105  }
106  if (validPoint)
107  {
108  SampleType sample;
109  sample.SetSize(sampleSize);
110  sample.Fill(itk::NumericTraits<ValueType>::ZeroValue());
111  for (unsigned int i = 0; i < sampleSize; ++i)
112  {
113  sample[i] = inIt.Get()[i];
114  }
115  listSample->PushBack(sample);
116  }
117  }
118  ClassifierPointerType classifier = ClassifierType::New();
119  classifier->SetMap(m_Map);
120  classifier->SetSample(listSample);
121  classifier->Update();
122 
123  typename ClassifierType::OutputType::Pointer membershipSample = classifier->GetOutput();
124  typename ClassifierType::OutputType::ConstIterator sampleIter = membershipSample->Begin();
125  typename ClassifierType::OutputType::ConstIterator sampleLast = membershipSample->End();
126 
127  OutputIteratorType outIt(outputPtr, outputRegionForThread);
128 
129  outIt.GoToBegin();
130 
131  while (!outIt.IsAtEnd() && (sampleIter != sampleLast))
132  {
133  outIt.Set(m_DefaultLabel);
134  ++outIt;
135  }
136 
137  outIt.GoToBegin();
138 
139  if (inputMaskPtr)
140  {
141  maskIt.GoToBegin();
142  }
143  validPoint = true;
144 
145  while (!outIt.IsAtEnd() && (sampleIter != sampleLast))
146  {
147  if (inputMaskPtr)
148  {
149  validPoint = maskIt.Get() > 0;
150  ++maskIt;
151  }
152  if (validPoint)
153  {
154  outIt.Set(sampleIter.GetClassLabel());
155  ++sampleIter;
156  }
157  ++outIt;
158  }
159 }
163 template <class TInputImage, class TOutputImage, class TSOMMap, class TMaskImage>
164 void
166 ::PrintSelf(std::ostream& os, itk::Indent indent) const
167 {
168  Superclass::PrintSelf(os, indent);
169 }
170 } // End namespace otb
171 #endif

Generated at Sun Feb 3 2013 00:47:50 for Orfeo Toolbox with doxygen 1.8.1.1