Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkLabelStatisticsImageFilter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkLabelStatisticsImageFilter.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-05-05 18:16:17 $
00007   Version:   $Revision: 1.13 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkLabelStatisticsImageFilter_h
00018 #define __itkLabelStatisticsImageFilter_h
00019 
00020 #include "itkImageToImageFilter.h"
00021 #include "itkNumericTraits.h"
00022 #include "itkArray.h"
00023 #include "itkSimpleDataObjectDecorator.h"
00024 #include "itk_hash_map.h"
00025 #include "itkHistogram.h"
00026 #include "itkFastMutexLock.h"
00027 #include <vector>
00028 
00029 namespace itk {
00030 
00054 template<class TInputImage, class TLabelImage>
00055 class ITK_EXPORT LabelStatisticsImageFilter : 
00056     public ImageToImageFilter<TInputImage, TInputImage>
00057 {
00058 public:
00060   typedef LabelStatisticsImageFilter                  Self;
00061   typedef ImageToImageFilter<TInputImage,TInputImage> Superclass;
00062   typedef SmartPointer<Self>                          Pointer;
00063   typedef SmartPointer<const Self>                    ConstPointer;
00064 
00066   itkNewMacro(Self);  
00067 
00069   itkTypeMacro(LabelStatisticsImageFilter, ImageToImageFilter);
00070 
00072   typedef typename TInputImage::Pointer    InputImagePointer;
00073   typedef typename TInputImage::RegionType RegionType;
00074   typedef typename TInputImage::SizeType   SizeType;
00075   typedef typename TInputImage::IndexType  IndexType;
00076   typedef typename TInputImage::PixelType  PixelType;
00077 
00079   typedef TLabelImage                      LabelImageType;
00080   typedef typename TLabelImage::Pointer    LabelImagePointer;
00081   typedef typename TLabelImage::RegionType LabelRegionType;
00082   typedef typename TLabelImage::SizeType   LabelSizeType;
00083   typedef typename TLabelImage::IndexType  LabelIndexType;
00084   typedef typename TLabelImage::PixelType  LabelPixelType;
00085 
00087   itkStaticConstMacro(ImageDimension, unsigned int,
00088         TInputImage::ImageDimension );
00089 
00091   typedef typename NumericTraits<PixelType>::RealType RealType;
00092 
00094   typedef typename DataObject::Pointer DataObjectPointer;
00095 
00097   typedef SimpleDataObjectDecorator<RealType> RealObjectType;
00098 
00100   typedef std::vector<typename IndexType::IndexValueType> BoundingBoxType;
00101 
00103 #ifdef ITK_USE_REVIEW_STATISTICS
00104   typedef itk::Statistics::Histogram<RealType> HistogramType;
00105 #else
00106   typedef itk::Statistics::Histogram<RealType,1> HistogramType;
00107 #endif
00108 
00109   typedef typename HistogramType::Pointer        HistogramPointer;
00110 
00113   class LabelStatistics
00114     {
00115     public:
00116 
00117     // default constructor
00118     LabelStatistics()
00119       {
00120       // initialized to the default values
00121       m_Count = 0;
00122       m_Sum = NumericTraits<RealType>::Zero;
00123       m_SumOfSquares = NumericTraits<RealType>::Zero;
00124       
00125       // Set such that the first pixel encountered can be compared
00126       m_Minimum = NumericTraits<RealType>::max();
00127       m_Maximum = NumericTraits<RealType>::NonpositiveMin();
00128       
00129       // Default these to zero
00130       m_Mean = NumericTraits<RealType>::Zero;
00131       m_Sigma = NumericTraits<RealType>::Zero;
00132       m_Variance = NumericTraits<RealType>::Zero;
00133         
00134       unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
00135       m_BoundingBox.resize(imageDimension*2);
00136       for (unsigned int i = 0; i < imageDimension * 2; i += 2)
00137         {
00138         m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
00139         m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
00140         }
00141       m_Histogram = 0;
00142       
00143       }
00144 
00145       // constructor with histogram enabled
00146       LabelStatistics(int size, RealType lowerBound, RealType upperBound)
00147         {
00148         
00149         // initialized to the default values
00150         m_Count = 0;
00151         m_Sum = NumericTraits<RealType>::Zero;
00152         m_SumOfSquares = NumericTraits<RealType>::Zero;
00153 
00154         // Set such that the first pixel encountered can be compared
00155         m_Minimum = NumericTraits<RealType>::max();
00156         m_Maximum = NumericTraits<RealType>::NonpositiveMin();
00157 
00158         // Default these to zero
00159         m_Mean = NumericTraits<RealType>::Zero;
00160         m_Sigma = NumericTraits<RealType>::Zero;
00161         m_Variance = NumericTraits<RealType>::Zero;
00162 
00163         unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
00164         m_BoundingBox.resize(imageDimension*2);
00165         for (unsigned int i = 0; i < imageDimension * 2; i += 2)
00166           {
00167           m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
00168           m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
00169           }
00170 
00171         // Histogram
00172         m_Histogram = HistogramType::New();
00173         typename HistogramType::SizeType              hsize;
00174         typename HistogramType::MeasurementVectorType lb;
00175         typename HistogramType::MeasurementVectorType ub;
00176 #ifdef ITK_USE_REVIEW_STATISTICS
00177         hsize.SetSize(1);
00178         lb.SetSize(1);
00179         ub.SetSize(1);
00180         m_Histogram->SetMeasurementVectorSize(1);
00181 #endif
00182         hsize[0] = size;
00183         lb[0] = lowerBound;
00184         ub[0] = upperBound;
00185         m_Histogram->Initialize(hsize, lb, ub);
00186         }
00187 
00188       // need copy constructor because of smart pointer to histogram
00189       LabelStatistics(const LabelStatistics& l)
00190         {
00191         m_Count = l.m_Count;
00192         m_Minimum = l.m_Minimum;
00193         m_Maximum = l.m_Maximum;
00194         m_Mean = l.m_Mean;
00195         m_Sum = l.m_Sum;
00196         m_SumOfSquares = l.m_SumOfSquares;
00197         m_Sigma = l.m_Sigma;
00198         m_Variance = l.m_Variance;
00199         m_BoundingBox = l.m_BoundingBox;
00200         m_Histogram = l.m_Histogram;
00201         }
00202 
00203     // added for completeness
00204       LabelStatistics& operator= (const LabelStatistics& l)
00205         {
00206         m_Count = l.m_Count;
00207         m_Minimum = l.m_Minimum;
00208         m_Maximum = l.m_Maximum;
00209         m_Mean = l.m_Mean;
00210         m_Sum = l.m_Sum;
00211         m_SumOfSquares = l.m_SumOfSquares;
00212         m_Sigma = l.m_Sigma;
00213         m_Variance = l.m_Variance;
00214         m_BoundingBox = l.m_BoundingBox;
00215         m_Histogram = l.m_Histogram;
00216         }
00217       
00218       unsigned long m_Count;
00219       RealType m_Minimum;
00220       RealType m_Maximum;
00221       RealType m_Mean;
00222       RealType m_Sum;
00223       RealType m_SumOfSquares;
00224       RealType m_Sigma;
00225       RealType m_Variance;
00226       BoundingBoxType m_BoundingBox;
00227       typename HistogramType::Pointer m_Histogram;
00228   };
00229   
00231   typedef itk::hash_map<LabelPixelType, LabelStatistics> MapType;
00232   typedef typename itk::hash_map<LabelPixelType, LabelStatistics>::iterator MapIterator;
00233   typedef typename itk::hash_map<LabelPixelType, LabelStatistics>::const_iterator MapConstIterator;
00234 
00235   // macros for Histogram enables
00236   itkSetMacro(UseHistograms, bool);
00237   itkGetConstMacro(UseHistograms, bool);
00238   itkBooleanMacro(UseHistograms);
00239   
00241   void SetLabelInput(TLabelImage *input)
00242     {
00243       // Process object is not const-correct so the const casting is required.
00244       this->SetNthInput(1, const_cast<TLabelImage *>(input) );
00245     }
00246 
00248   LabelImageType * GetLabelInput()
00249     {
00250       return static_cast<LabelImageType*>(const_cast<DataObject *>(this->ProcessObject::GetInput(1)));
00251     }
00252 
00255   bool HasLabel(LabelPixelType label) const
00256     {
00257     return m_LabelStatistics.find(label) != m_LabelStatistics.end();
00258     }
00259 
00261   unsigned long GetNumberOfObjects() const
00262     {
00263     return m_LabelStatistics.size();
00264     }
00265   unsigned long GetNumberOfLabels() const
00266     {
00267     return this->GetNumberOfObjects();
00268     }
00270 
00271   
00273   RealType GetMinimum(LabelPixelType label) const;
00274 
00276   RealType GetMaximum(LabelPixelType label) const;
00277 
00279   RealType GetMean(LabelPixelType label) const;
00280 
00282   RealType GetMedian(LabelPixelType label) const;
00283 
00285   RealType GetSigma(LabelPixelType label) const;
00286 
00288   RealType GetVariance(LabelPixelType label) const;
00289 
00291   BoundingBoxType GetBoundingBox(LabelPixelType label) const;
00292 
00294   RegionType GetRegion(LabelPixelType label) const;
00295 
00297   RealType GetSum(LabelPixelType label) const;
00298 
00300   unsigned long GetCount(LabelPixelType label) const;
00301 
00303   HistogramPointer GetHistogram(LabelPixelType label) const;
00304 
00306   void SetHistogramParameters(const int numBins, RealType lowerBound,
00307     RealType upperBound);
00308 
00309 #ifdef ITK_USE_CONCEPT_CHECKING
00310 
00311   itkConceptMacro(InputHasNumericTraitsCheck,
00312                   (Concept::HasNumericTraits<PixelType>));
00313 
00315 #endif
00316 
00317 protected:
00318   LabelStatisticsImageFilter();
00319   ~LabelStatisticsImageFilter(){};
00320   void PrintSelf(std::ostream& os, Indent indent) const;
00321 
00323   void AllocateOutputs();
00324 
00326   void BeforeThreadedGenerateData ();
00327 
00329   void AfterThreadedGenerateData ();
00330 
00332   void  ThreadedGenerateData (const RegionType& 
00333          outputRegionForThread,
00334          int threadId);
00335 
00336   // Override since the filter needs all the data for the algorithm
00337   void GenerateInputRequestedRegion();
00338 
00339   // Override since the filter produces all of its output
00340   void EnlargeOutputRequestedRegion(DataObject *data);
00341 
00342 
00343 private:
00344   LabelStatisticsImageFilter(const Self&); //purposely not implemented
00345   void operator=(const Self&); //purposely not implemented
00346 
00347   std::vector<MapType>  m_LabelStatisticsPerThread;
00348   MapType               m_LabelStatistics;
00349 
00350   bool                             m_UseHistograms;
00351   typename HistogramType::SizeType m_NumBins;
00352   RealType                         m_LowerBound;
00353   RealType                         m_UpperBound;
00354   SimpleFastMutexLock              m_Mutex;
00355 
00356 }; // end of class
00357 
00358 } // end namespace itk
00359   
00360 #ifndef ITK_MANUAL_INSTANTIATION
00361 #include "itkLabelStatisticsImageFilter.txx"
00362 #endif
00363 
00364 #endif
00365 

Generated at Thu May 28 10:38:46 2009 for ITK by doxygen 1.5.5 written by Dimitri van Heesch, © 1997-2000