ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkLabelStatisticsImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkLabelStatisticsImageFilter_h
19 #define itkLabelStatisticsImageFilter_h
20 
21 #include "itkImageToImageFilter.h"
22 #include "itkNumericTraits.h"
24 #include "itkHistogram.h"
25 #include <mutex>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace itk
30 {
58 template< typename TInputImage, typename TLabelImage >
59 class ITK_TEMPLATE_EXPORT LabelStatisticsImageFilter:
60  public ImageToImageFilter< TInputImage, TInputImage >
61 {
62 public:
63  ITK_DISALLOW_COPY_AND_ASSIGN(LabelStatisticsImageFilter);
64 
70 
72  itkNewMacro(Self);
73 
76 
78  using InputImagePointer = typename TInputImage::Pointer;
80  using SizeType = typename TInputImage::SizeType;
82  using PixelType = typename TInputImage::PixelType;
83 
85  using LabelImageType = TLabelImage;
86  using LabelImagePointer = typename TLabelImage::Pointer;
90  using LabelPixelType = typename TLabelImage::PixelType;
91 
93  static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
94 
97 
100 
103 
105  using BoundingBoxType = std::vector< IndexValueType >;
106 
110 
116  {
117 public:
118 
119  // default constructor
121  {
122  // initialized to the default values
125  m_SumOfSquares = NumericTraits< RealType >::ZeroValue();
126 
127  // Set such that the first pixel encountered can be compared
128  m_Minimum = NumericTraits< RealType >::max();
130 
131  // Default these to zero
135 
136  const unsigned int imageDimension = Self::ImageDimension;
137  m_BoundingBox.resize(imageDimension * 2);
138  for ( unsigned int i = 0; i < imageDimension * 2; i += 2 )
139  {
140  m_BoundingBox[i] = NumericTraits< IndexValueType >::max();
141  m_BoundingBox[i + 1] = NumericTraits< IndexValueType >::NonpositiveMin();
142  }
143  m_Histogram = nullptr;
144  }
145 
146  // constructor with histogram enabled
147  LabelStatistics(int size, RealType lowerBound, RealType upperBound)
148  {
149  // initialized to the default values
152  m_SumOfSquares = NumericTraits< RealType >::ZeroValue();
153 
154  // Set such that the first pixel encountered can be compared
155  m_Minimum = NumericTraits< RealType >::max();
157 
158  // Default these to zero
162 
163  const unsigned int imageDimension = Self::ImageDimension;
164  m_BoundingBox.resize(imageDimension * 2);
165  for ( unsigned int i = 0; i < imageDimension * 2; i += 2 )
166  {
167  m_BoundingBox[i] = NumericTraits< IndexValueType >::max();
168  m_BoundingBox[i + 1] = NumericTraits< IndexValueType >::NonpositiveMin();
169  }
170 
171  // Histogram
172  m_Histogram = HistogramType::New();
173  typename HistogramType::SizeType hsize;
176  hsize.SetSize(1);
177  lb.SetSize(1);
178  ub.SetSize(1);
179  m_Histogram->SetMeasurementVectorSize(1);
180  hsize[0] = size;
181  lb[0] = lowerBound;
182  ub[0] = upperBound;
183  m_Histogram->Initialize(hsize, lb, ub);
184  }
185 
186  // need copy constructor because of smart pointer to histogram
188  {
189  m_Count = l.m_Count;
190  m_Minimum = l.m_Minimum;
191  m_Maximum = l.m_Maximum;
192  m_Mean = l.m_Mean;
193  m_Sum = l.m_Sum;
194  m_SumOfSquares = l.m_SumOfSquares;
195  m_Sigma = l.m_Sigma;
196  m_Variance = l.m_Variance;
197  m_BoundingBox = l.m_BoundingBox;
198  m_Histogram = l.m_Histogram;
199  }
200 
201  LabelStatistics( LabelStatistics && ) = default;
202 
203  // added for completeness
204  LabelStatistics &operator= (const LabelStatistics& l)
205  {
206  if(this != &l)
207  {
208  m_Count = l.m_Count;
209  m_Minimum = l.m_Minimum;
210  m_Maximum = l.m_Maximum;
211  m_Mean = l.m_Mean;
212  m_Sum = l.m_Sum;
213  m_SumOfSquares = l.m_SumOfSquares;
214  m_Sigma = l.m_Sigma;
215  m_Variance = l.m_Variance;
216  m_BoundingBox = l.m_BoundingBox;
217  m_Histogram = l.m_Histogram;
218  }
219  return *this;
220  }
221 
232  };
233 
235  using MapType = std::unordered_map< LabelPixelType, LabelStatistics >;
236  using MapIterator = typename MapType::iterator;
237  using MapConstIterator = typename MapType::const_iterator;
239 
241  using ValidLabelValuesContainerType = std::vector<LabelPixelType>;
242 
243  // macros for Histogram enables
244  itkSetMacro(UseHistograms, bool);
245  itkGetConstMacro(UseHistograms, bool);
246  itkBooleanMacro(UseHistograms);
247 
248 
250  {
251  return m_ValidLabelValues;
252  }
253 
255  void SetLabelInput(const TLabelImage *input)
256  {
257  // Process object is not const-correct so the const casting is required.
258  this->SetNthInput( 1, const_cast< TLabelImage * >( input ) );
259  }
260 
263  {
264  return itkDynamicCastInDebugMode< LabelImageType * >( const_cast< DataObject * >( this->ProcessObject::GetInput(1) ) );
265  }
266 
269  bool HasLabel(LabelPixelType label) const
270  {
271  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
272  }
273 
276  {
277  return static_cast<MapSizeType>( m_LabelStatistics.size() );
278  }
279 
281  {
282  return static_cast<MapSizeType>( this->GetNumberOfObjects() );
283  }
284 
286  RealType GetMinimum(LabelPixelType label) const;
287 
289  RealType GetMaximum(LabelPixelType label) const;
290 
292  RealType GetMean(LabelPixelType label) const;
293 
296  RealType GetMedian(LabelPixelType label) const;
297 
299  RealType GetSigma(LabelPixelType label) const;
300 
302  RealType GetVariance(LabelPixelType label) const;
303 
307  BoundingBoxType GetBoundingBox(LabelPixelType label) const;
308 
310  RegionType GetRegion(LabelPixelType label) const;
311 
313  RealType GetSum(LabelPixelType label) const;
314 
316  MapSizeType GetCount(LabelPixelType label) const;
317 
319  HistogramPointer GetHistogram(LabelPixelType label) const;
320 
322  void SetHistogramParameters(const int numBins, RealType lowerBound,
323  RealType upperBound);
324 
325 #ifdef ITK_USE_CONCEPT_CHECKING
326  // Begin concept checking
327  itkConceptMacro( InputHasNumericTraitsCheck,
329  // End concept checking
330 #endif
331 
332 protected:
334  ~LabelStatisticsImageFilter() override = default;
335  void PrintSelf(std::ostream & os, Indent indent) const override;
336 
339  void AllocateOutputs() override;
340 
343  void AfterThreadedGenerateData() override;
344 
345  void DynamicThreadedGenerateData( const RegionType & ) override;
346 
347  // Override since the filter produces all of its output
348  void EnlargeOutputRequestedRegion(DataObject *data) override;
349 
350 private:
351 
352  void MergeMap( MapType &, MapType &) const;
353 
356 
358 
360 
363 
364  std::mutex m_Mutex;
365 
366 }; // end of class
367 } // end namespace itk
368 
369 #ifndef ITK_MANUAL_INSTANTIATION
370 #include "itkLabelStatisticsImageFilter.hxx"
371 #endif
372 
373 #endif
bool HasLabel(LabelPixelType label) const
void SetLabelInput(const TLabelImage *input)
Light weight base class for most itk classes.
virtual const ValidLabelValuesContainerType & GetValidLabelValues() const
Define numeric traits for std::vector.
typename TInputImage::PixelType PixelType
This class stores measurement vectors in the context of n-dimensional histogram.
Definition: itkHistogram.h:77
typename NumericTraits< PixelType >::RealType RealType
Given an intensity image and a label map, compute min, max, variance and mean of the pixels associate...
typename TInputImage::RegionType RegionType
typename TLabelImage::RegionType LabelRegionType
typename TLabelImage::PixelType LabelPixelType
typename HistogramType::Pointer HistogramPointer
typename TInputImage::SizeType SizeType
void SetSize(SizeValueType sz)
Decorates any &quot;simple&quot; data type (data types without smart pointers) with a DataObject API...
typename TLabelImage::SizeType LabelSizeType
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
std::vector< LabelPixelType > ValidLabelValuesContainerType
typename MapType::const_iterator MapConstIterator
ValidLabelValuesContainerType m_ValidLabelValues
DataObject * GetInput(const DataObjectIdentifierType &key)
Return an input.
std::unordered_map< LabelPixelType, LabelStatistics > MapType
Base class for filters that take an image as input and produce an image as output.
LabelStatistics(int size, RealType lowerBound, RealType upperBound)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
const LabelImageType * GetLabelInput() const
typename Superclass::MeasurementVectorType MeasurementVectorType
Definition: itkHistogram.h:102
typename TLabelImage::Pointer LabelImagePointer
typename TLabelImage::IndexType LabelIndexType
typename TInputImage::IndexType IndexType
typename TInputImage::Pointer InputImagePointer
SmartPointer< Self > Pointer
#define itkConceptMacro(name, concept)
Base class for all data objects in ITK.
std::vector< IndexValueType > BoundingBoxType