OTB  9.0.0
Orfeo Toolbox
otbSOMClassifier.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbSOMClassifier_hxx
22 #define otbSOMClassifier_hxx
23 
24 #include "otbSOMClassifier.h"
25 #include "otbMacro.h"
26 
27 namespace otb
28 {
32 template <class TSample, class TSOMMap, class TLabel>
34 {
35  this->SetNumberOfRequiredInputs(1);
36  SOMMapPointerType sommap = SOMMapType::New();
37  this->SetNthInput(0, sommap.GetPointer());
38  m_Sample = SampleType::New();
40 
41  OutputPointerType outputPtr = OutputType::New();
42  this->SetNthOutput(0, outputPtr.GetPointer());
43 }
44 
45 template <class TSample, class TSOMMap, class TLabel>
47 {
48  return dynamic_cast<OutputType*>(this->itk::ProcessObject::GetOutput(0));
49 }
50 
54 template <class TSample, class TSOMMap, class TLabel>
56 {
57  return dynamic_cast<SOMMapType*>(this->itk::ProcessObject::GetInput(0));
58 }
59 
63 template <class TSample, class TSOMMap, class TLabel>
65 {
66  this->itk::ProcessObject::SetNthInput(0, const_cast<SOMMapType*>(sommap));
67 }
68 
72 template <class TSample, class TSOMMap, class TLabel>
73 void SOMClassifier<TSample, TSOMMap, TLabel>::PrintSelf(std::ostream& os, itk::Indent indent) const
74 {
75  Superclass::PrintSelf(os, indent);
76 }
77 
81 template <class TSample, class TSOMMap, class TLabel>
83 {
84  typename OutputType::Pointer outputPtr = this->GetOutput();
85  outputPtr->SetSample(this->GetSample());
86  // outputPtr->Resize(this->GetSample()->Size()); //FIXME Check if still required
87  typename SOMMapType::SizeType size = this->GetMap()->GetLargestPossibleRegion().GetSize();
88  unsigned int numberOfClasses = 1;
89  for (unsigned int i = 0; i < SOMMapType::ImageDimension; ++i)
90  {
91  numberOfClasses *= size[i];
92  }
93  outputPtr->SetNumberOfClasses(numberOfClasses);
95 
96  typename TSample::Iterator iter = this->GetSample()->Begin();
97  typename TSample::Iterator end = this->GetSample()->End();
98 
99  typename OutputType::ConstIterator iterO = outputPtr->Begin();
100  typename OutputType::ConstIterator endO = outputPtr->End();
101  typename TSample::MeasurementVectorType measurements;
102 
103  typename SOMMapType::IndexType index;
104 
105  SOMMapPointerType somMap = this->GetMap();
106 
107  otbMsgDebugMacro(<< "Starting iterations ");
108  while (iter != end && iterO != endO)
109  {
110 
111  measurements = iter.GetMeasurementVector();
112  index = somMap->GetWinner(measurements);
113  ClassLabelType classLabel = static_cast<ClassLabelType>((index[1] * size[1]) + index[0]);
114  outputPtr->AddInstance(classLabel, iterO.GetInstanceIdentifier());
115  ++iter;
116  ++iterO;
117  }
118 }
119 } // end of namespace otb
120 
121 #endif
otb::SOMClassifier::SOMMapPointerType
SOMMapType::Pointer SOMMapPointerType
Definition: otbSOMClassifier.h:70
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::SOMClassifier::GenerateData
void GenerateData() override
Definition: otbSOMClassifier.hxx:82
otb::SOMClassifier::SOMClassifier
SOMClassifier()
Definition: otbSOMClassifier.hxx:33
otbMacro.h
otb::SOMClassifier::SOMMapType
TSOMMap SOMMapType
Definition: otbSOMClassifier.h:69
otb::SOMClassifier::GetMap
SOMMapType * GetMap(void)
Definition: otbSOMClassifier.hxx:55
otb::SOMClassifier::ClassLabelType
TLabel ClassLabelType
Definition: otbSOMClassifier.h:77
otb::SOMClassifier::SetMap
void SetMap(SOMMapType *sommap)
Accessors.
Definition: otbSOMClassifier.hxx:64
otb::SOMClassifier::GetOutput
OutputType * GetOutput()
Definition: otbSOMClassifier.hxx:46
otb::SOMClassifier::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbSOMClassifier.hxx:73
otbMsgDebugMacro
#define otbMsgDebugMacro(x)
Definition: otbMacro.h:62
otb::SOMClassifier::OutputType
itk::Statistics::MembershipSample< SampleType > OutputType
Definition: otbSOMClassifier.h:73
otb::SOMClassifier::OutputPointerType
OutputType::Pointer OutputPointerType
Definition: otbSOMClassifier.h:74
otbSOMClassifier.h