]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Algorithms/IterativeGaussianModelEstimator.h
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / IterativeGaussianModelEstimator.h
index aa8e6688cc177e918cc18f7cb74a1346b3be00e7..b38e06ede2d6732e86f4793a97964de10b0859b1 100644 (file)
 #include <vector>
 #include <itkConceptChecking.h>
 #include <itkObject.h>
-#include <vnl/vnl_matrix.h>
+#include <itkObjectFactory.h>
+
+#include <itkCovariantVector.h>
+#include <itkMatrix.h>
+#include <itkPoint.h>
+#include <itkVector.h>
 
 namespace cpExtensions
+{
+  namespace Algorithms
   {
-    namespace Algorithms
+    /**
+     * Recursive formulation taken from:
+     * http://lmb.informatik.uni-freiburg.de/lectures/mustererkennung/Englische_Folien/07_c_ME_en.pdf
+     */
+    template< class _TScalar, unsigned int _VDimension >
+    class IterativeGaussianModelEstimator
+      : public itk::Object
     {
-      /**
-       */
-      template< class S, unsigned int D >
-      class IterativeGaussianModelEstimator
-        : public itk::Object
-      {
-      public:
-        typedef IterativeGaussianModelEstimator Self;
-        typedef itk::Object                     Superclass;
-        typedef itk::SmartPointer< Self >       Pointer;
-        typedef itk::SmartPointer< const Self > ConstPointer;
-
-        typedef S TScalar;
-        itkStaticConstMacro( Dimension, unsigned int, D );
-
-        // Begin concept checking
-#ifdef ITK_USE_CONCEPT_CHECKING
-        itkConceptMacro(
-          ScalarTypeHasFloatResolution,
-          ( itk::Concept::IsFloatingPoint< S > )
-          );
-#endif
-        // End concept checking
-
-        typedef vnl_matrix< S >        TMatrix;
-        typedef std::vector< TMatrix > TMatrices;
-
-      public:
-        itkNewMacro( Self );
-        itkTypeMacro( IterativeGaussianModelEstimator, itkObject );
-
-      public:
-        unsigned long GetNumberOfSamples( ) const
-          { return( ( unsigned long )( this->m_N ) ); }
-        const TMatrix& GetMu( ) const
-          { return( this->m_M ); }
-        const TMatrix& GetOmega( ) const
-          { return( this->m_O ); }
-
-        void SetNumberOfSamples( unsigned long n );
-        void SetMu( const TMatrix& m );
-        void SetOmega( const TMatrix& O );
-
-        bool SaveModelToFile( const std::string& filename ) const;
-        bool LoadModelFromFile( const std::string& filename );
-
-        template< class V >
-        S Probability( const V& sample ) const;
+    public:
+      typedef IterativeGaussianModelEstimator Self;
+      typedef itk::Object                     Superclass;
+      typedef itk::SmartPointer< Self >       Pointer;
+      typedef itk::SmartPointer< const Self > ConstPointer;
 
-        S Probability( const S& s_x, const S& s_y, ... ) const;
+      typedef _TScalar TScalar;
+      itkStaticConstMacro( Dimension, unsigned int, _VDimension );
 
-        template< class V, class M >
-        void GetModel( V& m, M& E ) const;
-
-        void Clear( );
-
-        template< class V >
-        void AddSample( const V& sample );
-
-        void AddSample( const S& s_x, const S& s_y, ... );
-
-      protected:
-        IterativeGaussianModelEstimator( );
-        virtual ~IterativeGaussianModelEstimator( );
-
-      protected:
-        void _UpdateModel( ) const;
-
-      private:
-        // Purposely not implemented
-        IterativeGaussianModelEstimator( const Self& other );
-        void operator=( const Self& other );
+      // Begin concept checking
+#ifdef ITK_USE_CONCEPT_CHECKING
+      itkConceptMacro(
+        ScalarTypeHasFloatResolution,
+        ( itk::Concept::IsFloatingPoint< _TScalar > )
+        );
+#endif
+      // End concept checking
 
-      protected:
-        S m_N;
-        TMatrix m_M;
-        TMatrix m_O;
+      typedef itk::Matrix< TScalar, Dimension, Dimension > TMatrix;
+      typedef itk::Vector< TScalar, Dimension > TVector;
 
-        mutable bool m_Updated;
-        mutable TMatrix m_Cov;
-        mutable TMatrix m_Inv;
-        mutable S m_Norm;
-      };
+    public:
+      itkNewMacro( Self );
+      itkTypeMacro( IterativeGaussianModelEstimator, itkObject );
 
-    } // ecapseman
+      itkGetConstMacro( Mean, TVector );
+      itkGetConstMacro( Covariance, TMatrix );
+      itkGetConstMacro( UnbiasedCovariance, TMatrix );
+      itkGetConstMacro( NumberOfSamples, unsigned long );
+
+      itkSetMacro( Mean, TVector );
+      itkSetMacro( Covariance, TMatrix );
+      itkSetMacro( NumberOfSamples, unsigned long );
+
+    public:
+      void Clear( );
+
+      template< class _TOtherScalar >
+      void AddSample( const _TOtherScalar& x1, ... );
+
+      template< class _TOtherScalar >
+      void AddSample( const _TOtherScalar* array );
+
+      template< class _TOtherScalar >
+      void AddSample( const vnl_vector< _TOtherScalar >& v );
+
+      template< class _TOtherScalar >
+      void AddSample(
+        const itk::CovariantVector< _TOtherScalar, _VDimension >& v
+        );
+
+      template< class _TOtherScalar >
+      void AddSample( const itk::Point< _TOtherScalar, _VDimension >& p );
+
+      template< class _TOtherScalar >
+      void AddSample( const itk::Vector< _TOtherScalar, _VDimension >& v );
+
+      template< class _TOtherScalar >
+      _TScalar SquaredMahalanobis( const _TOtherScalar& x1, ... ) const;
+
+      template< class _TOtherScalar >
+      _TScalar SquaredMahalanobis( const _TOtherScalar* array ) const;
+
+      template< class _TOtherScalar >
+      _TScalar SquaredMahalanobis(
+        const vnl_vector< _TOtherScalar >& v
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar SquaredMahalanobis(
+        const itk::CovariantVector< _TOtherScalar, _VDimension >& v
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar SquaredMahalanobis(
+        const itk::Point< _TOtherScalar, _VDimension >& p
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar SquaredMahalanobis(
+        const itk::Vector< _TOtherScalar, _VDimension >& v
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar Density( const _TOtherScalar& x1, ... ) const;
+
+      template< class _TOtherScalar >
+      _TScalar Density( const _TOtherScalar* array ) const;
+
+      template< class _TOtherScalar >
+      _TScalar Density(
+        const vnl_vector< _TOtherScalar >& v
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar Density(
+        const itk::CovariantVector< _TOtherScalar, _VDimension >& v
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar Density(
+        const itk::Point< _TOtherScalar, _VDimension >& p
+        ) const;
+
+      template< class _TOtherScalar >
+      _TScalar Density(
+        const itk::Vector< _TOtherScalar, _VDimension >& v
+        ) const;
+
+    protected:
+      IterativeGaussianModelEstimator( );
+      virtual ~IterativeGaussianModelEstimator( );
+
+    protected:
+      void _AddSample( const TVector& v ) const;
+      _TScalar _SquaredMahalanobis( const TVector& v ) const;
+      _TScalar _Density( const TVector& v ) const;
+
+    private:
+      // Purposely not implemented
+      IterativeGaussianModelEstimator( const Self& other );
+      void operator=( const Self& other );
+
+    protected:
+      // Recursive avg/cov values
+      mutable unsigned long m_NumberOfSamples;
+      mutable TVector m_Mean;
+      mutable TMatrix m_Covariance;
+      mutable TMatrix m_UnbiasedCovariance;
+      mutable bool m_InverseUpdated;
+      mutable TMatrix m_InverseUnbiasedCovariance;
+    };
+
+  } // ecapseman
 
 } // ecapseman