OTB  6.7.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-2019 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"
30 #include <unordered_map>
31 
32 namespace otb
33 {
34 
52 template<class TRealVectorPixelType>
54 {
55 public:
56 
57  typedef typename TRealVectorPixelType::ValueType RealValueType;
60 
61  // Constructor (default)
63 
64  // Constructor (initialize the accumulator with the given pixel)
66  bool useNoDataValue,
67  const TRealVectorPixelType & pixel)
68  : m_NoDataValue(noDataValue),
69  m_Count(1),
70  m_UseNoDataValue(useNoDataValue)
71  {
72  m_Count = 1;
73  m_Sum = pixel;
74  m_Min = pixel;
75  m_Max = pixel;
76  m_SqSum = pixel;
77  m_BandCount.SetSize(pixel.GetSize());
78  for (unsigned int band = 0 ; band < m_SqSum.GetSize() ; band++)
79  {
80  auto val = pixel[band];
81  if (!m_UseNoDataValue || val != m_NoDataValue)
82  {
83  m_BandCount[band] = 1;
84  m_SqSum[band] *= m_SqSum[band];
85  }
86  else
87  {
88  m_BandCount[band] = 0;
89  }
90  }
91  }
92 
93  // Function update (pixel)
94  void Update(const TRealVectorPixelType & pixel)
95  {
96  m_Count++;
97  const unsigned int nBands = pixel.GetSize();
98  for (unsigned int band = 0 ; band < nBands ; band ++ )
99  {
100  const RealValueType value = pixel[band];
101  const RealValueType sqValue = value * value;
102 
104  value, sqValue,
105  value, value,
106  m_BandCount[band],
107  m_Sum[band], m_SqSum[band],
108  m_Min[band], m_Max[band]);
109  }
110  }
111 
112  // Function update (self)
113  void Update(const StatisticsAccumulator & other)
114  {
115  m_Count += other.m_Count;
116  const unsigned int nBands = other.m_Sum.GetSize();
117  for (unsigned int band = 0 ; band < nBands ; band ++ )
118  {
119  UpdateValues(other.m_BandCount[band],
120  other.m_Sum[band], other.m_SqSum[band],
121  other.m_Min[band], other.m_Max[band],
122  m_BandCount[band],
123  m_Sum[band], m_SqSum[band],
124  m_Min[band], m_Max[band]);
125  }
126  }
127 
128  // Accessors
129  itkGetMacro(BandCount, PixelCountVectorType);
130  itkGetMacro(Sum, TRealVectorPixelType);
131  itkGetMacro(SqSum, TRealVectorPixelType);
132  itkGetMacro(Min, TRealVectorPixelType);
133  itkGetMacro(Max, TRealVectorPixelType);
134  itkGetMacro(Count, double);
135 
136 private:
137  void UpdateValues(PixelCountType otherCount,
138  RealValueType otherSum, RealValueType otherSqSum,
139  RealValueType otherMin, RealValueType otherMax,
140  PixelCountType & count,
141  RealValueType & sum, RealValueType & sqSum,
142  RealValueType & min, RealValueType & max)
143  {
144  count += otherCount;
145  sum += otherSum;
146  sqSum += otherSqSum;
147  if (otherMin < min)
148  min = otherMin;
149  if (otherMax > max)
150  max = otherMax;
151  }
152 
153 protected:
155  TRealVectorPixelType m_Sum;
156  TRealVectorPixelType m_SqSum;
157  TRealVectorPixelType m_Min;
158  TRealVectorPixelType m_Max;
162 };
163 
182 template<class TInputVectorImage, class TLabelImage>
184 public PersistentImageFilter<TInputVectorImage, TInputVectorImage>
185 {
186 public:
192 
194  itkNewMacro(Self);
195 
198 
200  typedef TInputVectorImage VectorImageType;
201  typedef typename TInputVectorImage::Pointer InputVectorImagePointer;
202  typedef TLabelImage LabelImageType;
203  typedef typename TLabelImage::Pointer LabelImagePointer;
204 
205  typedef typename VectorImageType::RegionType RegionType;
206  typedef typename VectorImageType::PixelType VectorPixelType;
207  typedef typename VectorImageType::PixelType::ValueType VectorPixelValueType;
208  typedef typename LabelImageType::PixelType LabelPixelType;
211  typedef std::unordered_map<LabelPixelType, AccumulatorType > AccumulatorMapType;
212  typedef std::vector<AccumulatorMapType> AccumulatorMapCollectionType;
213  typedef std::unordered_map<LabelPixelType, RealVectorPixelType > PixelValueMapType;
214  typedef std::unordered_map<LabelPixelType, double> LabelPopulationMapType;
215 
216  itkStaticConstMacro(InputImageDimension, unsigned int,
217  TInputVectorImage::ImageDimension);
218 
220  itkStaticConstMacro(ImageDimension, unsigned int,
221  TInputVectorImage::ImageDimension);
222 
223  itkGetMacro(NoDataValue, VectorPixelValueType);
224  itkSetMacro(NoDataValue, VectorPixelValueType);
225  itkGetMacro(UseNoDataValue, bool);
226  itkSetMacro(UseNoDataValue, bool);
227 
231 
234 
237 
239  virtual void SetInputLabelImage( const LabelImageType *image);
240 
242  virtual const LabelImageType * GetInputLabelImage();
243 
245  PixelValueMapType GetMeanValueMap() const;
246 
248  PixelValueMapType GetStandardDeviationValueMap() const;
249 
251  PixelValueMapType GetMinValueMap() const;
252 
254  PixelValueMapType GetMaxValueMap() const;
255 
257  LabelPopulationMapType GetLabelPopulationMap() const;
258 
261  DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override;
262  using Superclass::MakeOutput;
263 
267  void AllocateOutputs() override;
268 
269  void GenerateOutputInformation() override;
270 
271  void Synthetize(void) override;
272 
273  void Reset(void) override;
274 
278  void GenerateInputRequestedRegion() override;
279 
280 protected:
283  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
284 
285  void ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId ) override;
286 
287 private:
289  void operator =(const Self&) = delete;
290 
293 
295 
300 
302 
303 }; // end of class PersistentStreamingStatisticsMapFromLabelImageFilter
304 
305 
306 /*===========================================================================*/
307 
350 template<class TInputVectorImage, class TLabelImage>
352 public PersistentFilterStreamingDecorator<PersistentStreamingStatisticsMapFromLabelImageFilter<TInputVectorImage, TLabelImage> >
353 {
354 public:
361 
363  itkNewMacro(Self);
364 
367 
368  typedef TInputVectorImage VectorImageType;
369  typedef TLabelImage LabelImageType;
370 
371  typedef typename VectorImageType::PixelType VectorPixelType;
372  typedef typename VectorImageType::PixelType::ValueType VectorPixelValueType;
373 
374  typedef typename Superclass::FilterType::PixelValueMapType PixelValueMapType;
375  typedef typename Superclass::FilterType::PixelValueMapObjectType PixelValueMapObjectType;
376 
377  typedef typename Superclass::FilterType::LabelPopulationMapType LabelPopulationMapType;
378 
380  using Superclass::SetInput;
381  void SetInput(const VectorImageType * input)
382  {
383  this->GetFilter()->SetInput(input);
384  }
386 
389  {
390  return this->GetFilter()->GetInput();
391  }
392 
394  void SetInputLabelImage(const LabelImageType * input)
395  {
396  this->GetFilter()->SetInputLabelImage(input);
397  }
398 
401  {
402  return this->GetFilter()->GetInputLabelImage();
403  }
404 
407  {
408  return this->GetFilter()->GetMeanValueMap();
409  }
410 
413  {
414  return this->GetFilter()->GetStandardDeviationValueMap();
415  }
416 
419  {
420  return this->GetFilter()->GetMinValueMap();
421  }
422 
425  {
426  return this->GetFilter()->GetMaxValueMap();
427  }
428 
431  {
432  return this->GetFilter()->GetLabelPopulationMap();
433  }
434 
437  {
438  this->GetFilter()->SetNoDataValue(value);
439  }
440 
443  {
444  return this->GetFilter()->GetNoDataValue();
445  }
446 
448  void SetUseNoDataValue(bool useNoDataValue)
449  {
450  this->GetFilter()->SetUseNoDataValue(useNoDataValue);
451  }
452 
454  bool GetUseNoDataValue() const
455  {
456  return this->GetFilter()->GetUseNoDataValue();
457  }
458 
459 protected:
462 
465 
466 private:
467  StreamingStatisticsMapFromLabelImageFilter(const Self &) = delete;
468  void operator =(const Self&) = delete;
469 };
470 
471 } // end namespace otb
472 
473 #ifndef OTB_MANUAL_INSTANTIATION
475 #endif
476 
477 #endif
Computes mean radiometric value for each label of a label image, based on a support VectorImage...
Superclass::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
StatisticsAccumulator(RealValueType noDataValue, bool useNoDataValue, const TRealVectorPixelType &pixel)
OTBOSSIMAdapters_EXPORT char const * NoDataValue
Compute the density of a neighboorhood centerred in a pixel.
void Update(const StatisticsAccumulator &other)
void Update(const TRealVectorPixelType &pixel)
DataObjectPointerArray::size_type DataObjectPointerArraySizeType
Computes mean radiometric value for each label of a label image, based on a support VectorImage...
KWIML_INT_uint64_t uint64_t
unsigned int ThreadIdType
PersistentImageFilter< TInputVectorImage, TInputVectorImage > Superclass
PersistentFilterStreamingDecorator< PersistentStreamingStatisticsMapFromLabelImageFilter< TInputVectorImage, TLabelImage > > Superclass
void UpdateValues(PixelCountType otherCount, RealValueType otherSum, RealValueType otherSqSum, RealValueType otherMin, RealValueType otherMax, PixelCountType &count, RealValueType &sum, RealValueType &sqSum, RealValueType &min, RealValueType &max)
itk::VariableLengthVector< PixelCountType > PixelCountVectorType
std::unordered_map< LabelPixelType, RealVectorPixelType > PixelValueMapType
void SetSize(unsigned int sz, TReallocatePolicy reallocatePolicy, TKeepValuesPolicy keepValues)
Superclass::FilterType::PixelValueMapObjectType PixelValueMapObjectType
This filter is the base class for all filter persisting data through multiple update. For instance, a filter computing global statistics on an image with streaming capabilities will have to keep the temporary results for each streamed piece of the image in order to synthesize the global statistics at the end. This filter is an itk::ImageToImageFilter, providing two additional methods. The first one, Synthetize(), allows the user to synthesize temporary data produced by the multiple updates on different pieces of the image to the global result. The second one, Reset(), allows the user to reset the temporary data for a new input image for instance.
This filter link a persistent filter with a StreamingImageVirtualWriter.