ITK/Examples/WishList/Segmentation/EstimatePCAModel: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Use itkMultiplyImageFilter instead of the deprecated itkMultiplyByConstantImageFilter. Correct signed-ness of input arguments.)
m (moved ITK/Examples/Segmentation/EstimatePCAModel to ITK/Examples/WishList/Segmentation/EstimatePCAModel over redirect: Need example input images and arguments.)
(No difference)

Revision as of 15:02, 28 October 2012

EstimatePCAModel.cxx

<source lang="cpp"> /* Author: Juan Cardelino <juan dot cardelino at gmail dot com>

  • /

// ITK

  1. include "itkBinaryThresholdImageFilter.h"
  2. include "itkBoundedReciprocalImageFilter.h"
  3. include "itkChangeInformationImageFilter.h"
  4. include "itkCommand.h"
  5. include "itkCurvatureAnisotropicDiffusionImageFilter.h"
  6. include "itkEuler2DTransform.h"
  7. include "itkFastMarchingImageFilter.h"
  8. include "itkGeodesicActiveContourShapePriorLevelSetImageFilter.h"
  9. include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
  10. include "itkImage.h"
  11. include "itkImageFileReader.h"
  12. include "itkImageFileWriter.h"
  13. include "itkImagePCAShapeModelEstimator.h"
  14. include "itkMultiplyImageFilter.h"
  15. include "itkNumericSeriesFileNames.h"
  16. include "itkNormalVariateGenerator.h"
  17. include "itkOnePlusOneEvolutionaryOptimizer.h"
  18. include "itkPCAShapeSignedDistanceFunction.h"
  19. include "itkRescaleIntensityImageFilter.h"
  20. include "itkShapePriorMAPCostFunction.h"
  21. include "itkSpatialFunctionImageEvaluatorFilter.h"

// VNL

  1. include "vnl/vnl_sample.h"

int main( int argc, char *argv[] ) {

 if( argc < 5 )
   {
   std::cerr << "Missing Parameters " << std::endl;
   std::cerr << "Usage: " << argv[0];
   std::cerr << " nbTrain  trainFilePattern";
   std::cerr << " nbModes  modeFilePattern";
   std::cerr << std::endl;
   return 1;
   }
 for(int i=0; i<argc;i++)
   {
   std::cout << "id: " << i << " arg: " << argv[i] << std::endl;
   }
 const   unsigned int        Dimension = 2;
 typedef float    my_PixelType;
 typedef itk::Image< my_PixelType, Dimension >    ImageType;
 typedef  itk::ImageFileReader< ImageType > ReaderType;
 typedef  itk::ImageFileWriter<  ImageType  > WriterType;
 typedef itk::MultiplyImageFilter < ImageType , ImageType ,ImageType > ScaleType;
 unsigned int nb_train=atoi(argv[1]);
 itk::NumericSeriesFileNames::Pointer fileNamesCreator =
                                      itk::NumericSeriesFileNames::New();
 std::vector<ImageType::Pointer> trainingImages( nb_train );
 fileNamesCreator->SetStartIndex( 0 );
 fileNamesCreator->SetEndIndex( nb_train - 1 );
 fileNamesCreator->SetSeriesFormat( argv[2] );
 const std::vector<std::string> & shapeModeFileNames =
                                  fileNamesCreator->GetFileNames();
 for ( unsigned int k = 0; k < nb_train; k++ )
   {
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName( shapeModeFileNames[k].c_str() );
   reader->Update();
   trainingImages[k] = reader->GetOutput();
   }
 typedef itk::ImagePCAShapeModelEstimator<ImageType,   ImageType >  my_Estimatortype;
 my_Estimatortype::Pointer filter = my_Estimatortype::New();
 filter->SetNumberOfTrainingImages(nb_train);
 filter->SetNumberOfPrincipalComponentsRequired(2);
 for ( unsigned int k = 0; k < nb_train; k++ )
   {
   filter->SetInput(k, trainingImages[k] );
   }
 unsigned int nb_modes=atoi(argv[3]);
 itk::NumericSeriesFileNames::Pointer fileNamesOutCreator =
                                      itk::NumericSeriesFileNames::New();
 fileNamesOutCreator->SetStartIndex( 0 );
 fileNamesOutCreator->SetEndIndex( nb_modes-1  );
 fileNamesOutCreator->SetSeriesFormat( argv[4] );
 const std::vector<std::string> & outFileNames = fileNamesOutCreator->GetFileNames();
 ScaleType::Pointer scaler = ScaleType::New();
 filter->Update();
 my_Estimatortype::VectorOfDoubleType v=filter->GetEigenValues();
 double sv_mean=sqrt(v[0]);
 for ( unsigned int k = 0; k < nb_modes; k++ )
   {
   double sv=sqrt(v[k]);
   double sv_n=sv/sv_mean;
   //double sv_n=sv;
   std::cout << "writing: " << outFileNames[k] << std::endl;
   std::cout << "svd[" << k << "]: " << sv << " norm: " << sv_n << std::endl;
   WriterType::Pointer writer = WriterType::New();
   writer->SetFileName( outFileNames[k].c_str() );
   scaler->SetInput(filter->GetOutput(k));
   scaler->SetConstant(sv_n);
   writer->SetInput( scaler->GetOutput() );
   writer->Update();
   }
 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(EstimatePCAModel)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(EstimatePCAModel MACOSX_BUNDLE EstimatePCAModel.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(EstimatePCAModel ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(EstimatePCAModel ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build EstimatePCAModel

Click here to download EstimatePCAModel and its CMakeLists.txt file. Once the tarball EstimatePCAModel.tar has been downloaded and extracted,

cd EstimatePCAModel/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./EstimatePCAModel

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.