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

itkVectorGradientMagnitudeImageFilter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkVectorGradientMagnitudeImageFilter.h,v $
00005   Language:  C++
00006   Date:      $Date: 2003/01/30 21:02:04 $
00007   Version:   $Revision: 1.5 $
00008 
00009   Copyright (c) 2002 Insight 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 __itkVectorGradientMagnitudeImageFilter_h
00018 #define __itkVectorGradientMagnitudeImageFilter_h
00019 
00020 #include "itkConstNeighborhoodIterator.h"
00021 #include "itkNeighborhoodIterator.h"
00022 #include "itkImageToImageFilter.h"
00023 #include "itkImage.h"
00024 #include "itkVector.h"
00025 #include "vnl/vnl_matrix.h"
00026 #include "vnl/algo/vnl_symmetric_eigensystem.h"
00027 #include "vnl/vnl_math.h"
00028 
00029 namespace itk
00030 {
00125 template < typename TInputImage,
00126            typename TRealType = float,
00127            typename TOutputImage = Image< TRealType,
00128                             ::itk::GetImageDimension<TInputImage>::ImageDimension >
00129 >
00130 class ITK_EXPORT VectorGradientMagnitudeImageFilter :
00131     public ImageToImageFilter< TInputImage, TOutputImage >
00132 {
00133 public:
00135   typedef VectorGradientMagnitudeImageFilter Self;
00136   typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
00137   typedef SmartPointer<Self> Pointer;
00138   typedef SmartPointer<const Self>  ConstPointer;
00139 
00141   itkNewMacro(Self);
00142 
00144   itkTypeMacro(VectorGradientMagnitudeImageFilter, ImageToImageFilter);
00145   
00148   typedef typename TOutputImage::PixelType OutputPixelType;
00149   typedef typename TInputImage::PixelType  InputPixelType;
00150 
00152   typedef TInputImage  InputImageType;
00153   typedef TOutputImage OutputImageType;
00154   typedef typename InputImageType::Pointer  InputImagePointer;
00155   typedef typename OutputImageType::Pointer OutputImagePointer;
00156   
00158   itkStaticConstMacro(ImageDimension, unsigned int,
00159                       TOutputImage::ImageDimension);
00160   
00162   itkStaticConstMacro(VectorDimension, unsigned int,
00163                       InputPixelType::Dimension);
00164 
00166   typedef TRealType RealType;
00167   typedef Vector<TRealType, ::itk::GetVectorDimension<InputPixelType>::VectorDimension> RealVectorType;
00168   typedef Image<RealVectorType, ::itk::GetImageDimension<TInputImage>::ImageDimension>  RealVectorImageType;
00169   
00170 
00173   typedef ConstNeighborhoodIterator<RealVectorImageType> ConstNeighborhoodIteratorType;
00174   
00176   typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
00177 
00186   virtual void GenerateInputRequestedRegion() throw(InvalidRequestedRegionError);
00187 
00191   void SetUseImageSpacingOn()
00192   { this->SetUseImageSpacing(true); }
00193 
00197   void SetUseImageSpacingOff()
00198   { this->SetUseImageSpacing(false); }
00199 
00202   void SetUseImageSpacing(bool);                         
00203   itkGetMacro(UseImageSpacing, bool);
00204 
00207   void SetDerivativeWeights(TRealType data[]);
00208   itkGetVectorMacro(DerivativeWeights, const TRealType, itk::GetImageDimension<TInputImage>::ImageDimension);
00209 
00213   itkSetVectorMacro(ComponentWeights, TRealType, itk::GetVectorDimension<InputPixelType>::VectorDimension);
00214   itkGetVectorMacro(ComponentWeights, const TRealType, itk::GetVectorDimension<InputPixelType>::VectorDimension);
00215   
00221   itkSetMacro(UsePrincipleComponents, bool);
00222   itkGetMacro(UsePrincipleComponents, bool);
00223   void SetUsePrincipleComponentsOn()
00224   {
00225     this->SetUsePrincipleComponents(true);
00226   }
00227   void SetUsePrincipleComponentsOff()
00228   {
00229     this->SetUsePrincipleComponents(false);
00230   }
00231   
00232 protected:
00233   VectorGradientMagnitudeImageFilter();
00234   virtual ~VectorGradientMagnitudeImageFilter() {}
00235 
00239   void BeforeThreadedGenerateData ();
00240 
00252   void ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
00253                             int threadId );
00254 
00255   void PrintSelf(std::ostream& os, Indent indent) const;
00256 
00257   TRealType NonPCEvaluateAtNeighborhood(const ConstNeighborhoodIteratorType &it) const
00258   {
00259     unsigned i, j;
00260     TRealType dx, sum, accum;
00261     accum = NumericTraits<TRealType>::Zero;
00262     for (i = 0; i < ImageDimension; ++i)
00263       {
00264         sum = NumericTraits<TRealType>::Zero;
00265         for (j = 0; j < VectorDimension; ++j)
00266           {
00267             dx = m_DerivativeWeights[i]
00268               * (0.5 * it.GetNext(i)[j] - 0.5 * it.GetPrevious(i)[j]);
00269             sum += m_ComponentWeights[j] * (dx * dx);
00270           }
00271         accum += sum;
00272       }
00273     return ::vnl_math_sqrt(accum);
00274   }
00275 
00276   // Function is defined here because the templating confuses gcc 2.96 when defined
00277   // in .txx file.  jc 1/29/03
00278   TRealType EvaluateAtNeighborhood(const ConstNeighborhoodIteratorType &it) const
00279   {
00280     unsigned int i, j;
00281     vnl_matrix<TRealType> g(ImageDimension, ImageDimension);
00282     vnl_vector_fixed<TRealType, VectorDimension>
00283       d_phi_du[itk::GetImageDimension<TInputImage>::ImageDimension];
00284     
00285     // Calculate the directional derivatives for each vector component using
00286     // central differences.
00287     for (i = 0; i < ImageDimension; i++)
00288       {
00289         for (j = 0; j < VectorDimension; j++)
00290           {  d_phi_du[i][j] = m_DerivativeWeights[i]
00291                * (it.GetNext(i)[j] * 0.5 - it.GetPrevious(i)[j] * 0.5); }
00292       }
00293     
00294     // Calculate the symmetric metric tensor g
00295     for (i = 0; i < ImageDimension; i++)
00296       {
00297         for (j = i; j < ImageDimension; j++)
00298           {
00299             g[j][i] = g[i][j] = dot_product(d_phi_du[i], d_phi_du[j]);
00300           }
00301       }
00302     
00303     // Find the eigenvalues of g
00304     vnl_symmetric_eigensystem<TRealType> E(g);
00305     
00306     // Return the difference in length between the first two principle axes.
00307     // Note that other edge strength metrics may be appropriate here instead..
00308     return ( E.get_eigenvalue(ImageDimension - 1)
00309              - E.get_eigenvalue(ImageDimension - 2) ); 
00310   }
00311   
00313   TRealType m_DerivativeWeights[itk::GetImageDimension<TInputImage>::ImageDimension];
00314 
00317   TRealType m_ComponentWeights[itk::GetVectorDimension<InputPixelType>::VectorDimension];
00318   
00319 private:
00320   bool m_UseImageSpacing;
00321   bool m_UsePrincipleComponents;
00322   int m_RequestedNumberOfThreads;
00323 
00324   typedef typename InputImageType::Superclass ImageBaseType;
00325 
00326   typename ImageBaseType::ConstPointer m_RealValuedInputImage;
00327   
00328   VectorGradientMagnitudeImageFilter(const Self&); //purposely not implemented
00329   void operator=(const Self&); //purposely not implemented
00330 
00331   typename ConstNeighborhoodIteratorType::RadiusType m_NeighborhoodRadius;  
00332 };
00333   
00334 } // end namespace itk
00335 
00336 #ifndef ITK_MANUAL_INSTANTIATION
00337 #include "itkVectorGradientMagnitudeImageFilter.txx"
00338 #endif
00339 
00340 #endif

Generated at Fri May 21 01:15:30 2004 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000