📄 itkmultiresolutionimageregistrationmethodtest_1.cxx
字号:
************************************************************/
RegistrationType::ParametersType solution =
registration->GetLastTransformParameters();
std::cout << "Solution is: " << solution << std::endl;
RegistrationType::ParametersType trueParameters(
transform->GetNumberOfParameters() );
trueParameters.Fill( 0.0 );
trueParameters[ 0] = 1/scale[0];
trueParameters[ 4] = 1/scale[1];
trueParameters[ 8] = 1/scale[2];
trueParameters[ 9] = - displacement[0]/scale[0];
trueParameters[10] = - displacement[1]/scale[1];
trueParameters[11] = - displacement[2]/scale[2];
std::cout << "True solution is: " << trueParameters << std::endl;
for( j = 0; j < 9; j++ )
{
if( vnl_math_abs( solution[j] - trueParameters[j] ) > 0.025 )
{
pass = false;
}
}
for( j = 9; j < 12; j++ )
{
if( vnl_math_abs( solution[j] - trueParameters[j] ) > 1.0 )
{
pass = false;
}
}
if( !pass )
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
//store the schedules for fixed and moving images. These schedules will be
//used by the second registration run.
fixedImageSchedule = registration->GetFixedImagePyramid()->GetSchedule();
movingImageSchedule = registration->GetMovingImagePyramid()->GetSchedule();
/*************************************************
* Check for parzen window exception
**************************************************/
double oldValue = metric->GetMovingImageStandardDeviation();
metric->SetMovingImageStandardDeviation( 0.005 );
try
{
pass = false;
registration->StartRegistration();
}
catch(itk::ExceptionObject& err)
{
std::cout << "Caught expected ExceptionObject" << std::endl;
std::cout << err << std::endl;
pass = true;
}
if( !pass )
{
std::cout << "Should have caught an exception" << std::endl;
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
metric->SetMovingImageStandardDeviation( oldValue );
/*************************************************
* Check for mapped out of image error
**************************************************/
solution[5] = 1000;
registration->SetInitialTransformParameters( solution );
try
{
pass = false;
registration->StartRegistration();
}
catch(itk::ExceptionObject& err)
{
std::cout << "Caught expected ExceptionObject" << std::endl;
std::cout << err << std::endl;
pass = true;
}
if( !pass )
{
std::cout << "Should have caught an exception" << std::endl;
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
/* To avoid confusion, SetNumberOfLevels and SetSchedules are not allowed to be
* used together. An exception is thrown if SetSchedules() is invoked after
* invoking SetNumberOfLevels */
try
{
registration->SetNumberOfLevels( numberOfLevels );
registration->SetSchedules( fixedImageSchedule, movingImageSchedule );
pass=false;
}
catch( itk::ExceptionObject & e )
{
std::cout << "Expected exception is thrown since we tried to set schedules after"
<< " setting the number of levels" << std::endl;
std::cout << "Reason " << e.GetDescription() << std::endl;
}
if( !pass )
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
}
/* The second registration uses user defined schedules. For testing purpose, we
* will use the schedules internally generated in the first registration run
* by the fixed and moving image after the number of levels is set. The final
* registration transform parameter values should remain the same*/
{
MetricType::Pointer metric = MetricType::New();
TransformType::Pointer transform = TransformType::New();
OptimizerType::Pointer optimizer = OptimizerType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
FixedImagePyramidType::Pointer fixedImagePyramid =
FixedImagePyramidType::New();
MovingImagePyramidType::Pointer movingImagePyramid =
MovingImagePyramidType::New();
RegistrationType::Pointer registration = RegistrationType::New();
/******************************************************************
* Set up the optimizer.
******************************************************************/
// set the translation scale
typedef OptimizerType::ScalesType ScalesType;
ScalesType parametersScales( transform->GetNumberOfParameters() );
parametersScales.Fill( 1.0 );
for ( j = 9; j < 12; j++ )
{
parametersScales[j] = 0.0001;
}
optimizer->SetScales( parametersScales );
// need to maximize for mutual information
optimizer->MaximizeOn();
/******************************************************************
* Set up the metric.
******************************************************************/
metric->SetMovingImageStandardDeviation( 5.0 );
metric->SetFixedImageStandardDeviation( 5.0 );
metric->SetNumberOfSpatialSamples( 50 );
/******************************************************************
* Set up the registrator.
******************************************************************/
// connect up the components
registration->SetMetric( metric );
registration->SetOptimizer( optimizer );
registration->SetTransform( transform );
registration->SetFixedImage( fixedImage );
registration->SetMovingImage( movingImage );
registration->SetInterpolator( interpolator );
registration->SetFixedImagePyramid( fixedImagePyramid );
registration->SetMovingImagePyramid( movingImagePyramid );
registration->SetFixedImageRegion( fixedImage->GetBufferedRegion() );
// set initial parameters to identity
RegistrationType::ParametersType initialParameters(
transform->GetNumberOfParameters() );
initialParameters.Fill( 0.0 );
initialParameters[0] = 1.0;
initialParameters[4] = 1.0;
initialParameters[8] = 1.0;
/******************************************************************
* Attach registration to a simple UI and run registration
******************************************************************/
SimpleMultiResolutionImageRegistrationUI2<RegistrationType>
simpleUI( registration );
unsigned short numberOfLevels = 3;
itk::Array<unsigned int> niter( numberOfLevels );
itk::Array<double> rates( numberOfLevels );
niter[0] = 100;
niter[1] = 300;
niter[2] = 550;
rates[0] = 1e-3;
rates[1] = 5e-4;
rates[2] = 1e-4;
simpleUI.SetNumberOfIterations( niter );
simpleUI.SetLearningRates( rates );
try
{
metric->ReinitializeSeed( 121212 );
registration->SetSchedules( fixedImageSchedule, movingImageSchedule);
registration->SetInitialTransformParameters( initialParameters );
registration->StartRegistration();
}
catch( itk::ExceptionObject & e )
{
std::cout << "Registration failed" << std::endl;
std::cout << "Reason " << e.GetDescription() << std::endl;
return EXIT_FAILURE;
}
/***********************************************************
* Check the results
************************************************************/
RegistrationType::ParametersType solution =
registration->GetLastTransformParameters();
std::cout << "Solution is: " << solution << std::endl;
RegistrationType::ParametersType trueParameters(
transform->GetNumberOfParameters() );
trueParameters.Fill( 0.0 );
trueParameters[ 0] = 1/scale[0];
trueParameters[ 4] = 1/scale[1];
trueParameters[ 8] = 1/scale[2];
trueParameters[ 9] = - displacement[0]/scale[0];
trueParameters[10] = - displacement[1]/scale[1];
trueParameters[11] = - displacement[2]/scale[2];
std::cout << "True solution is: " << trueParameters << std::endl;
for( j = 0; j < 9; j++ )
{
if( vnl_math_abs( solution[j] - trueParameters[j] ) > 0.025 )
{
pass = false;
}
}
for( j = 9; j < 12; j++ )
{
if( vnl_math_abs( solution[j] - trueParameters[j] ) > 1.0 )
{
pass = false;
}
}
if( !pass )
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
}
namespace
{
/**
* This function defines the test image pattern.
* The pattern is a 3D gaussian in the middle
* and some directional pattern on the outside.
*/
double F( itk::Vector<double,3> & v )
{
double x = v[0];
double y = v[1];
double z = v[2];
const double s = 50;
double value = 200.0 * exp( - ( x*x + y*y + z*z )/(s*s) );
x -= 8; y += 3; z += 0;
double r = vcl_sqrt( x*x + y*y + z*z );
if( r > 35 )
{
value = 2 * ( vnl_math_abs( x ) +
0.8 * vnl_math_abs( y ) +
0.5 * vnl_math_abs( z ) );
}
if( r < 4 )
{
value = 400;
}
return value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -