1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkPointSetToPointSetRegistrationTest.cxx,v $
Language: C++
Date: $Date: 2006-06-07 02:58:00 $
Version: $Revision: 1.7 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifdef _WIN32
#pragma warning ( disable : 4786 )
#endif
#include "itkTranslationTransform.h"
#include "itkEuclideanDistancePointMetric.h"
#include "itkLevenbergMarquardtOptimizer.h"
#include "itkPointSet.h"
#include "itkPointSetToPointSetRegistrationMethod.h"
#include "itkDanielssonDistanceMapImageFilter.h"
#include "itkPointSetToImageFilter.h"
#include <iostream>
/**
*
* This program tests the registration of a PointSet against an other PointSet.
*
*/
int itkPointSetToPointSetRegistrationTest(int, char* [] )
{
//------------------------------------------------------------
// Create two simple point sets
//------------------------------------------------------------
typedef itk::PointSet< float, 2 > FixedPointSetType;
FixedPointSetType::Pointer fixedPointSet = FixedPointSetType::New();
const unsigned int numberOfPoints = 500;
fixedPointSet->SetPointData( FixedPointSetType::PointDataContainer::New() );
fixedPointSet->GetPoints()->Reserve( numberOfPoints );
fixedPointSet->GetPointData()->Reserve( numberOfPoints );
FixedPointSetType::PointType point;
unsigned int id = 0;
for(unsigned int i=0;i<numberOfPoints/2;i++)
{
point[0]=0;
point[1]=i;
fixedPointSet->SetPoint( id++, point );
}
for(unsigned int i=0;i<numberOfPoints/2;i++)
{
point[0]=i;
point[1]=0;
fixedPointSet->SetPoint( id++, point );
}
// Moving Point Set
typedef itk::PointSet< float, 2 > MovingPointSetType;
MovingPointSetType::Pointer movingPointSet = MovingPointSetType::New();
movingPointSet->SetPointData( MovingPointSetType::PointDataContainer::New() );
movingPointSet->GetPoints()->Reserve( numberOfPoints );
movingPointSet->GetPointData()->Reserve( numberOfPoints );
id = 0;
for(unsigned int i=0;i<numberOfPoints/2;i++)
{
point[0]=0;
point[1]=i;
movingPointSet->SetPoint( id++, point );
}
for(unsigned int i=0;i<numberOfPoints/2;i++)
{
point[0]=i;
point[1]=0;
movingPointSet->SetPoint( id++, point );
}
//-----------------------------------------------------------
// Set up the Metric
//-----------------------------------------------------------
typedef itk::EuclideanDistancePointMetric<
FixedPointSetType,
MovingPointSetType>
MetricType;
typedef MetricType::TransformType TransformBaseType;
typedef TransformBaseType::ParametersType ParametersType;
typedef TransformBaseType::JacobianType JacobianType;
MetricType::Pointer metric = MetricType::New();
//-----------------------------------------------------------
// Set up a Transform
//-----------------------------------------------------------
typedef itk::TranslationTransform<
double,
2 > TransformType;
TransformType::Pointer transform = TransformType::New();
// Optimizer Type
typedef itk::LevenbergMarquardtOptimizer OptimizerType;
OptimizerType::Pointer optimizer = OptimizerType::New();
optimizer->SetUseCostFunctionGradient(false);
// Registration Method
typedef itk::PointSetToPointSetRegistrationMethod<
FixedPointSetType,
MovingPointSetType > RegistrationType;
RegistrationType::Pointer registration = RegistrationType::New();
// Scale the translation components of the Transform in the Optimizer
OptimizerType::ScalesType scales( transform->GetNumberOfParameters() );
scales.Fill( 1.0 );
unsigned long numberOfIterations = 100;
double gradientTolerance = 1e-1; // convergence criterion
double valueTolerance = 1e-1; // convergence criterion
double epsilonFunction = 1e-9; // convergence criterion
optimizer->SetScales( scales );
optimizer->SetNumberOfIterations( numberOfIterations );
optimizer->SetValueTolerance(valueTolerance);
optimizer->SetGradientTolerance(gradientTolerance);
optimizer->SetEpsilonFunction(epsilonFunction);
// Start from an Identity transform (in a normal case, the user
// can probably provide a better guess than the identity...
transform->SetIdentity();
registration->SetInitialTransformParameters( transform->GetParameters() );
//------------------------------------------------------
// Connect all the components required for Registration
//------------------------------------------------------
registration->SetMetric( metric );
registration->SetOptimizer( optimizer );
registration->SetTransform( transform );
registration->SetFixedPointSet( fixedPointSet );
registration->SetMovingPointSet( movingPointSet );
//------------------------------------------------------------
// Set up transform parameters
//------------------------------------------------------------
ParametersType parameters( transform->GetNumberOfParameters() );
// initialize the offset/vector part
for( unsigned int k = 0; k < 2; k++ )
{
parameters[k]= 10.0;
}
transform->SetParameters(parameters);
registration->SetInitialTransformParameters( transform->GetParameters() );
try
{
registration->StartRegistration();
}
catch( itk::ExceptionObject & e )
{
std::cout << e << std::endl;
return EXIT_FAILURE;
}
std::cout << "Solution = " << transform->GetParameters() << std::endl;
if((vnl_math_abs(transform->GetParameters()[0])>1.0)
||
(vnl_math_abs(transform->GetParameters()[1])>1.0)
)
{
return EXIT_FAILURE;
}
/** Test with the danielsson distance map */
typedef itk::Image<unsigned char,2> BinaryImageType;
typedef itk::Image<unsigned short,2> ImageType;
typedef itk::PointSetToImageFilter<FixedPointSetType,BinaryImageType> PSToImageFilterType;
PSToImageFilterType::Pointer psToImageFilter = PSToImageFilterType::New();
psToImageFilter->SetInput(fixedPointSet);
double origin[2] = {0.0, 0.0}, spacing[2] = {1.0, 1.0};
psToImageFilter->SetSpacing(spacing);
psToImageFilter->SetOrigin(origin);
std::cout << "Spacing and origin set: ["
<< psToImageFilter->GetSpacing() << "], ,["
<< psToImageFilter->GetOrigin() << "]" << std::endl;
psToImageFilter->Update();
std::cout << "psToImageFilter: " << psToImageFilter << std::endl;
BinaryImageType::Pointer binaryImage = psToImageFilter->GetOutput();
typedef itk::DanielssonDistanceMapImageFilter<BinaryImageType,ImageType> DDFilterType;
DDFilterType::Pointer ddFilter = DDFilterType::New();
ddFilter->SetInput(binaryImage);
ddFilter->Update();
metric->SetDistanceMap(ddFilter->GetOutput());
metric->ComputeSquaredDistanceOn();
// initialize the offset/vector part
for( unsigned int k = 0; k < 2; k++ )
{
parameters[k]= 10.0;
}
transform->SetParameters(parameters);
registration->SetInitialTransformParameters( transform->GetParameters() );
try
{
registration->StartRegistration();
}
catch( itk::ExceptionObject & e )
{
std::cout << e << std::endl;
return EXIT_FAILURE;
}
std::cout << "Solution = " << transform->GetParameters() << std::endl;
if((vnl_math_abs(transform->GetParameters()[0])>1.0)
||
(vnl_math_abs(transform->GetParameters()[1])>1.0)
)
{
return EXIT_FAILURE;
}
std::cout << "TEST DONE" << std::endl;
return EXIT_SUCCESS;
}
|