X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=appli%2Fexamples%2Fexample_ImageGaussianModelEstimator.cxx;fp=appli%2Fexamples%2Fexample_ImageGaussianModelEstimator.cxx;h=0187f2a66b37e334f8c8dc3909b645aa9385f092;hb=d1641ad5bb3587c30c473e78a6ae060a19d7a60e;hp=0000000000000000000000000000000000000000;hpb=bd25303e6df8698b696a61975d9a1aff40c7231d;p=cpPlugins.git diff --git a/appli/examples/example_ImageGaussianModelEstimator.cxx b/appli/examples/example_ImageGaussianModelEstimator.cxx new file mode 100644 index 0000000..0187f2a --- /dev/null +++ b/appli/examples/example_ImageGaussianModelEstimator.cxx @@ -0,0 +1,85 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +// ------------------------------------------------------------------------- +const unsigned int Dim = 2; +typedef double TScalar; +typedef unsigned char TChannel; +typedef itk::RGBPixel< TChannel > TPixel; +typedef itk::Image< TPixel, Dim > TImage; + +// ------------------------------------------------------------------------- +int main( int argc, char* argv[] ) +{ + if( argc < 2 ) + { + std::cerr + << "Usage: " << argv[ 0 ] + << " input_image" << std::endl; + return( 1 ); + + } // fi + std::string input_image_file = argv[ 1 ]; + + // Read image + itk::ImageFileReader< TImage >::Pointer reader = + itk::ImageFileReader< TImage >::New( ); + reader->SetFileName( input_image_file ); + try + { + reader->Update( ); + } + catch( itk::ExceptionObject& err ) + { + std::cerr << "Error caught: " << err << std::endl; + return( 1 ); + + } // yrt + TImage::ConstPointer input_image = reader->GetOutput( ); + + // Compute gaussian model + typedef cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< TScalar, 3 > TEstimator; + + TEstimator::Pointer estimator = TEstimator::New( ); + estimator->Clear( ); + + itk::ImageRegionConstIterator< TImage > it( + input_image, + input_image->GetRequestedRegion( ) + ); + for( it.GoToBegin( ); !it.IsAtEnd( ); ++it ) + { + estimator->AddSample( + TScalar( it.Get( ).GetRed( ) ), + TScalar( it.Get( ).GetGreen( ) ), + TScalar( it.Get( ).GetBlue( ) ) + ); + + } // rof + + itk::Vector< TScalar, 3 > mean; + itk::Matrix< TScalar, 3, 3 > cova; + + estimator->GetModel( mean, cova ); + std::cout + << "Covariance = " << std::endl << cova << std::endl; + std::cout + << "Inverse covariance = " + << std::endl << cova.GetInverse( ) << std::endl; + std::cout + << "Mean = " << std::endl << mean << std::endl; + + return( 0 ); +} + +// eof - $RCSfile$