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: 2006/03/29 14:53:40 $
00007   Version:   $Revision: 1.9 $
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   typedef itk::Statistics::Histogram<RealType,1> HistogramType;
00104   typedef typename HistogramType::Pointer HistogramPointer;
00105 
00107   class LabelStatistics
00108   {
00109   public:
00110 
00111     // default constructor
00112     LabelStatistics()
00113       {
00114       // initialized to the default values
00115       m_Count = 0;
00116       m_Sum = NumericTraits<RealType>::Zero;
00117       m_SumOfSquares = NumericTraits<RealType>::Zero;
00118       
00119       // Set such that the first pixel encountered can be compared
00120       m_Minimum = NumericTraits<RealType>::max();
00121       m_Maximum = NumericTraits<RealType>::NonpositiveMin();
00122       
00123       // Default these to zero
00124       m_Mean = NumericTraits<RealType>::Zero;
00125       m_Sigma = NumericTraits<RealType>::Zero;
00126       m_Variance = NumericTraits<RealType>::Zero;
00127 
00128       unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
00129       m_BoundingBox.resize(imageDimension*2);
00130         for (unsigned int i = 0; i < imageDimension * 2; i += 2)
00131           {
00132           m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
00133           m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
00134           }
00135       m_Histogram = 0;
00136 
00137       }
00138 
00139       // constructor with histogram enabled
00140       LabelStatistics(int size, RealType lowerBound, RealType upperBound)
00141         {
00142         
00143         // initialized to the default values
00144         m_Count = 0;
00145         m_Sum = NumericTraits<RealType>::Zero;
00146         m_SumOfSquares = NumericTraits<RealType>::Zero;
00147 
00148         // Set such that the first pixel encountered can be compared
00149         m_Minimum = NumericTraits<RealType>::max();
00150         m_Maximum = NumericTraits<RealType>::NonpositiveMin();
00151 
00152         // Default these to zero
00153         m_Mean = NumericTraits<RealType>::Zero;
00154         m_Sigma = NumericTraits<RealType>::Zero;
00155         m_Variance = NumericTraits<RealType>::Zero;
00156 
00157         unsigned int imageDimension = itkGetStaticConstMacro(ImageDimension);
00158         m_BoundingBox.resize(imageDimension*2);
00159         for (unsigned int i = 0; i < imageDimension * 2; i += 2)
00160           {
00161           m_BoundingBox[i] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::max();
00162           m_BoundingBox[i+1] = NumericTraits<ITK_TYPENAME IndexType::IndexValueType>::NonpositiveMin();
00163           }
00164 
00165        // Histogram
00166         m_Histogram = HistogramType::New();
00167         typename HistogramType::SizeType hsize;
00168         hsize[0] = size;
00169         typename HistogramType::MeasurementVectorType lb;
00170         lb[0] = lowerBound;
00171         typename HistogramType::MeasurementVectorType ub;
00172         ub[0] = upperBound;
00173         m_Histogram->Initialize(hsize, lb, ub);
00174         }
00175 
00176       // need copy constructor because of smart pointer to histogram
00177       LabelStatistics(const LabelStatistics& l)
00178         {
00179         m_Count = l.m_Count;
00180         m_Minimum = l.m_Minimum;
00181         m_Maximum = l.m_Maximum;
00182         m_Mean = l.m_Mean;
00183         m_Sum = l.m_Sum;
00184         m_SumOfSquares = l.m_SumOfSquares;
00185         m_Sigma = l.m_Sigma;
00186         m_Variance = l.m_Variance;
00187         m_BoundingBox = l.m_BoundingBox;
00188         m_Histogram = l.m_Histogram;
00189         }
00190 
00191     // added for completeness
00192       LabelStatistics& operator= (const LabelStatistics& l)
00193         {
00194         m_Count = l.m_Count;
00195         m_Minimum = l.m_Minimum;
00196         m_Maximum = l.m_Maximum;
00197         m_Mean = l.m_Mean;
00198         m_Sum = l.m_Sum;
00199         m_SumOfSquares = l.m_SumOfSquares;
00200         m_Sigma = l.m_Sigma;
00201         m_Variance = l.m_Variance;
00202         m_BoundingBox = l.m_BoundingBox;
00203         m_Histogram = l.m_Histogram;
00204         }
00205       
00206       unsigned long m_Count;
00207       RealType m_Minimum;
00208       RealType m_Maximum;
00209       RealType m_Mean;
00210       RealType m_Sum;
00211       RealType m_SumOfSquares;
00212       RealType m_Sigma;
00213       RealType m_Variance;
00214       BoundingBoxType m_BoundingBox;
00215       typename HistogramType::Pointer m_Histogram;
00216   };
00217   
00219   typedef itk::hash_map<LabelPixelType, LabelStatistics> MapType;
00220   typedef typename itk::hash_map<LabelPixelType, LabelStatistics>::iterator MapIterator;
00221   typedef typename itk::hash_map<LabelPixelType, LabelStatistics>::const_iterator MapConstIterator;
00222 
00223   // macros for Histogram enables
00224   itkSetMacro(UseHistograms, bool);
00225   itkGetMacro(UseHistograms, bool);
00226   itkBooleanMacro(UseHistograms);
00227   
00229   void SetLabelInput(TLabelImage *input)
00230     {
00231       // Process object is not const-correct so the const casting is required.
00232       this->SetNthInput(1, const_cast<TLabelImage *>(input) );
00233     }
00234 
00236   LabelImageType * GetLabelInput()
00237     {
00238       return static_cast<LabelImageType*>(const_cast<DataObject *>(this->ProcessObject::GetInput(1)));
00239     }
00240 
00243   bool HasLabel(LabelPixelType label) const
00244     {
00245       return m_LabelStatistics.find(label) != m_LabelStatistics.end();
00246     }
00247 
00249   unsigned long GetNumberOfObjects() const
00250     {
00251       return m_LabelStatistics.size();
00252     }
00253   unsigned long GetNumberOfLabels() const
00254     {
00255       return this->GetNumberOfObjects();
00256     }
00258 
00259   
00261   RealType GetMinimum(LabelPixelType label) const;
00262 
00264   RealType GetMaximum(LabelPixelType label) const;
00265 
00267   RealType GetMean(LabelPixelType label) const;
00268 
00270   RealType GetMedian(LabelPixelType label) const;
00271 
00273   RealType GetSigma(LabelPixelType label) const;
00274 
00276   RealType GetVariance(LabelPixelType label) const;
00277 
00279   BoundingBoxType GetBoundingBox(LabelPixelType label) const;
00280 
00282   RegionType GetRegion(LabelPixelType label) const;
00283 
00285   RealType GetSum(LabelPixelType label) const;
00286 
00288   unsigned long GetCount(LabelPixelType label) const;
00289 
00291   HistogramPointer GetHistogram(LabelPixelType label) const;
00292 
00294   void SetHistogramParameters(const int numBins, RealType lowerBound,
00295     RealType upperBound) ;
00296 
00297 #ifdef ITK_USE_CONCEPT_CHECKING
00298 
00299   itkConceptMacro(InputHasNumericTraitsCheck,
00300                   (Concept::HasNumericTraits<PixelType>));
00301 
00303 #endif
00304 
00305 protected:
00306   LabelStatisticsImageFilter();
00307   ~LabelStatisticsImageFilter(){};
00308   void PrintSelf(std::ostream& os, Indent indent) const;
00309 
00311   void AllocateOutputs();      
00312 
00314   void BeforeThreadedGenerateData ();
00315 
00317   void AfterThreadedGenerateData ();
00318 
00320   void  ThreadedGenerateData (const RegionType& 
00321          outputRegionForThread,
00322          int threadId) ;
00323 
00324   // Override since the filter needs all the data for the algorithm
00325   void GenerateInputRequestedRegion();
00326 
00327   // Override since the filter produces all of its output
00328   void EnlargeOutputRequestedRegion(DataObject *data);
00329 
00330 
00331 private:
00332   LabelStatisticsImageFilter(const Self&); //purposely not implemented
00333   void operator=(const Self&); //purposely not implemented
00334 
00335   std::vector<MapType>  m_LabelStatisticsPerThread;
00336   MapType        m_LabelStatistics;
00337 
00338   bool m_UseHistograms;
00339   typename HistogramType::SizeType  m_NumBins;
00340   RealType m_LowerBound;
00341   RealType m_UpperBound;
00342   SimpleFastMutexLock m_Mutex;
00343 
00344 } ; // end of class
00345 
00346 } // end namespace itk
00347   
00348 #ifndef ITK_MANUAL_INSTANTIATION
00349 #include "itkLabelStatisticsImageFilter.txx"
00350 #endif
00351 
00352 #endif
00353 

Generated at Sun Sep 23 13:20:56 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000