Orfeo Toolbox  3.16
itkLabelStatisticsImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkLabelStatisticsImageFilter.h,v $
5  Language: C++
6  Date: $Date: 2010-02-05 18:18:37 $
7  Version: $Revision: 1.14 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkLabelStatisticsImageFilter_h
18 #define __itkLabelStatisticsImageFilter_h
19 
20 #include "itkImageToImageFilter.h"
21 #include "itkNumericTraits.h"
22 #include "itkArray.h"
24 #include "itk_hash_map.h"
25 #include "itkHistogram.h"
26 #include "itkFastMutexLock.h"
27 #include <vector>
28 
29 namespace itk {
30 
54 template<class TInputImage, class TLabelImage>
56  public ImageToImageFilter<TInputImage, TInputImage>
57 {
58 public:
64 
66  itkNewMacro(Self);
67 
70 
72  typedef typename TInputImage::Pointer InputImagePointer;
73  typedef typename TInputImage::RegionType RegionType;
74  typedef typename TInputImage::SizeType SizeType;
75  typedef typename TInputImage::IndexType IndexType;
76  typedef typename TInputImage::PixelType PixelType;
77 
79  typedef TLabelImage LabelImageType;
80  typedef typename TLabelImage::Pointer LabelImagePointer;
81  typedef typename TLabelImage::RegionType LabelRegionType;
82  typedef typename TLabelImage::SizeType LabelSizeType;
83  typedef typename TLabelImage::IndexType LabelIndexType;
84  typedef typename TLabelImage::PixelType LabelPixelType;
85 
87  itkStaticConstMacro(ImageDimension, unsigned int,
88  TInputImage::ImageDimension );
89 
91  typedef typename NumericTraits<PixelType>::RealType RealType;
92 
95 
98 
100  typedef std::vector<typename IndexType::IndexValueType> BoundingBoxType;
101 
103 #ifdef ITK_USE_REVIEW_STATISTICS
105 #else
107 #endif
108 
110 
114  {
115  public:
116 
117  // default constructor
119  {
120  // initialized to the default values
121  m_Count = 0;
122  m_Sum = NumericTraits<RealType>::Zero;
123  m_SumOfSquares = NumericTraits<RealType>::Zero;
124 
125  // Set such that the first pixel encountered can be compared
126  m_Minimum = NumericTraits<RealType>::max();
127  m_Maximum = NumericTraits<RealType>::NonpositiveMin();
128 
129  // Default these to zero
130  m_Mean = NumericTraits<RealType>::Zero;
131  m_Sigma = NumericTraits<RealType>::Zero;
132  m_Variance = NumericTraits<RealType>::Zero;
133 
134  unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
135  m_BoundingBox.resize(imageDimension*2);
136  for (unsigned int i = 0; i < imageDimension * 2; i += 2)
137  {
138  m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
139  m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
140  }
141  m_Histogram = 0;
142 
143  }
144 
145  // constructor with histogram enabled
146  LabelStatistics(int size, RealType lowerBound, RealType upperBound)
147  {
148 
149  // initialized to the default values
150  m_Count = 0;
151  m_Sum = NumericTraits<RealType>::Zero;
152  m_SumOfSquares = NumericTraits<RealType>::Zero;
153 
154  // Set such that the first pixel encountered can be compared
155  m_Minimum = NumericTraits<RealType>::max();
156  m_Maximum = NumericTraits<RealType>::NonpositiveMin();
157 
158  // Default these to zero
159  m_Mean = NumericTraits<RealType>::Zero;
160  m_Sigma = NumericTraits<RealType>::Zero;
161  m_Variance = NumericTraits<RealType>::Zero;
162 
163  unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
164  m_BoundingBox.resize(imageDimension*2);
165  for (unsigned int i = 0; i < imageDimension * 2; i += 2)
166  {
167  m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
168  m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
169  }
170 
171  // Histogram
172  m_Histogram = HistogramType::New();
173  typename HistogramType::SizeType hsize;
176 #ifdef ITK_USE_REVIEW_STATISTICS
177  hsize.SetSize(1);
178  lb.SetSize(1);
179  ub.SetSize(1);
180  m_Histogram->SetMeasurementVectorSize(1);
181 #endif
182  hsize[0] = size;
183  lb[0] = lowerBound;
184  ub[0] = upperBound;
185  m_Histogram->Initialize(hsize, lb, ub);
186  }
187 
188  // need copy constructor because of smart pointer to histogram
190  {
191  m_Count = l.m_Count;
192  m_Minimum = l.m_Minimum;
193  m_Maximum = l.m_Maximum;
194  m_Mean = l.m_Mean;
195  m_Sum = l.m_Sum;
196  m_SumOfSquares = l.m_SumOfSquares;
197  m_Sigma = l.m_Sigma;
198  m_Variance = l.m_Variance;
199  m_BoundingBox = l.m_BoundingBox;
200  m_Histogram = l.m_Histogram;
201  }
202 
203  // added for completeness
204  LabelStatistics& operator= (const LabelStatistics& l)
205  {
206  m_Count = l.m_Count;
207  m_Minimum = l.m_Minimum;
208  m_Maximum = l.m_Maximum;
209  m_Mean = l.m_Mean;
210  m_Sum = l.m_Sum;
211  m_SumOfSquares = l.m_SumOfSquares;
212  m_Sigma = l.m_Sigma;
213  m_Variance = l.m_Variance;
214  m_BoundingBox = l.m_BoundingBox;
215  m_Histogram = l.m_Histogram;
216  }
217 
218  unsigned long m_Count;
228  };
229 
234 
235  // macros for Histogram enables
236  itkSetMacro(UseHistograms, bool);
237  itkGetConstMacro(UseHistograms, bool);
238  itkBooleanMacro(UseHistograms);
239 
241  void SetLabelInput(const TLabelImage *input)
242  {
243  // Process object is not const-correct so the const casting is required.
244  this->SetNthInput(1, const_cast<TLabelImage *>(input) );
245  }
246 
248  const LabelImageType * GetLabelInput() const
249  {
250  return static_cast<LabelImageType*>(const_cast<DataObject *>(this->ProcessObject::GetInput(1)));
251  }
252 
255  bool HasLabel(LabelPixelType label) const
256  {
257  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
258  }
259 
261  unsigned long GetNumberOfObjects() const
262  {
263  return m_LabelStatistics.size();
264  }
265  unsigned long GetNumberOfLabels() const
266  {
267  return this->GetNumberOfObjects();
268  }
269 
270 
272  RealType GetMinimum(LabelPixelType label) const;
273 
275  RealType GetMaximum(LabelPixelType label) const;
276 
278  RealType GetMean(LabelPixelType label) const;
279 
281  RealType GetMedian(LabelPixelType label) const;
282 
284  RealType GetSigma(LabelPixelType label) const;
285 
287  RealType GetVariance(LabelPixelType label) const;
288 
290  BoundingBoxType GetBoundingBox(LabelPixelType label) const;
291 
293  RegionType GetRegion(LabelPixelType label) const;
294 
296  RealType GetSum(LabelPixelType label) const;
297 
299  unsigned long GetCount(LabelPixelType label) const;
300 
302  HistogramPointer GetHistogram(LabelPixelType label) const;
303 
305  void SetHistogramParameters(const int numBins, RealType lowerBound,
306  RealType upperBound);
307 
308 #ifdef ITK_USE_CONCEPT_CHECKING
309 
310  itkConceptMacro(InputHasNumericTraitsCheck,
313 #endif
314 
315 protected:
318  void PrintSelf(std::ostream& os, Indent indent) const;
319 
321  void AllocateOutputs();
322 
324  void BeforeThreadedGenerateData ();
325 
327  void AfterThreadedGenerateData ();
328 
330  void ThreadedGenerateData (const RegionType&
331  outputRegionForThread,
332  int threadId);
333 
334  // Override since the filter needs all the data for the algorithm
335  void GenerateInputRequestedRegion();
336 
337  // Override since the filter produces all of its output
338  void EnlargeOutputRequestedRegion(DataObject *data);
339 
340 
341 private:
342  LabelStatisticsImageFilter(const Self&); //purposely not implemented
343  void operator=(const Self&); //purposely not implemented
344 
345  std::vector<MapType> m_LabelStatisticsPerThread;
347 
353 
354 }; // end of class
355 
356 } // end namespace itk
357 
358 #ifndef ITK_MANUAL_INSTANTIATION
360 #endif
361 
362 #endif

Generated at Sat Feb 2 2013 23:50:15 for Orfeo Toolbox with doxygen 1.8.1.1