ITK/Examples/Registration/ExhaustiveOptimizer
ExhaustiveOptimizer.cxx
<source lang="cpp">
- include "itkImageFileReader.h"
- include "itkEuler2DTransform.h"
- include "itkExhaustiveOptimizerv4.h"
- include "itkMeanSquaresImageToImageMetricv4.h"
- include "itkCenteredTransformInitializer.h"
- include "itkImageRegistrationMethodv4.h"
- include "itkImage.h"
// Command observer to monitor the evolution of the registration process. //
- include "itkCommand.h"
class CommandIterationUpdate : public itk::Command { public:
typedef CommandIterationUpdate Self; typedef itk::Command Superclass; typedef itk::SmartPointer<Self> Pointer; itkNewMacro( Self );
protected:
CommandIterationUpdate() {};
public:
typedef itk::ExhaustiveOptimizerv4<double> OptimizerType; typedef const OptimizerType * OptimizerPointer;
void Execute(itk::Object *caller, const itk::EventObject & event) ITK_OVERRIDE { Execute( (const itk::Object *) caller, event); }
void Execute(const itk::Object * object, const itk::EventObject & event) ITK_OVERRIDE { OptimizerPointer optimizer = static_cast< OptimizerPointer >( object ); if( !(itk::IterationEvent().CheckEvent( &event )) ) { return; } std::cout << "Iteration: "; std::cout << optimizer->GetCurrentIteration() << " "; std::cout << optimizer->GetCurrentIndex() << " "; std::cout << optimizer->GetCurrentValue() << " "; std::cout << optimizer->GetCurrentPosition() << " "; std::cout << std::endl; }
};
int main (int argc, char *argv[]) {
if (argc < 3) { std::cout << "Usage: " << argv[0] << " fixedImage movingImage" << std::endl; return EXIT_FAILURE; } typedef itk::Image<double, 2> FixedImageType; typedef itk::Image<double, 2> MovingImageType; typedef itk::ImageFileReader<FixedImageType> FixedImageReaderType; typedef itk::ImageFileReader<MovingImageType> MovingImageReaderType; typedef itk::Euler2DTransform< double > TransformType; typedef itk::ExhaustiveOptimizerv4< double > OptimizerType; typedef itk::MeanSquaresImageToImageMetricv4< FixedImageType, MovingImageType > MetricType; typedef itk::CenteredTransformInitializer< TransformType, FixedImageType, MovingImageType > TransformInitializerType; typedef itk::ImageRegistrationMethodv4< FixedImageType, MovingImageType, TransformType > RegistrationType;
FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New(); MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New(); FixedImageType::Pointer fixedImage = FixedImageType::New(); MovingImageType::Pointer movingImage = MovingImageType::New(); TransformType::Pointer transform = TransformType::New(); MetricType::Pointer metric = MetricType::New(); OptimizerType::Pointer optimizer = OptimizerType::New(); RegistrationType::Pointer registration = RegistrationType::New(); TransformInitializerType::Pointer initializer = TransformInitializerType::New();
fixedImageReader->SetFileName (argv[1]); fixedImageReader->Update(); fixedImage = fixedImageReader->GetOutput();
movingImageReader->SetFileName (argv[2]); movingImageReader->Update(); movingImage = movingImageReader->GetOutput();
// Create the Command observer and register it with the optimizer. // CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New(); optimizer->AddObserver( itk::IterationEvent(), observer );
unsigned int angles = 12; OptimizerType::StepsType steps( transform->GetNumberOfParameters() ); steps[0] = int(angles/2); steps[1] = 0; steps[2] = 0; optimizer->SetNumberOfSteps( steps );
OptimizerType::ScalesType scales( transform->GetNumberOfParameters() ); scales[0] = 2.0 * vnl_math::pi / angles; scales[1] = 1.0; scales[2] = 1.0;
optimizer->SetScales( scales );
initializer->SetTransform( transform ); initializer->SetFixedImage( fixedImage ); initializer->SetMovingImage( movingImage ); initializer->InitializeTransform();
// Initialize registration
registration->SetMetric( metric ); registration->SetOptimizer( optimizer ); registration->SetFixedImage( fixedImage ); registration->SetMovingImage( movingImage ); registration->SetInitialTransform( transform ); registration->SetNumberOfLevels( 1); try { registration->Update(); std::cout << " MinimumMetricValue: " << optimizer->GetMinimumMetricValue() << std::endl; std::cout << " MaximumMetricValue: " << optimizer->GetMaximumMetricValue() << std::endl; std::cout << " MinimumMetricValuePosition: " << optimizer->GetMinimumMetricValuePosition() << std::endl; std::cout << " MaximumMetricValuePosition: " << optimizer->GetMaximumMetricValuePosition() << std::endl; std::cout << " StopConditionDescription: " << optimizer->GetStopConditionDescription() << std::endl; } catch( itk::ExceptionObject & err ) { std::cerr << "ExceptionObject caught !" << std::endl; std::cerr << err << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS;
} </source>
CMakeLists.txt
<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)
project(ExhaustiveOptimizer)
find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED) include(${VTK_USE_FILE})
endif()
add_executable(ExhaustiveOptimizer MACOSX_BUNDLE ExhaustiveOptimizer.cxx)
if( "${ITK_VERSION_MAJOR}" LESS 4 )
target_link_libraries(ExhaustiveOptimizer ITKReview ${ITK_LIBRARIES})
else( "${ITK_VERSION_MAJOR}" LESS 4 )
target_link_libraries(ExhaustiveOptimizer ${ITK_LIBRARIES})
endif( "${ITK_VERSION_MAJOR}" LESS 4 )
</syntaxhighlight>
Download and Build ExhaustiveOptimizer
Click here to download ExhaustiveOptimizer and its CMakeLists.txt file. Once the tarball ExhaustiveOptimizer.tar has been downloaded and extracted,
cd ExhaustiveOptimizer/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:
./ExhaustiveOptimizer
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.