OTB  9.0.0
Orfeo Toolbox
otbLabelMapWithClassLabelToLabeledSampleListFilter.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 otbLabelMapWithClassLabelToLabeledSampleListFilter_hxx
22 #define otbLabelMapWithClassLabelToLabeledSampleListFilter_hxx
23 
25 #include "itkDataObject.h"
26 
27 namespace otb
28 {
29 
30 template <class TInputLabelMap, class TOutputListSample, class TOutputTrainingListSample, class TMeasurementFunctor>
31 LabelMapWithClassLabelToLabeledSampleListFilter<TInputLabelMap, TOutputListSample, TOutputTrainingListSample,
33 {
34  this->SetNumberOfRequiredOutputs(2);
35  this->itk::ProcessObject::SetNthOutput(1, this->MakeOutput(1).GetPointer());
36 }
37 
38 template <class TInputLabelMap, class TOutputListSample, class TOutputTrainingListSample, class TMeasurementFunctor>
39 LabelMapWithClassLabelToLabeledSampleListFilter<TInputLabelMap, TOutputListSample, TOutputTrainingListSample,
40  TMeasurementFunctor>::~LabelMapWithClassLabelToLabeledSampleListFilter()
41 {
42 }
43 
44 
45 template <class TInputLabelMap, class TOutputListSample, class TOutputTrainingListSample, class TMeasurementFunctor>
46 typename LabelMapWithClassLabelToLabeledSampleListFilter<TInputLabelMap, TOutputListSample, TOutputTrainingListSample,
47  TMeasurementFunctor>::DataObjectPointerType
50 {
51  DataObjectPointerType output;
52 
53  switch (idx)
54  {
55  case 0:
56  output = static_cast<itk::DataObject*>(OutputSampleListType::New().GetPointer());
57  break;
58  case 1:
59  output = static_cast<itk::DataObject*>(OutputTrainingSampleListType::New().GetPointer());
60  break;
61  default:
62  output = static_cast<itk::DataObject*>(OutputSampleListType::New().GetPointer());
63  break;
64  }
65 
66  return output;
67 }
68 
69 // Get the output training sample list
70 template <class TInputLabelMap, class TOutputListSample, class TOutputTrainingListSample, class TMeasurementFunctor>
71 const typename LabelMapWithClassLabelToLabeledSampleListFilter<TInputLabelMap, TOutputListSample, TOutputTrainingListSample,
72  TMeasurementFunctor>::OutputTrainingSampleListType*
73 LabelMapWithClassLabelToLabeledSampleListFilter<TInputLabelMap, TOutputListSample, TOutputTrainingListSample,
74  TMeasurementFunctor>::GetOutputTrainingSampleList()
75 {
76  return dynamic_cast<OutputTrainingSampleListType*>(this->itk::ProcessObject::GetOutput(1));
77 }
78 
79 template <class TInputLabelMap, class TOutputListSample, class TOutputTrainingListSample, class TMeasurementFunctor>
81 {
82  // Get input
83  InputLabelMapConstPointerType inputLabelMap = this->GetInputLabelMap();
84 
85  // Get outputs
86  OutputSampleListPointerType outputSampleList = const_cast<OutputSampleListType*>(this->GetOutputSampleList());
87 
88  OutputTrainingSampleListPointerType outputTrainingSampleList = const_cast<OutputTrainingSampleListType*>(this->GetOutputTrainingSampleList());
89 
90  // Clear the ListSamples
91  outputSampleList->Clear();
92  outputTrainingSampleList->Clear();
93 
94  // Lets begin by declaring the iterator for the objects in the image.
95  ConstIteratorType it = ConstIteratorType(inputLabelMap);
96 
97  bool isFirstIteration = true;
98  // iterate on label objects
99  while (!it.IsAtEnd())
100  {
101  TraningVectorType label;
102  if (it.GetLabelObject()->HasClassLabel())
103  {
104  label[0] = static_cast<typename TraningVectorType::ValueType>(it.GetLabelObject()->GetClassLabel());
105  }
106  else
107  {
108  label[0] = itk::NumericTraits<typename InputLabelMapType::LabelObjectType::ClassLabelType>::max();
109  }
110  // Before pushin back the measurement vectors to the sampleList
111  // initialize their sizes : once
112  if (isFirstIteration)
113  {
114  // initialize the output sampleList
115  typename OutputSampleListType::MeasurementVectorSizeType measurementSize;
116  measurementSize = m_MeasurementFunctor(it.GetLabelObject()).Size();
117  outputSampleList->SetMeasurementVectorSize(measurementSize);
118 
119  // initialize the training samplelist
120  outputTrainingSampleList->SetMeasurementVectorSize(label.Size());
121  isFirstIteration = false;
122  }
123 
124  outputSampleList->PushBack(m_MeasurementFunctor(it.GetLabelObject()));
125  outputTrainingSampleList->PushBack(label);
126  ++it;
127  }
128 }
129 
130 template <class TInputLabelMap, class TOutputListSample, class TOutputTrainingListSample, class TMeasurementFunctor>
132  std::ostream& os, itk::Indent indent) const
133 {
134  Superclass::PrintSelf(os, indent);
135 }
136 
137 } // end namespace otb
138 
139 #endif
otb::LabelMapWithClassLabelToLabeledSampleListFilter::OutputSampleListType
TOutputSampleList OutputSampleListType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:68
otb::LabelMapWithClassLabelToLabeledSampleListFilter::OutputSampleListPointerType
OutputSampleListType::Pointer OutputSampleListPointerType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:69
otb::LabelMapWithClassLabelToLabeledSampleListFilter::TraningVectorType
OutputTrainingSampleListType::MeasurementVectorType TraningVectorType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:75
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LabelMapWithClassLabelToLabeledSampleListFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.hxx:131
otb::LabelMapWithClassLabelToLabeledSampleListFilter
This class converts a LabelObjectMap with some class labeled objects to a SampleList and a TrainingSa...
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:45
otb::LabelMapWithClassLabelToLabeledSampleListFilter::OutputTrainingSampleListPointerType
OutputTrainingSampleListType::Pointer OutputTrainingSampleListPointerType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:74
otbLabelMapWithClassLabelToLabeledSampleListFilter.h
otb::LabelMapWithClassLabelToLabeledSampleListFilter::MakeOutput
DataObjectPointerType MakeOutput(DataObjectPointerArraySizeType idx) override
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.hxx:48
otb::LabelMapWithClassLabelToLabeledSampleListFilter::ConstIteratorType
InputLabelMapType::ConstIterator ConstIteratorType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:64
otb::LabelMapWithClassLabelToLabeledSampleListFilter::OutputTrainingSampleListType
TOutputTrainingSampleList OutputTrainingSampleListType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:73
otb::LabelMapWithClassLabelToLabeledSampleListFilter::DataObjectPointerType
Superclass::DataObjectPointerType DataObjectPointerType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:81
otb::LabelMapWithClassLabelToLabeledSampleListFilter::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:65
otb::LabelMapWithClassLabelToLabeledSampleListFilter::GenerateData
void GenerateData() override
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.hxx:80
otb::LabelMapWithClassLabelToLabeledSampleListFilter::InputLabelMapConstPointerType
InputLabelMapType::ConstPointer InputLabelMapConstPointerType
Definition: otbLabelMapWithClassLabelToLabeledSampleListFilter.h:62