OTB  10.0.0
Orfeo Toolbox
otbImageToLabelMapWithAttributesFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbImageToLabelMapWithAttributesFilter_hxx
22 #define otbImageToLabelMapWithAttributesFilter_hxx
23 
25 #include "itkProcessObject.h"
26 
27 namespace otb
28 {
29 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
31 {
32  m_Output = LabelMapType::New();
33  this->itk::ProcessObject::SetNumberOfRequiredInputs(2);
34 }
35 
36 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
38 {
39  // call the superclass' implementation of this method
40  Superclass::GenerateInputRequestedRegion();
41 
42  // We need all the input.
43  typename InputImageType::Pointer input = const_cast<InputImageType*>(this->GetInput());
44 
45  if (!input)
46  {
47  return;
48  }
49 
50  input->SetRequestedRegion(input->GetLargestPossibleRegion());
51 }
52 
53 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
55 {
56  // Process object is not const-correct so the const_cast is required here
57  this->itk::ProcessObject::SetNthInput(0, const_cast<InputImageType*>(image));
58 }
59 
60 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
62 {
63  // Process object is not const-correct so the const_cast is required here
64  this->itk::ProcessObject::SetNthInput(1, const_cast<LabelledImageType*>(image));
65 }
66 
67 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
70 {
71  if (this->GetNumberOfInputs() < 2)
72  {
73  return nullptr;
74  }
75 
76  return static_cast<const InputImageType*>(this->itk::ProcessObject::GetInput(0));
77 }
78 
79 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
82 {
83  if (this->GetNumberOfInputs() < 2)
84  {
85  return nullptr;
86  }
87 
88  return static_cast<const LabelledImageType*>(this->itk::ProcessObject::GetInput(1));
89 }
90 
91 
92 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
95 {
96  return dynamic_cast<LabelMapType*>(this->Superclass::GetOutput(0));
97 }
98 
99 
100 template <class TInputImage, class TLabeledImage, class TOutputLabel, class TObjectLabel>
102 {
103  typename InputImageType::Pointer inputImage = const_cast<InputImageType*>(this->GetInput());
104  typename LabelledImageType::Pointer labelledImage = const_cast<LabelledImageType*>(this->GetLabelledImage());
105 
106  // Convert to label map
107  typename LabelMapFilterType::Pointer lfilter = LabelMapFilterType::New();
108  lfilter->SetBackgroundValue(itk::NumericTraits<LabelType>::max());
109  lfilter->SetInput(labelledImage);
110 
111  // Compute shape attributes
112  typename ShapeLabelMapFilterType::Pointer shapeLabelMapFilter = ShapeLabelMapFilterType::New();
113  shapeLabelMapFilter->SetInput(lfilter->GetOutput());
114 
115  // Compute radiometric attributes
116  typename BandStatisticsLabelMapFilterType::Pointer bandStatsLabelMapFilter = BandStatisticsLabelMapFilterType::New();
117  bandStatsLabelMapFilter->SetInput(shapeLabelMapFilter->GetOutput());
118  bandStatsLabelMapFilter->SetFeatureImage(inputImage);
119 
120  // Get the label map
121  bandStatsLabelMapFilter->GetOutput()->SetAdjacencyMap(lfilter->GetOutput()->GetAdjacencyMap());
122  bandStatsLabelMapFilter->GraftOutput(this->GetOutput());
123 
124  // execute the mini-pipeline
125  bandStatsLabelMapFilter->Update();
126 
127  // graft the mini-pipeline output back onto this filter's output.
128  this->GraftOutput(bandStatsLabelMapFilter->GetOutput());
129 }
130 }
131 
132 #endif
virtual void SetLabelledImage(const LabelledImageType *image)
This class is a LabelMap with additional adjacency information.
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.