OTB  10.0.0
Orfeo Toolbox
otbStreamingStatisticsMapFromLabelImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2024 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_h
23 #define otbStreamingStatisticsMapFromLabelImageFilter_h
24 
26 #include "itkNumericTraits.h"
27 #include "itkArray.h"
28 #include "itkSimpleDataObjectDecorator.h"
30 #include <unordered_map>
31 
32 namespace otb
33 {
34 
52 template <class TRealVectorPixelType>
54 {
55 public:
56  using RealValueType = typename TRealVectorPixelType::ValueType;
57  using PixelCountType = std::uint64_t;
58  using PixelCountVectorType = itk::VariableLengthVector<PixelCountType>;
59 
60  // Constructor (default)
62  {
63  }
64 
65  // Constructor (initialize the accumulator with the given pixel)
66  StatisticsAccumulator(RealValueType noDataValue, bool useNoDataValue, const TRealVectorPixelType& pixel)
67  : m_NoDataValue(noDataValue), m_Count(1), m_UseNoDataValue(useNoDataValue)
68  {
69  m_Count = 1;
70  m_BandCount.SetSize(pixel.GetSize());
71  m_Sum.SetSize(pixel.GetSize());
72  m_Min.SetSize(pixel.GetSize());
73  m_Max.SetSize(pixel.GetSize());
74  m_SqSum.SetSize(pixel.GetSize());
75  for (unsigned int band = 0; band < pixel.GetSize(); band++)
76  {
77  auto val = pixel[band];
78  if (!m_UseNoDataValue || val != m_NoDataValue)
79  {
80  m_BandCount[band] = 1;
81  m_Sum[band] = val;
82  m_Min[band] = val;
83  m_Max[band] = val;
84  m_SqSum[band] = val * val;
85  }
86  else
87  {
88  m_BandCount[band] = 0;
89  m_Sum[band] = itk::NumericTraits<RealValueType>::ZeroValue();
90  m_Min[band] = itk::NumericTraits<RealValueType>::max();
91  m_Max[band] = itk::NumericTraits<RealValueType>::min();
92  m_SqSum[band] = itk::NumericTraits<RealValueType>::ZeroValue();
93  }
94  }
95  }
96 
97  // Function update (pixel)
98  void Update(const TRealVectorPixelType& pixel)
99  {
100  m_Count++;
101  const unsigned int nBands = pixel.GetSize();
102  for (unsigned int band = 0; band < nBands; band++)
103  {
104  const RealValueType value = pixel[band];
105  const RealValueType sqValue = value * value;
106 
107  if (!m_UseNoDataValue || value != m_NoDataValue)
108  {
109  UpdateValues(1, value, sqValue, value, value, m_BandCount[band], m_Sum[band], m_SqSum[band], m_Min[band], m_Max[band]);
110  }
111  }
112  }
113 
114  // Function update (self)
115  void Update(const StatisticsAccumulator& other)
116  {
117  m_Count += other.m_Count;
118  const unsigned int nBands = other.m_Sum.GetSize();
119  for (unsigned int band = 0; band < nBands; band++)
120  {
121  UpdateValues(other.m_BandCount[band], other.m_Sum[band], other.m_SqSum[band], other.m_Min[band], other.m_Max[band], m_BandCount[band], m_Sum[band],
122  m_SqSum[band], m_Min[band], m_Max[band]);
123  }
124  }
125 
126  // Accessors
127  itkGetMacro(BandCount, PixelCountVectorType);
128  itkGetMacro(Sum, TRealVectorPixelType);
129  itkGetMacro(SqSum, TRealVectorPixelType);
130  itkGetMacro(Min, TRealVectorPixelType);
131  itkGetMacro(Max, TRealVectorPixelType);
132  itkGetMacro(Count, double);
133 
134 private:
135  void UpdateValues(PixelCountType otherCount, RealValueType otherSum, RealValueType otherSqSum, RealValueType otherMin, RealValueType otherMax,
136  PixelCountType& count, RealValueType& sum, RealValueType& sqSum, RealValueType& min, RealValueType& max)
137  {
138  count += otherCount;
139  sum += otherSum;
140  sqSum += otherSqSum;
141  if (otherMin < min)
142  min = otherMin;
143  if (otherMax > max)
144  max = otherMax;
145  }
146 
147 protected:
149  TRealVectorPixelType m_Sum;
150  TRealVectorPixelType m_SqSum;
151  TRealVectorPixelType m_Min;
152  TRealVectorPixelType m_Max;
156 };
157 
176 template <class TInputVectorImage, class TLabelImage>
177 class ITK_EXPORT PersistentStreamingStatisticsMapFromLabelImageFilter : public PersistentImageFilter<TInputVectorImage, TInputVectorImage>
178 {
179 public:
183  typedef itk::SmartPointer<Self> Pointer;
184  typedef itk::SmartPointer<const Self> ConstPointer;
185 
187  itkNewMacro(Self);
188 
191 
193  typedef TInputVectorImage VectorImageType;
194  typedef typename TInputVectorImage::Pointer InputVectorImagePointer;
195  typedef TLabelImage LabelImageType;
196  typedef typename TLabelImage::Pointer LabelImagePointer;
197 
198  typedef typename VectorImageType::RegionType RegionType;
199  typedef typename VectorImageType::PixelType VectorPixelType;
200  typedef typename VectorImageType::PixelType::ValueType VectorPixelValueType;
201  typedef typename LabelImageType::PixelType LabelPixelType;
202  typedef itk::VariableLengthVector<double> RealVectorPixelType;
204  typedef std::unordered_map<LabelPixelType, AccumulatorType> AccumulatorMapType;
205  typedef std::vector<AccumulatorMapType> AccumulatorMapCollectionType;
206  typedef std::unordered_map<LabelPixelType, RealVectorPixelType> PixelValueMapType;
207  typedef std::unordered_map<LabelPixelType, double> LabelPopulationMapType;
208 
209  itkStaticConstMacro(InputImageDimension, unsigned int, TInputVectorImage::ImageDimension);
210 
212  itkStaticConstMacro(ImageDimension, unsigned int, TInputVectorImage::ImageDimension);
213 
216  itkGetMacro(UseNoDataValue, bool);
217  itkSetMacro(UseNoDataValue, bool);
218 
220  typedef typename itk::DataObject::Pointer DataObjectPointer;
221  typedef itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType;
222 
223  typedef itk::ImageBase<InputImageDimension> ImageBaseType;
224  typedef typename ImageBaseType::RegionType InputImageRegionType;
225 
227  typedef itk::SimpleDataObjectDecorator<PixelValueMapType> PixelValueMapObjectType;
228 
230  virtual void SetInputLabelImage(const LabelImageType* image);
231 
233  virtual const LabelImageType* GetInputLabelImage();
234 
236  PixelValueMapType GetMeanValueMap() const;
237 
239  PixelValueMapType GetStandardDeviationValueMap() const;
240 
242  PixelValueMapType GetMinValueMap() const;
243 
245  PixelValueMapType GetMaxValueMap() const;
246 
248  LabelPopulationMapType GetLabelPopulationMap() const;
249 
252  DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override;
253  using Superclass::MakeOutput;
254 
258  void AllocateOutputs() override;
259 
260  void GenerateOutputInformation() override;
261 
262  void Synthetize(void) override;
263 
264  void Reset(void) override;
265 
269  void GenerateInputRequestedRegion() override;
270 
271 protected:
274  {
275  }
276  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
277 
278  void ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId) override;
279 
280 private:
282  void operator=(const Self&) = delete;
283 
286 
288 
293 
295 
296 }; // end of class PersistentStreamingStatisticsMapFromLabelImageFilter
297 
298 
299 /*===========================================================================*/
300 
343 template <class TInputVectorImage, class TLabelImage>
345  : public PersistentFilterStreamingDecorator<PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage>>
346 {
347 public:
351  typedef itk::SmartPointer<Self> Pointer;
352  typedef itk::SmartPointer<const Self> ConstPointer;
353 
355  itkNewMacro(Self);
356 
359 
360  typedef TInputVectorImage VectorImageType;
361  typedef TLabelImage LabelImageType;
362 
363  typedef typename VectorImageType::PixelType VectorPixelType;
364  typedef typename VectorImageType::PixelType::ValueType VectorPixelValueType;
365 
366  typedef typename Superclass::FilterType::PixelValueMapType PixelValueMapType;
367  typedef typename Superclass::FilterType::PixelValueMapObjectType PixelValueMapObjectType;
368 
369  typedef typename Superclass::FilterType::LabelPopulationMapType LabelPopulationMapType;
370 
372  using Superclass::SetInput;
373  void SetInput(const VectorImageType* input)
374  {
375  this->GetFilter()->SetInput(input);
376  }
378 
381  {
382  return this->GetFilter()->GetInput();
383  }
384 
387  {
388  this->GetFilter()->SetInputLabelImage(input);
389  }
390 
393  {
394  return this->GetFilter()->GetInputLabelImage();
395  }
396 
399  {
400  return this->GetFilter()->GetMeanValueMap();
401  }
402 
405  {
406  return this->GetFilter()->GetStandardDeviationValueMap();
407  }
408 
411  {
412  return this->GetFilter()->GetMinValueMap();
413  }
414 
417  {
418  return this->GetFilter()->GetMaxValueMap();
419  }
420 
423  {
424  return this->GetFilter()->GetLabelPopulationMap();
425  }
426 
429  {
430  this->GetFilter()->SetNoDataValue(value);
431  }
432 
435  {
436  return this->GetFilter()->GetNoDataValue();
437  }
438 
440  void SetUseNoDataValue(bool useNoDataValue)
441  {
442  this->GetFilter()->SetUseNoDataValue(useNoDataValue);
443  }
444 
446  bool GetUseNoDataValue() const
447  {
448  return this->GetFilter()->GetUseNoDataValue();
449  }
450 
451 protected:
454  {
455  }
456 
459  {
460  }
461 
462 private:
464  void operator=(const Self&) = delete;
465 };
466 
467 } // end namespace otb
468 
469 #ifndef OTB_MANUAL_INSTANTIATION
471 #endif
472 
473 #endif
Compute the density of a neighborhood centerred in a pixel.
This filter link a persistent filter with a StreamingImageVirtualWriter.
This filter is the base class for all filter persisting data through multiple update....
Computes mean radiometric value for each label of a label image, based on a support VectorImage.
std::unordered_map< LabelPixelType, RealVectorPixelType > PixelValueMapType
PersistentImageFilter< TInputVectorImage, TInputVectorImage > Superclass
StatisticsAccumulator(RealValueType noDataValue, bool useNoDataValue, const TRealVectorPixelType &pixel)
itk::VariableLengthVector< PixelCountType > PixelCountVectorType
void Update(const TRealVectorPixelType &pixel)
void Update(const StatisticsAccumulator &other)
typename TRealVectorPixelType::ValueType RealValueType
void UpdateValues(PixelCountType otherCount, RealValueType otherSum, RealValueType otherSqSum, RealValueType otherMin, RealValueType otherMax, PixelCountType &count, RealValueType &sum, RealValueType &sqSum, RealValueType &min, RealValueType &max)
Computes mean radiometric value for each label of a label image, based on a support VectorImage.
PersistentFilterStreamingDecorator< PersistentStreamingStatisticsMapFromLabelImageFilter< TInputVectorImage, TLabelImage > > Superclass
Superclass::FilterType::PixelValueMapObjectType PixelValueMapObjectType
StreamingStatisticsMapFromLabelImageFilter(const Self &)=delete
OTBMetadata_EXPORT char const * NoDataValue
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.