OTB  9.0.0
Orfeo Toolbox
otbStreamingStatisticsMapFromLabelImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbStreamingStatisticsMapFromLabelImageFilter_hxx
23 #define otbStreamingStatisticsMapFromLabelImageFilter_hxx
25 
26 #include "itkInputDataObjectIterator.h"
27 #include "itkImageRegionIterator.h"
28 #include "itkProgressReporter.h"
29 #include "otbMacro.h"
30 #include <cmath>
31 #include <utility>
32 
33 namespace otb
34 {
35 
36 template <class TInputVectorImage, class TLabelImage>
38  : m_UseNoDataValue()
39 {
40  // first output is a copy of the image, DataObject created by
41  // superclass
42  //
43  // allocate the data objects for the outputs which are
44  // just decorators around pixel types
45  typename PixelValueMapObjectType::Pointer output = static_cast<PixelValueMapObjectType*>(this->MakeOutput(1).GetPointer());
46  this->itk::ProcessObject::SetNthOutput(1, output.GetPointer());
47 
48  this->Reset();
49 }
50 
51 template <class TInputVectorImage, class TLabelImage>
52 typename itk::DataObject::Pointer
54 {
55  return static_cast<itk::DataObject*>(PixelValueMapObjectType::New().GetPointer());
56 }
57 
58 template <class TInputVectorImage, class TLabelImage>
60 {
61  // Process object is not const-correct so the const_cast is required here
62  this->itk::ProcessObject::SetNthInput(1, const_cast<LabelImageType*>(input));
63 }
64 
65 template <class TInputVectorImage, class TLabelImage>
68 {
69  return static_cast<const TLabelImage*>(this->itk::ProcessObject::GetInput(1));
70 }
71 
72 template <class TInputVectorImage, class TLabelImage>
75 {
76  return m_MeanRadiometricValue;
77 }
78 
79 template <class TInputVectorImage, class TLabelImage>
82 {
83  return m_StDevRadiometricValue;
84 }
85 
86 template <class TInputVectorImage, class TLabelImage>
89 {
90  return m_MinRadiometricValue;
91 }
92 
93 template <class TInputVectorImage, class TLabelImage>
96 {
97  return m_MaxRadiometricValue;
98 }
99 
100 template <class TInputVectorImage, class TLabelImage>
103 {
104  return m_LabelPopulation;
105 }
106 
107 template <class TInputVectorImage, class TLabelImage>
109 {
110  Superclass::GenerateOutputInformation();
111 
112  if (this->GetInput())
113  {
114  this->GetOutput()->CopyInformation(this->GetInput());
115  this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
116 
117  if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
118  {
119  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
120  }
121  }
122 }
123 
124 template <class TInputVectorImage, class TLabelImage>
126 {
127  // This is commented to prevent the streaming of the whole image for the first stream strip
128  // It shall not cause any problem because the output image of this filter is not intended to be used.
129  // InputImagePointer image = const_cast< TInputImage * >( this->GetInput() );
130  // this->GraftOutput( image );
131  // Nothing that needs to be allocated for the remaining outputs
132 }
133 
134 template <class TInputVectorImage, class TLabelImage>
136 {
137  // Update temporary accumulator
138  AccumulatorMapType outputAcc;
139  auto endAcc = outputAcc.end();
140 
141  for (auto const& threadAccMap : m_AccumulatorMaps)
142  {
143  for (auto const& it : threadAccMap)
144  {
145  auto label = it.first;
146  auto itAcc = outputAcc.find(label);
147  if (itAcc == endAcc)
148  {
149  outputAcc.emplace(label, it.second);
150  }
151  else
152  {
153  itAcc->second.Update(it.second);
154  }
155  }
156  }
157 
158  // Publish output maps
159  for (auto& it : outputAcc)
160  {
161  const LabelPixelType label = it.first;
162  const auto& bandCount = it.second.GetBandCount();
163  const auto& sum = it.second.GetSum();
164  const auto& sqSum = it.second.GetSqSum();
165 
166  // Count
167  m_LabelPopulation[label] = it.second.GetCount();
168 
169  // Mean & stdev
171  RealVectorPixelType std(sqSum);
172  RealVectorPixelType min(it.second.GetMin());
173  RealVectorPixelType max(it.second.GetMax());
174  for (unsigned int band = 0; band < mean.GetSize(); band++)
175  {
176  // Number of valid pixels in band
177  auto count = bandCount[band];
178  // Mean
179  mean[band] /= count;
180 
181  // Unbiased standard deviation (not sure unbiased is useful here)
182  const double variance = (sqSum[band] - (sum[band] * mean[band])) / (count - 1);
183  std[band] = std::sqrt(variance);
184 
185  // Use the no data value when no valid pixels were found
186  if (this->GetUseNoDataValue() && count == 0)
187  {
188  min[band] = this->GetNoDataValue();
189  max[band] = this->GetNoDataValue();
190  }
191  }
192  m_MeanRadiometricValue.emplace(label, std::move(mean));
193  m_StDevRadiometricValue.emplace(label, std::move(std));
194 
195  // Min & max
196  m_MinRadiometricValue.emplace(label, std::move(min));
197  m_MaxRadiometricValue.emplace(label, std::move(max));
198  }
199 }
200 
201 template <class TInputVectorImage, class TLabelImage>
203 {
204  m_AccumulatorMaps.clear();
205 
206  m_MeanRadiometricValue.clear();
207  m_StDevRadiometricValue.clear();
208  m_MinRadiometricValue.clear();
209  m_MaxRadiometricValue.clear();
210  m_LabelPopulation.clear();
211  m_AccumulatorMaps.resize(this->GetNumberOfThreads());
212 }
213 
214 template <class TInputVectorImage, class TLabelImage>
216 {
217  // The Requested Regions of all the inputs are set to their Largest Possible Regions
218  this->itk::ProcessObject::GenerateInputRequestedRegion();
219 
220  // Iteration over all the inputs of the current filter (this)
221  for (itk::InputDataObjectIterator it(this); !it.IsAtEnd(); it++)
222  {
223  // Check whether the input is an image of the appropriate dimension
224  // dynamic_cast of all the input images as itk::ImageBase objects
225  // in order to pass the if ( input ) test whatever the inputImageType (vectorImage or labelImage)
226  ImageBaseType* input = dynamic_cast<ImageBaseType*>(it.GetInput());
227 
228  if (input)
229  {
230  // Use the function object RegionCopier to copy the output region
231  // to the input. The default region copier has default implementations
232  // to handle the cases where the input and output are the same
233  // dimension, the input a higher dimension than the output, and the
234  // input a lower dimension than the output.
235  InputImageRegionType inputRegion;
236  this->CallCopyOutputRegionToInputRegion(inputRegion, this->GetOutput()->GetRequestedRegion());
237  input->SetRequestedRegion(inputRegion);
238  }
239  }
240 }
241 
242 template <class TInputVectorImage, class TLabelImage>
244  itk::ThreadIdType threadId)
245 {
249  InputVectorImagePointer inputPtr = const_cast<TInputVectorImage*>(this->GetInput());
250  LabelImagePointer labelInputPtr = const_cast<TLabelImage*>(this->GetInputLabelImage());
252 
253  itk::ImageRegionConstIterator<TInputVectorImage> inIt(inputPtr, outputRegionForThread);
254  itk::ImageRegionConstIterator<TLabelImage> labelIt(labelInputPtr, outputRegionForThread);
255  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
256 
257  auto& acc = m_AccumulatorMaps[threadId];
258  auto endAcc = acc.end();
259 
260  // do the work
261  for (inIt.GoToBegin(), labelIt.GoToBegin(); !inIt.IsAtEnd() && !labelIt.IsAtEnd(); ++inIt, ++labelIt)
262  {
263  const auto& value = inIt.Get();
264  auto label = labelIt.Get();
265 
266  // Update the accumulator
267  auto itAcc = acc.find(label);
268  if (itAcc == endAcc)
269  {
270  acc.emplace(label, AccumulatorType(this->GetNoDataValue(), this->GetUseNoDataValue(), value));
271  }
272  else
273  {
274  itAcc->second.Update(value);
275  }
276 
277  progress.CompletedPixel();
278  }
279 }
280 
281 template <class TInputVectorImage, class TLabelImage>
283 {
284  Superclass::PrintSelf(os, indent);
285 }
286 
287 } // end namespace otb
288 #endif
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::PixelValueMapType
std::unordered_map< LabelPixelType, RealVectorPixelType > PixelValueMapType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:206
otb::mean
Definition: otbParserXPlugins.h:261
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:221
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::LabelImageType
TLabelImage LabelImageType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:195
otbStreamingStatisticsMapFromLabelImageFilter.h
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::ImageBaseType
itk::ImageBase< InputImageDimension > ImageBaseType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:223
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::RegionType
VectorImageType::RegionType RegionType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:198
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GetMeanValueMap
PixelValueMapType GetMeanValueMap() const
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:74
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::Synthetize
void Synthetize(void) override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:135
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::InputImageRegionType
ImageBaseType::RegionType InputImageRegionType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:224
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GetInputLabelImage
virtual const LabelImageType * GetInputLabelImage()
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:67
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:215
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::StatisticsAccumulator
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:53
otbMacro.h
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::PixelValueMapObjectType
itk::SimpleDataObjectDecorator< PixelValueMapType > PixelValueMapObjectType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:227
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::RealVectorPixelType
itk::VariableLengthVector< double > RealVectorPixelType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:202
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::LabelPopulationMapType
std::unordered_map< LabelPixelType, double > LabelPopulationMapType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:207
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::PersistentStreamingStatisticsMapFromLabelImageFilter
PersistentStreamingStatisticsMapFromLabelImageFilter()
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:37
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::SetInputLabelImage
virtual void SetInputLabelImage(const LabelImageType *image)
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:59
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GetMaxValueMap
PixelValueMapType GetMaxValueMap() const
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:95
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::AccumulatorMapType
std::unordered_map< LabelPixelType, AccumulatorType > AccumulatorMapType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:204
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GetLabelPopulationMap
LabelPopulationMapType GetLabelPopulationMap() const
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:102
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:108
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::LabelPixelType
LabelImageType::PixelType LabelPixelType
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:201
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::Reset
void Reset(void) override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:202
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::InputVectorImagePointer
TInputVectorImage::Pointer InputVectorImagePointer
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:194
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::MakeOutput
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:53
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:243
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::AllocateOutputs
void AllocateOutputs() override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:125
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::LabelImagePointer
TLabelImage::Pointer LabelImagePointer
Definition: otbStreamingStatisticsMapFromLabelImageFilter.h:196
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GetMinValueMap
PixelValueMapType GetMinValueMap() const
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:88
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::GetStandardDeviationValueMap
PixelValueMapType GetStandardDeviationValueMap() const
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:81
otb::PersistentStreamingStatisticsMapFromLabelImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbStreamingStatisticsMapFromLabelImageFilter.hxx:282