ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMovingHistogramMorphologicalGradientImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkMovingHistogramMorphologicalGradientImageFilter_h
00019 #define __itkMovingHistogramMorphologicalGradientImageFilter_h
00020 
00021 #include "itkMovingHistogramImageFilter.h"
00022 #include <map>
00023 
00024 namespace itk
00025 {
00026 namespace Function
00027 {
00028 template< class TInputPixel >
00029 class MorphologicalGradientHistogram
00030 {
00031 public:
00032   MorphologicalGradientHistogram()
00033   {
00034   }
00035 
00036   ~MorphologicalGradientHistogram(){}
00037 
00038   inline void AddBoundary() {}
00039 
00040   inline void RemoveBoundary() {}
00041 
00042   typedef std::map< TInputPixel, SizeValueType > MapType;
00043 
00044   inline void AddPixel(const TInputPixel & p)
00045   {
00046     m_Map[p]++;
00047   }
00048 
00049   inline void RemovePixel(const TInputPixel & p)
00050   {
00051     m_Map[p]--;
00052   }
00053 
00054   inline TInputPixel GetValue(const TInputPixel &)
00055   {
00056     return GetValue();
00057   }
00058 
00059   inline TInputPixel GetValue()
00060   {
00061     // clean the map
00062     typename MapType::iterator mapIt = m_Map.begin();
00063     while ( mapIt != m_Map.end() )
00064       {
00065       if ( mapIt->second == 0 )
00066         {
00067         // this value must be removed from the histogram
00068         // The value must be stored and the iterator updated before removing the
00069         // value
00070         // or the iterator is invalidated.
00071         TInputPixel toErase = mapIt->first;
00072         mapIt++;
00073         m_Map.erase(toErase);
00074         }
00075       else
00076         {
00077         mapIt++;
00078         }
00079       }
00080 
00081     // and return the value
00082     if( !m_Map.empty() )
00083       {
00084       return m_Map.rbegin()->first - m_Map.begin()->first;
00085       }
00086     return 0;
00087   }
00088 
00089   static bool UseVectorBasedAlgorithm()
00090   {
00091     return false;
00092   }
00093 
00094   MapType m_Map;
00095 };
00096 
00097 
00098 template< class TInputPixel >
00099 class VectorMorphologicalGradientHistogram
00100 {
00101 public:
00102   VectorMorphologicalGradientHistogram()
00103   {
00104     // initialize members need for the vector based algorithm
00105     m_Vector.resize(NumericTraits< TInputPixel >::max() - NumericTraits< TInputPixel >::NonpositiveMin() + 1, 0);
00106     m_Max = NumericTraits< TInputPixel >::NonpositiveMin();
00107     m_Min = NumericTraits< TInputPixel >::max();
00108     m_Count = 0;
00109   }
00110 
00111   ~VectorMorphologicalGradientHistogram(){}
00112 
00113   inline void AddBoundary() {}
00114 
00115   inline void RemoveBoundary() {}
00116 
00117 
00118   inline void AddPixel(const TInputPixel & p)
00119   {
00120     m_Vector[p - NumericTraits < TInputPixel > ::NonpositiveMin()]++;
00121     if ( p > m_Max )
00122       {
00123       m_Max = p;
00124       }
00125     if ( p < m_Min )
00126       {
00127       m_Min = p;
00128       }
00129     m_Count++;
00130   }
00131 
00132   inline void RemovePixel(const TInputPixel & p)
00133   {
00134     m_Vector[p - NumericTraits < TInputPixel > ::NonpositiveMin()]--;
00135     m_Count--;
00136     if ( m_Count > 0 )
00137       {
00138       while ( m_Vector[m_Max - NumericTraits < TInputPixel > ::NonpositiveMin()] == 0 )
00139         {
00140         m_Max--;
00141         }
00142       while ( m_Vector[m_Min - NumericTraits < TInputPixel > ::NonpositiveMin()] == 0 )
00143         {
00144         m_Min++;
00145         }
00146       }
00147     else
00148       {
00149       m_Max = NumericTraits< TInputPixel >::NonpositiveMin();
00150       m_Min = NumericTraits< TInputPixel >::max();
00151       }
00152   }
00153 
00154   inline TInputPixel GetValue(const TInputPixel &)
00155   {
00156     return GetValue();
00157   }
00158 
00159   inline TInputPixel GetValue()
00160   {
00161     if ( m_Count > 0 )
00162       {
00163       return m_Max - m_Min;
00164       }
00165     else
00166       {
00167       return NumericTraits< TInputPixel >::Zero;
00168       }
00169   }
00170 
00171   static bool UseVectorBasedAlgorithm()
00172   {
00173     return true;
00174   }
00175 
00176   std::vector< SizeValueType > m_Vector;
00177   TInputPixel                  m_Min;
00178   TInputPixel                  m_Max;
00179   SizeValueType                m_Count;
00180 };
00181 
00184 // now create MorphologicalGradientHistogram specilizations using the VectorMorphologicalGradientHistogram
00185 // as base class
00186 
00187 template<>
00188 class MorphologicalGradientHistogram<unsigned char>:
00189   public VectorMorphologicalGradientHistogram<unsigned char>
00190 {
00191 };
00192 
00193 template<>
00194 class MorphologicalGradientHistogram<signed char>:
00195   public VectorMorphologicalGradientHistogram<signed char>
00196 {
00197 };
00198 
00199 template<>
00200 class MorphologicalGradientHistogram<bool>:
00201   public VectorMorphologicalGradientHistogram<bool>
00202 {
00203 };
00204 
00207 } // end namespace Function
00208 
00224 template< class TInputImage, class TOutputImage, class TKernel >
00225 class ITK_EXPORT MovingHistogramMorphologicalGradientImageFilter:
00226   public MovingHistogramImageFilter< TInputImage, TOutputImage, TKernel,
00227                                      typename  Function::MorphologicalGradientHistogram< typename TInputImage::
00228                                                                                          PixelType > >
00229 {
00230 public:
00232   typedef MovingHistogramMorphologicalGradientImageFilter Self;
00233   typedef MovingHistogramImageFilter< TInputImage, TOutputImage, TKernel,
00234                                       typename  Function::MorphologicalGradientHistogram< typename TInputImage::
00235                                                                                           PixelType > >  Superclass;
00236   typedef SmartPointer< Self >       Pointer;
00237   typedef SmartPointer< const Self > ConstPointer;
00238 
00240   itkNewMacro(Self);
00241 
00243   itkTypeMacro(MovingHistogramMorphologicalGradientImageFilter,
00244                ImageToImageFilter);
00245 
00247   typedef TInputImage                                InputImageType;
00248   typedef TOutputImage                               OutputImageType;
00249   typedef typename TInputImage::RegionType           RegionType;
00250   typedef typename TInputImage::SizeType             SizeType;
00251   typedef typename TInputImage::IndexType            IndexType;
00252   typedef typename TInputImage::PixelType            PixelType;
00253   typedef typename TInputImage::OffsetType           OffsetType;
00254   typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
00255   typedef typename TOutputImage::PixelType           OutputPixelType;
00256 
00257   typedef Function::MorphologicalGradientHistogram< PixelType > HistogramType;
00258 
00260   itkStaticConstMacro(ImageDimension, unsigned int,
00261                       TInputImage::ImageDimension);
00262 
00265   static bool GetUseVectorBasedAlgorithm()
00266   { return Function::MorphologicalGradientHistogram< typename TInputImage::PixelType >::UseVectorBasedAlgorithm(); }
00267 protected:
00268   MovingHistogramMorphologicalGradientImageFilter() {}
00269   ~MovingHistogramMorphologicalGradientImageFilter() {}
00270 private:
00271   MovingHistogramMorphologicalGradientImageFilter(const Self &); //purposely not
00272                                                                  // implemented
00273   void operator=(const Self &);                                  //purposely not
00274                                                                  // implemented
00275 };                                                               // end of class
00276 } // end namespace itk
00278 
00279 #endif
00280