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

itkMovingHistogramMorphologyImageFilter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMovingHistogramMorphologyImageFilter.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 __itkMovingHistogramMorphologyImageFilter_h
00018 #define __itkMovingHistogramMorphologyImageFilter_h
00019 
00020 #include "itkMovingHistogramImageFilter.h"
00021 #include <list>
00022 #include <map>
00023 
00024 namespace itk {
00025 
00026 namespace Function {
00027 template <class TInputPixel, class TCompare>
00028 class MorphologyHistogram
00029 {
00030 public:
00031   MorphologyHistogram()
00032     {
00033     m_UseVectorBasedAlgorithm = UseVectorBasedAlgorithm();
00034     if( m_UseVectorBasedAlgorithm )
00035       { initVector(); }
00036     }
00037   ~MorphologyHistogram(){}
00038 
00039   MorphologyHistogram * Clone() const
00040     {
00041     MorphologyHistogram * result = new MorphologyHistogram();
00042     result->m_Map = this->m_Map;
00043     result->m_Vector = this->m_Vector;
00044     result->m_CurrentValue = this->m_CurrentValue;
00045     result->m_Compare = this->m_Compare;
00046     result->m_Direction = this->m_Direction;
00047     result->m_Boundary = this->m_Boundary;
00048     return result;
00049     }
00050 
00051   // define the method required by the functor and dispatch to the specialized methods
00052 
00053 
00054   inline void AddBoundary()
00055     {
00056     if( m_UseVectorBasedAlgorithm )
00057       { AddBoundaryVector(); }
00058     else
00059       { AddBoundaryMap(); }
00060     }
00061 
00062   inline void RemoveBoundary()
00063     {
00064     if( m_UseVectorBasedAlgorithm )
00065       { RemoveBoundaryVector(); }
00066     else
00067       { RemoveBoundaryMap(); }
00068     }
00069 
00070   inline void AddPixel( const TInputPixel &p )
00071     {
00072     if( m_UseVectorBasedAlgorithm )
00073       { AddPixelVector( p ); }
00074     else
00075       { AddPixelMap( p ); }
00076     }
00077 
00078   inline void RemovePixel( const TInputPixel &p )
00079     {
00080     if( m_UseVectorBasedAlgorithm )
00081       { RemovePixelVector( p ); }
00082     else
00083       { RemovePixelMap( p ); }
00084     }
00085 
00086   inline TInputPixel GetValue( const TInputPixel & )
00087     {
00088     if( m_UseVectorBasedAlgorithm )
00089       { return GetValueVector(); }
00090     else
00091       { return GetValueMap(); }
00092     }
00093 
00094 
00095   inline static bool UseVectorBasedAlgorithm()
00096     {
00097     // bool, short and char are acceptable for vector based algorithm: they do not require
00098     // too much memory. Other types are not usable with that algorithm
00099     return typeid(TInputPixel) == typeid(unsigned char)
00100         || typeid(TInputPixel) == typeid(signed char)
00101         || typeid(TInputPixel) == typeid(unsigned short)
00102         || typeid(TInputPixel) == typeid(signed short)
00103         || typeid(TInputPixel) == typeid(bool);
00104     }
00105 
00106   bool m_UseVectorBasedAlgorithm;
00107   
00108   //
00109   // the map based algorithm
00110   //
00111 
00112   typedef typename std::map< TInputPixel, unsigned long, TCompare > MapType;
00113 
00114   inline void AddBoundaryMap()
00115     { m_Map[ m_Boundary ]++; }
00116 
00117   inline void RemoveBoundaryMap()
00118     { m_Map[ m_Boundary ]--; }
00119 
00120   inline void AddPixelMap( const TInputPixel &p )
00121     { m_Map[ p ]++; }
00122 
00123   inline void RemovePixelMap( const TInputPixel &p )
00124     { m_Map[ p ]--; }
00125 
00126   inline TInputPixel GetValueMap()
00127     {
00128     // clean the map
00129     typename MapType::iterator mapIt = m_Map.begin();
00130     while( mapIt != m_Map.end() )
00131       {
00132       if( mapIt->second == 0 )
00133         { 
00134         // this value must be removed from the histogram
00135         // The value must be stored and the iterator updated before removing the value
00136         // or the iterator is invalidated.
00137         TInputPixel toErase = mapIt->first;
00138         mapIt++;
00139         m_Map.erase( toErase );
00140         }
00141       else
00142         {
00143         mapIt++;
00144         // don't remove all the zero value found, just remove the one before the current maximum value
00145         // the histogram may become quite big on real type image, but it's an important increase of performances
00146         break;
00147         }
00148       }
00149 
00150     // and return the value
00151     return m_Map.begin()->first;
00152     }
00153 
00154   MapType m_Map;
00155 
00156 
00157   //
00158   // the vector based algorithm
00159   //
00160 
00161   inline void initVector()
00162     {
00163     // initialize members need for the vector based algorithm
00164     m_Vector.resize( static_cast<int>( NumericTraits< TInputPixel >::max() - NumericTraits< TInputPixel >::NonpositiveMin() + 1 ), 0 );
00165     if( m_Compare( NumericTraits< TInputPixel >::max(), NumericTraits< TInputPixel >::NonpositiveMin() ) )
00166       {
00167       m_CurrentValue = NumericTraits< TInputPixel >::NonpositiveMin();
00168       m_Direction = -1;
00169       }
00170     else
00171       {
00172       m_CurrentValue = NumericTraits< TInputPixel >::max();
00173       m_Direction = 1;
00174       }
00175     }
00176   
00177 
00178   inline void AddBoundaryVector()
00179     { AddPixelVector( m_Boundary ); }
00180 
00181   inline void RemoveBoundaryVector()
00182     { RemovePixelVector( m_Boundary ); }
00183 
00184   inline void AddPixelVector( const TInputPixel &p )
00185     {
00186     m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]++;
00187     if( m_Compare( p, m_CurrentValue ) )
00188       { m_CurrentValue = p; }
00189     }
00190 
00191   inline void RemovePixelVector( const TInputPixel &p )
00192     {
00193     m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]--;
00194     while( m_Vector[ static_cast<int>( m_CurrentValue - NumericTraits< TInputPixel >::NonpositiveMin() ) ] == 0 )
00195       { m_CurrentValue += m_Direction; }
00196     }
00197 
00198   inline TInputPixel GetValueVector()
00199     { return m_CurrentValue; }
00200 
00201   std::vector<unsigned long> m_Vector;
00202   TInputPixel                m_CurrentValue;
00203   TCompare                   m_Compare;
00204   signed int                 m_Direction;
00205   
00206 
00207   // accessor for boundary value
00208 
00209   void SetBoundary( const TInputPixel & val )
00210     { m_Boundary = val; }
00211 
00212   TInputPixel m_Boundary;
00213 };
00214 } // end namespace Function
00215 
00216 
00228 template<class TInputImage, class TOutputImage, class TKernel, class THistogram>
00229 class ITK_EXPORT MovingHistogramMorphologyImageFilter : 
00230     public MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel, THistogram>
00231 {
00232 public:
00234   typedef MovingHistogramMorphologyImageFilter             Self;
00235   typedef MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel, THistogram>
00236                                                            Superclass;
00237   typedef SmartPointer<Self>                               Pointer;
00238   typedef SmartPointer<const Self>                         ConstPointer;
00239 
00241   itkNewMacro(Self);  
00242 
00244   itkTypeMacro(MovingHistogramMorphologyImageFilter, 
00245                ImageToImageFilter);
00246 
00248   typedef TInputImage                                      InputImageType;
00249   typedef TOutputImage                                     OutputImageType;
00250   typedef typename TInputImage::RegionType                 RegionType;
00251   typedef typename TInputImage::SizeType                   SizeType;
00252   typedef typename TInputImage::IndexType                  IndexType;
00253   typedef typename TInputImage::PixelType                  PixelType;
00254   typedef typename TInputImage::OffsetType                 OffsetType;
00255   typedef typename Superclass::OutputImageRegionType       OutputImageRegionType;
00256   typedef typename TOutputImage::PixelType                 OutputPixelType;
00257 
00259   itkStaticConstMacro(ImageDimension, unsigned int,
00260                       TInputImage::ImageDimension);
00261 
00263   typedef TKernel                                          KernelType;
00264 
00266   typedef typename KernelType::ConstIterator               KernelIteratorType;
00267 
00269   typedef typename KernelType::SizeType                    RadiusType;
00270 
00271   typedef typename std::list< OffsetType >                 OffsetListType;
00272 
00273   typedef typename std::map< OffsetType, OffsetListType, typename OffsetType::LexicographicCompare >          OffsetMapType;
00274 
00276   itkSetMacro(Boundary, PixelType);
00277   itkGetConstMacro(Boundary, PixelType);
00279 
00282   static bool GetUseVectorBasedAlgorithm()
00283     { return THistogram::UseVectorBasedAlgorithm(); }
00284 
00285 protected:
00286   MovingHistogramMorphologyImageFilter();
00287   ~MovingHistogramMorphologyImageFilter() {};
00288   void PrintSelf(std::ostream& os, Indent indent) const;
00289   
00291 //   void  ThreadedGenerateData (const OutputImageRegionType& 
00292 //                               outputRegionForThread,
00293 //                               int threadId);
00294 
00296   virtual THistogram * NewHistogram();
00297 
00298   PixelType m_Boundary;
00299 
00300 private:
00301   MovingHistogramMorphologyImageFilter(const Self&); //purposely not implemented
00302   void operator=(const Self&); //purposely not implemented
00303 
00304 }; // end of class
00305 
00306 } // end namespace itk
00307   
00308 #ifndef ITK_MANUAL_INSTANTIATION
00309 #include "itkMovingHistogramMorphologyImageFilter.txx"
00310 #endif
00311 
00312 #endif
00313 

Generated at Mon Jul 12 2010 19:11:17 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000