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

Generated at Wed Nov 5 22:36:08 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000