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

itkAnchorHistogram.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkAnchorHistogram.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-10-20 16:32:08 $
00007   Version:   $Revision: 1.2 $
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 
00018 // histogram from the moving histogram operations
00019 #ifndef __itkAnchorHistogram_h
00020 #define __itkAnchorHistogram_h
00021 #include "itkNumericTraits.h"
00022 #include <vector>
00023 #include <map>
00024 #include "itkIndent.h"
00025 namespace itk {
00026 
00027 // a simple histogram class hierarchy. One subclass will be maps, the
00028 // other vectors
00029 template <class TInputPixel>
00030 class MorphologyHistogram
00031 {
00032 public:
00033   MorphologyHistogram() {}
00034   virtual ~MorphologyHistogram(){}
00035 
00036   virtual void Reset(){}
00037   
00038   virtual void AddBoundary(){}
00039 
00040   virtual void RemoveBoundary(){}
00041   
00042   virtual void AddPixel(const TInputPixel &){}
00043 
00044   virtual void RemovePixel(const TInputPixel &){}
00045  
00046   virtual TInputPixel GetValue()
00047     {
00048     return(m_Boundary);  // a dummy return
00049     }
00050 
00051   void SetBoundary( const TInputPixel & val )
00052     {
00053     m_Boundary = val; 
00054     }
00055 protected:
00056   TInputPixel  m_Boundary;
00057 
00058 };
00059 
00060 template <class TInputPixel, class TCompare>
00061 class MorphologyHistogramMap : public MorphologyHistogram<TInputPixel>
00062 {
00063 private:
00064   typedef typename std::map< TInputPixel, unsigned long, TCompare > MapType;
00065   
00066   MapType m_Map;
00067 
00068 public:
00069   MorphologyHistogramMap() 
00070     {
00071     }
00072   ~MorphologyHistogramMap(){}
00073 
00074   void Reset()
00075     {
00076     m_Map.clear();
00077     }
00078   
00079   void AddBoundary()
00080     {
00081     m_Map[ this->m_Boundary ]++;
00082     }
00083 
00084   void RemoveBoundary()
00085     {
00086     m_Map[ this->m_Boundary ]--; 
00087     }
00088   
00089   void AddPixel(const TInputPixel &p)
00090     {
00091     m_Map[ p ]++; 
00092     }
00093 
00094   void RemovePixel(const TInputPixel &p)
00095     {
00096     m_Map[ p ]--; 
00097     }
00098  
00099   TInputPixel GetValue()
00100     {    // clean the map
00101     typename MapType::iterator mapIt = m_Map.begin();
00102     while( mapIt != m_Map.end() )
00103       {
00104       if( mapIt->second <= 0 )
00105         {
00106         // this value must be removed from the histogram
00107         // The value must be stored and the iterator updated before removing the value
00108         // or the iterator is invalidated.
00109         TInputPixel toErase = mapIt->first;
00110         mapIt++;
00111         m_Map.erase( toErase );
00112         }
00113       else
00114         {
00115         mapIt++;
00116         // don't remove all the zero value found, just remove the one before the current maximum value
00117         // the histogram may become quite big on real type image, but it's an important increase of performances
00118         break;
00119         }
00120       }
00121     
00122     // and return the value
00123     return m_Map.begin()->first;
00124     }
00125 
00126 };
00127 
00128 template <class TInputPixel, class TCompare>
00129 class MorphologyHistogramVec : public MorphologyHistogram<TInputPixel>
00130 {
00131 public:
00132   MorphologyHistogramVec() 
00133     {
00134     m_Size = static_cast<unsigned int>( NumericTraits< TInputPixel >::max() - 
00135                                         NumericTraits< TInputPixel >::NonpositiveMin() + 1 );
00136     m_Vec.resize(m_Size, 0 );
00137     std::fill(m_Vec.begin(), m_Vec.end(),0);
00138     if( m_Compare( NumericTraits< TInputPixel >::max(), 
00139                    NumericTraits< TInputPixel >::NonpositiveMin() ) )
00140       {
00141       m_CurrentValue = m_InitVal = NumericTraits< TInputPixel >::NonpositiveMin();
00142       m_Direction = -1;
00143       }
00144     else
00145       {
00146       m_CurrentValue = m_InitVal = NumericTraits< TInputPixel >::max();
00147       m_Direction = 1;
00148       }
00149     m_Entries = 0;
00150     }
00151   ~MorphologyHistogramVec(){}
00152 
00153   void Reset()
00154     {
00155     m_CurrentValue = m_InitVal;
00156     if (m_Entries != 0)
00157       {
00158       std::fill(m_Vec.begin(), m_Vec.end(),0);
00159       m_Entries = 0;
00160       }
00161     }
00162   
00163   void AddBoundary()
00164     {
00165     AddPixel(this->m_Boundary);
00166     ++m_Entries;
00167     }
00168 
00169   void RemoveBoundary()
00170     {
00171     RemovePixel(this->m_Boundary);
00172     --m_Entries;
00173     }
00174   
00175   void AddPixel(const TInputPixel &p)
00176     {
00177     
00178     m_Vec[ (long unsigned int)(p - NumericTraits< TInputPixel >::NonpositiveMin())  ]++;
00179     if (m_Compare(p, m_CurrentValue))
00180       {
00181       m_CurrentValue = p;
00182       }
00183     ++m_Entries;
00184     }
00185 
00186   void RemovePixel(const TInputPixel &p)
00187     {
00188     assert((int)p - (int)NumericTraits< TInputPixel >::NonpositiveMin() >= 0);
00189     assert(((int)p - (int)NumericTraits< TInputPixel >::NonpositiveMin()) < (int)m_Vec.size());
00190     assert(m_Entries >= 1);
00191     m_Vec[ (long unsigned int)(p - NumericTraits< TInputPixel >::NonpositiveMin())  ]--; 
00192     --m_Entries;
00193     assert(static_cast<int>((int)m_CurrentValue -
00194                             (int)NumericTraits< TInputPixel >::NonpositiveMin() ) >= 0);
00195     assert(static_cast<int>((int)m_CurrentValue -
00196                             (int)NumericTraits< TInputPixel >::NonpositiveMin() ) < (int)m_Vec.size());
00197     if (m_Entries > 0)
00198       {
00199       while( m_Vec[static_cast<int>(m_CurrentValue - 
00200                                     NumericTraits< TInputPixel >::NonpositiveMin() )] == 0 )
00201         {
00202         m_CurrentValue += m_Direction;
00203         assert(static_cast<int>(m_CurrentValue -
00204                                 NumericTraits< TInputPixel >::NonpositiveMin() ) >= 0);
00205         assert(static_cast<int>(m_CurrentValue -
00206                                 NumericTraits< TInputPixel >::NonpositiveMin() ) < (int)m_Vec.size());
00207         }
00208       }
00209     }
00210  
00211   TInputPixel GetValue()
00212     { 
00213     assert(m_Entries > 0);
00214     return(m_CurrentValue);
00215     }
00216 
00217 private:
00218   typedef typename std::vector<unsigned long> VecType;
00219   
00220   VecType      m_Vec;
00221   unsigned int m_Size;
00222   TCompare     m_Compare;
00223   TInputPixel  m_CurrentValue;
00224   TInputPixel  m_InitVal;
00225   int          m_Direction;
00226   int          m_Entries;
00227 
00228 };
00229 
00230 } // end namespace itk
00231 #endif
00232 

Generated at Sat Feb 28 11:57:14 2009 for ITK by doxygen 1.5.6 written by Dimitri van Heesch, © 1997-2000