From a3e7c837b7425ae3b8b3d2f9f596c9ea9f1978f9 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Tue, 28 Apr 2015 17:31:53 -0500 Subject: [PATCH] Gaussian estimation region grow updated --- ...age_RegionGrow_GaussianModelEstimation.cxx | 6 ++- .../Functors/GaussianModelEstimatorFunction.h | 6 ++- .../GaussianModelEstimatorFunction.hxx | 43 +++++++++++++++---- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/appli/examples/example_Image_RegionGrow_GaussianModelEstimation.cxx b/appli/examples/example_Image_RegionGrow_GaussianModelEstimation.cxx index 2d27878..914e3fa 100644 --- a/appli/examples/example_Image_RegionGrow_GaussianModelEstimation.cxx +++ b/appli/examples/example_Image_RegionGrow_GaussianModelEstimation.cxx @@ -41,7 +41,7 @@ int main( int argc, char* argv[] ) { std::cerr << "Usage: " << argv[ 0 ] - << " input_image neighborhood_order support" + << " input_image neighborhood_order support [model_file]" << std::endl; return( 1 ); @@ -123,7 +123,9 @@ int main( int argc, char* argv[] ) typedef fpa::Image::Functors::GaussianModelEstimatorFunction< TColorImage, TScalar > TFunction; TFunction::Pointer function = TFunction::New( ); function->SetModelSupport( support ); - + if( argc > 4 ) + function->LoadModelFromFile( argv[ 4 ] ); + // Prepare region grow filter typedef fpa::Image::RegionGrow< TColorImage, TImage > TFilter; TFilter::Pointer filter = TFilter::New( ); diff --git a/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.h b/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.h index 1017803..e3cabfe 100644 --- a/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.h +++ b/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.h @@ -33,9 +33,12 @@ namespace fpa itkTypeMacro( GaussianModelEstimatorFunction, itkFunctionBase ); itkGetConstMacro( ModelSupport, unsigned long ); - itkSetMacro( ModelSupport, unsigned long ); public: + bool SaveModelToFile( const std::string& filename ) const; + bool LoadModelFromFile( const std::string& filename ); + + virtual void SetModelSupport( const unsigned long& s ); virtual bool Evaluate( const typename I::PixelType& rgb ) const; protected: @@ -52,6 +55,7 @@ namespace fpa TYPbPrFunction m_YPbPrFunction; unsigned long m_ModelSupport; + unsigned long m_RealModelSupport; mutable bool m_Estimating; }; diff --git a/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.hxx b/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.hxx index c9adb71..accfb27 100644 --- a/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.hxx +++ b/lib/fpa/Image/Functors/GaussianModelEstimatorFunction.hxx @@ -1,26 +1,52 @@ #ifndef __FPA__IMAGE__FUNCTORS__GAUSSIANMODELESTIMATORFUNCTION__HXX__ #define __FPA__IMAGE__FUNCTORS__GAUSSIANMODELESTIMATORFUNCTION__HXX__ +// ------------------------------------------------------------------------- +template< class I, class S > +bool fpa::Image::Functors::GaussianModelEstimatorFunction< I, S >:: +SaveModelToFile( const std::string& filename ) const +{ + this->m_Estimator->SaveModelToFile( filename ); +} + +// ------------------------------------------------------------------------- +template< class I, class S > +bool fpa::Image::Functors::GaussianModelEstimatorFunction< I, S >:: +LoadModelFromFile( const std::string& filename ) +{ + this->m_Estimator->LoadModelFromFile( filename ); + this->m_RealModelSupport = + this->m_ModelSupport + this->m_Estimator->GetNumberOfSamples( ); + this->m_Estimating = false; + this->Modified( ); +} + +// ------------------------------------------------------------------------- +template< class I, class S > +void fpa::Image::Functors::GaussianModelEstimatorFunction< I, S >:: +SetModelSupport( const unsigned long& s ) +{ + this->m_ModelSupport = s; + this->m_RealModelSupport = s; + this->Modified( ); +} + // ------------------------------------------------------------------------- template< class I, class S > bool fpa::Image::Functors::GaussianModelEstimatorFunction< I, S >:: Evaluate( const typename I::PixelType& rgb ) const { - if( !this->m_Estimating ) + if( !this->m_Estimating && this->m_ModelSupport > 0 ) { this->m_Estimator->AddSample( this->m_YPbPrFunction( rgb ) ); - if( this->m_Estimator->GetNumberOfSamples( ) == this->m_ModelSupport ) - { + if( this->m_Estimator->GetNumberOfSamples( ) == this->m_RealModelSupport ) this->m_Estimating = true; - this->m_Estimator->UpdateModel( ); - - } // fi return( true ); } else { S p = this->m_Estimator->Probability( this->m_YPbPrFunction( rgb ) ); - return( p >= this->m_Estimator->GetMinimumProbability( ) ); + return( p >= 0.3 ); } // fi } @@ -30,7 +56,8 @@ template< class I, class S > fpa::Image::Functors::GaussianModelEstimatorFunction< I, S >:: GaussianModelEstimatorFunction( ) : Superclass( ), - m_ModelSupport( 10 ) + m_ModelSupport( 10 ), + m_RealModelSupport( 10 ) { this->m_Estimator = TEstimator::New( ); this->m_Estimator->Clear( ); -- 2.47.1