ITK  4.2.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 "itksys/hash_map.hxx"
25 #include "itkHistogram.h"
26 #include "itkFastMutexLock.h"
27 #include <vector>
28 
29 namespace itk
30 {
59 template< class TInputImage, class TLabelImage >
60 class ITK_EXPORT LabelStatisticsImageFilter:
61  public ImageToImageFilter< TInputImage, TInputImage >
62 {
63 public:
69 
71  itkNewMacro(Self);
72 
75 
77  typedef typename TInputImage::Pointer InputImagePointer;
78  typedef typename TInputImage::RegionType RegionType;
79  typedef typename TInputImage::SizeType SizeType;
80  typedef typename TInputImage::IndexType IndexType;
81  typedef typename TInputImage::PixelType PixelType;
82 
84  typedef TLabelImage LabelImageType;
85  typedef typename TLabelImage::Pointer LabelImagePointer;
86  typedef typename TLabelImage::RegionType LabelRegionType;
87  typedef typename TLabelImage::SizeType LabelSizeType;
88  typedef typename TLabelImage::IndexType LabelIndexType;
89  typedef typename TLabelImage::PixelType LabelPixelType;
90 
92  itkStaticConstMacro(ImageDimension, unsigned int,
93  TInputImage::ImageDimension);
94 
97 
100 
103 
105  typedef std::vector< IndexValueType > BoundingBoxType;
106 
110 
116  {
117 public:
118 
119  // default constructor
121  {
122  // initialized to the default values
125  m_SumOfSquares = NumericTraits< RealType >::Zero;
126 
127  // Set such that the first pixel encountered can be compared
128  m_Minimum = NumericTraits< RealType >::max();
130 
131  // Default these to zero
134  m_Variance = NumericTraits< RealType >::Zero;
135 
136  unsigned int imageDimension = itkGetStaticConstMacro(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 = 0;
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 >::Zero;
153 
154  // Set such that the first pixel encountered can be compared
155  m_Minimum = NumericTraits< RealType >::max();
157 
158  // Default these to 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< 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  // added for completeness
202  void operator= (const LabelStatistics& l)
203  {
204  m_Count = l.m_Count;
205  m_Minimum = l.m_Minimum;
206  m_Maximum = l.m_Maximum;
207  m_Mean = l.m_Mean;
208  m_Sum = l.m_Sum;
209  m_SumOfSquares = l.m_SumOfSquares;
210  m_Sigma = l.m_Sigma;
211  m_Variance = l.m_Variance;
212  m_BoundingBox = l.m_BoundingBox;
213  m_Histogram = l.m_Histogram;
214  }
215 
226  };
227 
229  typedef itksys::hash_map< LabelPixelType, LabelStatistics > MapType;
230  typedef typename itksys::hash_map< LabelPixelType, LabelStatistics >::iterator MapIterator;
231  typedef typename itksys::hash_map< LabelPixelType, LabelStatistics >::const_iterator MapConstIterator;
233 
235  typedef std::vector<LabelPixelType> ValidLabelValuesContainerType;
236 
237  // macros for Histogram enables
238  itkSetMacro(UseHistograms, bool);
239  itkGetConstMacro(UseHistograms, bool);
240  itkBooleanMacro(UseHistograms);
241 
242 
243  virtual const ValidLabelValuesContainerType &GetValidLabelValues() const
244  {
245  return m_ValidLabelValues;
246  }
247 
249  void SetLabelInput(const TLabelImage *input)
250  {
251  // Process object is not const-correct so the const casting is required.
252  this->SetNthInput( 1, const_cast< TLabelImage * >( input ) );
253  }
254 
256  const LabelImageType * GetLabelInput() const
257  {
258  return static_cast< LabelImageType * >( const_cast< DataObject * >( this->ProcessObject::GetInput(1) ) );
259  }
260 
263  bool HasLabel(LabelPixelType label) const
264  {
265  return m_LabelStatistics.find(label) != m_LabelStatistics.end();
266  }
267 
269  MapSizeType GetNumberOfObjects() const
270  {
271  return m_LabelStatistics.size();
272  }
273 
274  MapSizeType GetNumberOfLabels() const
275  {
276  return this->GetNumberOfObjects();
277  }
278 
280  RealType GetMinimum(LabelPixelType label) const;
281 
283  RealType GetMaximum(LabelPixelType label) const;
284 
286  RealType GetMean(LabelPixelType label) const;
287 
290  RealType GetMedian(LabelPixelType label) const;
291 
293  RealType GetSigma(LabelPixelType label) const;
294 
296  RealType GetVariance(LabelPixelType label) const;
297 
299  BoundingBoxType GetBoundingBox(LabelPixelType label) const;
300 
302  RegionType GetRegion(LabelPixelType label) const;
303 
305  RealType GetSum(LabelPixelType label) const;
306 
308  MapSizeType GetCount(LabelPixelType label) const;
309 
311  HistogramPointer GetHistogram(LabelPixelType label) const;
312 
314  void SetHistogramParameters(const int numBins, RealType lowerBound,
315  RealType upperBound);
316 
317 #ifdef ITK_USE_CONCEPT_CHECKING
318 
319  itkConceptMacro( InputHasNumericTraitsCheck,
321 
323 #endif
324 protected:
327  void PrintSelf(std::ostream & os, Indent indent) const;
329 
332  void AllocateOutputs();
333 
335  void BeforeThreadedGenerateData();
336 
339  void AfterThreadedGenerateData();
340 
342  void ThreadedGenerateData(const RegionType &
343  outputRegionForThread,
344  ThreadIdType threadId);
345 
346  // Override since the filter needs all the data for the algorithm
347  void GenerateInputRequestedRegion();
348 
349  // Override since the filter produces all of its output
350  void EnlargeOutputRequestedRegion(DataObject *data);
351 
352 private:
353  LabelStatisticsImageFilter(const Self &); //purposely not implemented
354  void operator=(const Self &); //purposely not implemented
355 
356  std::vector< MapType > m_LabelStatisticsPerThread;
359 
361 
363 
367 }; // end of class
368 } // end namespace itk
369 
370 #ifndef ITK_MANUAL_INSTANTIATION
371 #include "itkLabelStatisticsImageFilter.hxx"
372 #endif
373 
374 #endif
375