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

itkMovingHistogramMorphologicalGradientImageFilter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMovingHistogramMorphologicalGradientImageFilter.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-09-19 19:10:37 $
00007   Version:   $Revision: 1.6 $
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 __itkMovingHistogramMorphologicalGradientImageFilter_h
00018 #define __itkMovingHistogramMorphologicalGradientImageFilter_h
00019 
00020 #include "itkMovingHistogramImageFilter.h"
00021 #include <map>
00022 
00023 namespace itk {
00024 
00025 namespace Function {
00026 template <class TInputPixel>
00027 class MorphologicalGradientHistogram
00028 {
00029 public:
00030   MorphologicalGradientHistogram()
00031     {
00032     m_UseVectorBasedAlgorithm = UseVectorBasedAlgorithm();
00033     if( m_UseVectorBasedAlgorithm )
00034       { initVector(); }
00035     }
00036   ~MorphologicalGradientHistogram(){}
00037 
00038   MorphologicalGradientHistogram * Clone() const
00039     {
00040     MorphologicalGradientHistogram * result = new MorphologicalGradientHistogram();
00041     result->m_Map = this->m_Map;
00042     result->m_Vector = this->m_Vector;
00043     result->m_Min = this->m_Min;
00044     result->m_Max = this->m_Max;
00045     result->m_Count = this->m_Count;
00046     return result;
00047     }
00048 
00049   inline void AddBoundary() {}
00050 
00051   inline void RemoveBoundary() {}
00052 
00053   inline void AddPixel( const TInputPixel &p )
00054     {
00055     if( m_UseVectorBasedAlgorithm )
00056       { AddPixelVector( p ); }
00057     else
00058       { AddPixelMap( p ); }
00059     }
00060 
00061   inline void RemovePixel( const TInputPixel &p )
00062     {
00063     if( m_UseVectorBasedAlgorithm )
00064       { RemovePixelVector( p ); }
00065     else
00066       { RemovePixelMap( p ); }
00067     }
00068 
00069   inline TInputPixel GetValue( const TInputPixel & )
00070     {
00071     if( m_UseVectorBasedAlgorithm )
00072       { return GetValueVector(); }
00073     else
00074       { return GetValueMap(); }
00075     }
00076 
00077 
00078   static inline bool UseVectorBasedAlgorithm()
00079     {
00080     // bool, short and char are acceptable for vector based algorithm: they do not require
00081     // too much memory. Other types are not usable with that algorithm
00082     return typeid(TInputPixel) == typeid(unsigned char)
00083         || typeid(TInputPixel) == typeid(signed char)
00084         || typeid(TInputPixel) == typeid(unsigned short)
00085         || typeid(TInputPixel) == typeid(signed short)
00086         || typeid(TInputPixel) == typeid(bool);
00087     }
00088 
00089   bool m_UseVectorBasedAlgorithm;
00090   
00091   //
00092   // the map based algorithm
00093   //
00094 
00095   typedef std::map< TInputPixel, unsigned long > MapType;
00096 
00097   inline void AddPixelMap( const TInputPixel &p )
00098     { m_Map[ p ]++; }
00099 
00100   inline void RemovePixelMap( const TInputPixel &p )
00101     { m_Map[ p ]--; }
00102 
00103   inline TInputPixel GetValueMap()
00104     {
00105     // clean the map
00106     typename MapType::iterator mapIt = m_Map.begin();
00107     while( mapIt != m_Map.end() )
00108       {
00109       if( mapIt->second == 0 )
00110         { 
00111         // this value must be removed from the histogram
00112         // The value must be stored and the iterator updated before removing the value
00113         // or the iterator is invalidated.
00114         TInputPixel toErase = mapIt->first;
00115         mapIt++;
00116         m_Map.erase(toErase);
00117         }
00118       else
00119         {
00120         mapIt++;
00121         }
00122       }
00123 
00124     // and return the value
00125     if( !m_Map.empty() )
00126       { return m_Map.rbegin()->first - m_Map.begin()->first; }
00127     return 0;
00128     }
00129 
00130   MapType m_Map;
00131 
00132 
00133   //
00134   // the vector based algorithm
00135   //
00136 
00137   inline void initVector()
00138     {
00139     // initialize members need for the vector based algorithm
00140     m_Vector.resize( static_cast<int>( NumericTraits< TInputPixel >::max() - NumericTraits< TInputPixel >::NonpositiveMin() + 1 ), 0 );
00141     m_Max = NumericTraits< TInputPixel >::NonpositiveMin();
00142     m_Min = NumericTraits< TInputPixel >::max();
00143     m_Count = 0;
00144     }
00145 
00146   inline void AddPixelVector( const TInputPixel &p )
00147     {
00148     m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]++;
00149     if( p > m_Max )
00150       { m_Max = p; }
00151     if( p < m_Min )
00152       { m_Min = p; }
00153     m_Count++;
00154     }
00155 
00156   inline void RemovePixelVector( const TInputPixel &p )
00157     {
00158     m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]--;
00159     m_Count--;
00160     if( m_Count > 0 )
00161       {
00162       while( m_Vector[ static_cast<int>( m_Max - NumericTraits< TInputPixel >::NonpositiveMin() ) ] == 0 )
00163         { m_Max--; }
00164       while( m_Vector[ static_cast<int>( m_Min - NumericTraits< TInputPixel >::NonpositiveMin() ) ] == 0 )
00165         { m_Min++; }
00166       }
00167     else
00168       {
00169       m_Max = NumericTraits< TInputPixel >::NonpositiveMin();
00170       m_Min = NumericTraits< TInputPixel >::max();
00171       }
00172     }
00173 
00174   inline TInputPixel GetValueVector()
00175     {
00176     if( m_Count > 0 )
00177       { return m_Max - m_Min; }
00178     else
00179       { return NumericTraits< TInputPixel >::Zero; }
00180     }
00181 
00182   std::vector<unsigned long> m_Vector;
00183   TInputPixel                m_Min;
00184   TInputPixel                m_Max;
00185   unsigned long              m_Count;
00186 
00187 
00188 };
00189 } // end namespace Function
00190 
00206 template<class TInputImage, class TOutputImage, class TKernel>
00207 class ITK_EXPORT MovingHistogramMorphologicalGradientImageFilter : 
00208     public MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel,
00209       typename  Function::MorphologicalGradientHistogram< typename TInputImage::PixelType > >
00210 {
00211 public:
00213   typedef MovingHistogramMorphologicalGradientImageFilter Self;
00214   typedef MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel,
00215       typename  Function::MorphologicalGradientHistogram< typename TInputImage::PixelType > >  Superclass;
00216   typedef SmartPointer<Self>        Pointer;
00217   typedef SmartPointer<const Self>  ConstPointer;
00218 
00220   itkNewMacro(Self);  
00221 
00223   itkTypeMacro(MovingHistogramMorphologicalGradientImageFilter, 
00224                ImageToImageFilter);
00225 
00227   typedef TInputImage                                 InputImageType;
00228   typedef TOutputImage                                OutputImageType;
00229   typedef typename TInputImage::RegionType            RegionType;
00230   typedef typename TInputImage::SizeType              SizeType;
00231   typedef typename TInputImage::IndexType             IndexType;
00232   typedef typename TInputImage::PixelType             PixelType;
00233   typedef typename TInputImage::OffsetType            OffsetType;
00234   typedef typename Superclass::OutputImageRegionType  OutputImageRegionType;
00235   typedef typename TOutputImage::PixelType            OutputPixelType;
00236 
00237   typedef Function::MorphologicalGradientHistogram< PixelType > HistogramType;
00238 
00239 
00241   itkStaticConstMacro(ImageDimension, unsigned int,
00242                       TInputImage::ImageDimension);
00243 
00244 
00247   static bool GetUseVectorBasedAlgorithm()
00248     { return Function::MorphologicalGradientHistogram< ITK_TYPENAME TInputImage::PixelType >::UseVectorBasedAlgorithm(); }
00249 
00250 protected:
00251   MovingHistogramMorphologicalGradientImageFilter() {};
00252   ~MovingHistogramMorphologicalGradientImageFilter() {};
00253 
00254 
00255 private:
00256   MovingHistogramMorphologicalGradientImageFilter(const Self&); //purposely not implemented
00257   void operator=(const Self&); //purposely not implemented
00258 
00259 }; // end of class
00260 
00261 } // end namespace itk
00262   
00263 #endif
00264 

Generated at Fri Apr 16 19:04:04 2010 for ITK by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2000