OTB  9.0.0
Orfeo Toolbox
otbBandsStatisticsAttributesLabelMapFilter.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 otbBandsStatisticsAttributesLabelMapFilter_hxx
22 #define otbBandsStatisticsAttributesLabelMapFilter_hxx
23 
25 #include "itkVectorIndexSelectionCastImageFilter.h"
26 
27 namespace otb
28 {
29 namespace Functor
30 {
32 template <class TLabelObject, class TFeatureImage>
34 {
35 }
36 
38 template <class TLabelObject, class TFeatureImage>
40 {
41 }
42 
44 template <class TLabelObject, class TFeatureImage>
46 {
47  bool resp = true;
48  resp = resp && (m_ReducedAttributeSet != self.m_ReducedAttributeSet);
49  resp = resp && (m_StatsFunctorsMap != self.m_StatsFunctorsMap);
50  return resp;
51 }
53 
54 template <class TLabelObject, class TFeatureImage>
56 {
57  return !(this != self);
58 }
59 
63 template <class TLabelObject, class TFeatureImage>
65 {
66  // Walk every registered functors
67  for (typename StatsFunctorsMapType::const_iterator it = m_StatsFunctorsMap.begin(); it != m_StatsFunctorsMap.end(); ++it)
68  {
69  (it->second)(lo);
70  }
71 }
73 
75 template <class TLabelObject, class TFeatureImage>
76 void BandStatsAttributesLabelObjectFunctor<TLabelObject, TFeatureImage>::AddFeature(const std::string& name, const TFeatureImage* img)
77 {
78  // Create a new functor
79  StatsFunctorType newFunctor;
80 
81  // Set the reduced attribute set option
82  newFunctor.SetReducedAttributeSet(m_ReducedAttributeSet);
83 
84  // Set the feature and its name
85  newFunctor.SetFeatureName(name);
86 
87  // Set the feature image
88  newFunctor.SetFeatureImage(img);
89 
90  // Add it to the map
91  m_StatsFunctorsMap[name] = newFunctor;
92 }
93 
95 template <class TLabelObject, class TFeatureImage>
97 {
98  return (m_StatsFunctorsMap.erase(name) == 1);
99 }
100 
102 template <class TLabelObject, class TFeatureImage>
104 {
105  if (m_StatsFunctorsMap.count(name) == 0)
106  {
107  itkGenericExceptionMacro(<< "No feature named " << name << " in map.");
108  }
109  return m_StatsFunctorsMap[name].GetFeatureImage();
110 }
112 
114 template <class TLabelObject, class TFeatureImage>
116 {
117  m_StatsFunctorsMap.clear();
118 }
119 
121 template <class TLabelObject, class TFeatureImage>
123 {
124  return m_StatsFunctorsMap.size();
125 }
126 
128 template <class TLabelObject, class TFeatureImage>
130 {
131  // Set the flag
132  m_ReducedAttributeSet = flag;
133 
134  // Set the flag to all the already existing functors
135  for (typename StatsFunctorsMapType::iterator it = m_StatsFunctorsMap.begin(); it != m_StatsFunctorsMap.end(); ++it)
136  {
137  it->second.SetReducedAttributeSet(m_ReducedAttributeSet);
138  }
139 }
141 template <class TLabelObject, class TFeatureImage>
143 {
144  return m_ReducedAttributeSet;
145 }
146 } // End namespace Functor
147 
148 template <class TImage, class TFeatureImage>
150 {
151  this->SetNumberOfRequiredInputs(2);
152 }
153 
155 template <class TImage, class TFeatureImage>
157 {
158  // Set the Nth input
159  this->SetNthInput(1, const_cast<TFeatureImage*>(input));
160 }
161 
163 template <class TImage, class TFeatureImage>
166 {
167  return static_cast<const TFeatureImage*>(this->itk::ProcessObject::GetInput(1));
168 }
169 
171 template <class TImage, class TFeatureImage>
173 {
174  if (this->GetFunctor().GetReducedAttributeSet() != flag)
175  {
176  this->GetFunctor().SetReducedAttributeSet(flag);
177  this->Modified();
178  }
179 }
181 
183 template <class TImage, class TFeatureImage>
185 {
186  return this->GetFunctor().GetReducedAttributeSet();
187 }
188 
189 
190 template <class TImage, class TFeatureImage>
192 {
193  // if told to run in place and the types support it,
194  if (this->GetInPlace() && this->CanRunInPlace())
195  {
196  // Graft this first input to the output. Later, we'll need to
197  // remove the input's hold on the bulk data.
198  //
199  ImagePointer inputAsOutput = dynamic_cast<TImage*>(const_cast<TImage*>(this->GetInput()));
200 
201  if (inputAsOutput)
202  {
203 
204  this->GraftOutput(inputAsOutput);
205  this->GetOutput()->SetLargestPossibleRegion(this->GetOutput()->GetLargestPossibleRegion());
206  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
207  this->GetOutput()->SetBufferedRegion(this->GetOutput()->GetBufferedRegion());
208  }
209 
210  // If there are more than one outputs, allocate the remaining outputs
211  for (unsigned int i = 1; i < this->GetNumberOfOutputs(); ++i)
212  {
213  ImagePointer outputPtr;
214 
215  outputPtr = this->GetOutput(i);
216  outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
217  outputPtr->Allocate();
218  }
219  }
220  else
221  {
222  //
223 
224  Superclass::AllocateOutputs();
225  // copy the content of the input image to the output image (will be done by ImageSource AllocateOutputs Method)
226  // would never occur : inputasoutput condition is always true, since output and input type is TImage for
227  // BandsStatisticsAttributesLabelMapFilter class
228  }
229 }
230 
231 
232 template <class TImage, class TFeatureImage>
234 {
235  itk::ImageToImageFilter<TImage, TImage>::GenerateInputRequestedRegion();
236 }
237 
238 
239 template <class TImage, class TFeatureImage>
241 {
242  // First call superclass implementation
243  Superclass::BeforeThreadedGenerateData();
244 
245  unsigned long nbComponents = this->GetFeatureImage()->GetNumberOfComponentsPerPixel();
246 
247  // Clear any previous feature
248  this->GetFunctor().ClearAllFeatures();
249 
250  // Add each band of the feature image to the statistics functor
251  for (unsigned int i = 0; i < nbComponents; ++i)
252  {
253  typedef itk::VectorIndexSelectionCastImageFilter<FeatureImageType, InternalImageType> VectorIndexSelectionCastImageFilterType;
254 
255  typename VectorIndexSelectionCastImageFilterType::Pointer band = VectorIndexSelectionCastImageFilterType::New();
256  band->SetInput(this->GetFeatureImage());
257  band->SetIndex(i);
258  band->UpdateOutputInformation();
259  band->GetOutput()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
260  band->Update();
261  std::ostringstream oss;
262  oss << "Band" << i + 1; // [1..N] convention in feature naming
263  this->GetFunctor().AddFeature(oss.str(), band->GetOutput());
264  }
265 }
266 
267 template <class TImage, class TFeatureImage>
269 {
270  Superclass::PrintSelf(os, indent);
271 }
272 
273 } // end namespace otb
274 #endif
otb::BandsStatisticsAttributesLabelMapFilter::GetReducedAttributeSet
bool GetReducedAttributeSet() const
Definition: otbBandsStatisticsAttributesLabelMapFilter.hxx:184
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::StatisticsAttributesLabelObjectFunctor::SetReducedAttributeSet
void SetReducedAttributeSet(bool flag)
Definition: otbStatisticsAttributesLabelMapFilter.hxx:335
otb::operator==
constexpr bool operator==(extents< StaticExtentsL... > const &lhs, extents< StaticExtentsR... > const &rhs)
Definition: otbExtents.h:150
otb::Functor::BandStatsAttributesLabelObjectFunctor::SetReducedAttributeSet
void SetReducedAttributeSet(bool flag)
Definition: otbBandsStatisticsAttributesLabelMapFilter.hxx:129
otb::Functor::BandStatsAttributesLabelObjectFunctor
Functor to compute bands statistics attributes.
Definition: otbBandsStatisticsAttributesLabelMapFilter.h:51
otb::Functor::BandStatsAttributesLabelObjectFunctor::BandStatsAttributesLabelObjectFunctor
BandStatsAttributesLabelObjectFunctor()
Definition: otbBandsStatisticsAttributesLabelMapFilter.hxx:33
otb::BandsStatisticsAttributesLabelMapFilter::FeatureImageType
TFeatureImage FeatureImageType
Definition: otbBandsStatisticsAttributesLabelMapFilter.h:148
otb::Functor::StatisticsAttributesLabelObjectFunctor::SetFeatureName
void SetFeatureName(const std::string &name)
Definition: otbStatisticsAttributesLabelMapFilter.hxx:307
otb::Functor::StatisticsAttributesLabelObjectFunctor::SetFeatureImage
void SetFeatureImage(const TFeatureImage *img)
Definition: otbStatisticsAttributesLabelMapFilter.hxx:321
otbBandsStatisticsAttributesLabelMapFilter.h
otb::BandsStatisticsAttributesLabelMapFilter::ImagePointer
ImageType::Pointer ImagePointer
Definition: otbBandsStatisticsAttributesLabelMapFilter.h:161
otb::operator!=
constexpr bool operator!=(extents< StaticExtentsL... > const &lhs, extents< StaticExtentsR... > const &rhs)
Definition: otbExtents.h:164
otb::BandsStatisticsAttributesLabelMapFilter::SetReducedAttributeSet
void SetReducedAttributeSet(bool flag)
Definition: otbBandsStatisticsAttributesLabelMapFilter.hxx:172
otb::Functor::BandStatsAttributesLabelObjectFunctor< TImage::LabelObjectType, otb::Image< double, 2 > >::LabelObjectType
TImage::LabelObjectType LabelObjectType
Typedef of the label object.
Definition: otbBandsStatisticsAttributesLabelMapFilter.h:61
otb::BandsStatisticsAttributesLabelMapFilter
This filter computes band statistics attributes for each object.
Definition: otbBandsStatisticsAttributesLabelMapFilter.h:139
otb::Functor::StatisticsAttributesLabelObjectFunctor
Functor to compute statistics attributes of one LabelObject.
Definition: otbStatisticsAttributesLabelMapFilter.h:42