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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkMultiResolutionPDEDeformableRegistration.txx,v $
Language: C++
Date: $Date: 2007-07-31 23:12:20 $
Version: $Revision: 1.28 $
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.
=========================================================================*/
#ifndef _itkMultiResolutionPDEDeformableRegistration_txx
#define _itkMultiResolutionPDEDeformableRegistration_txx
#include "itkMultiResolutionPDEDeformableRegistration.h"
#include "itkRecursiveMultiResolutionPyramidImageFilter.h"
#include "itkImageRegionIterator.h"
#include "vnl/vnl_math.h"
namespace itk {
/*
* Default constructor
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::MultiResolutionPDEDeformableRegistration()
{
this->SetNumberOfRequiredInputs(2);
typename DefaultRegistrationType::Pointer registrator =
DefaultRegistrationType::New();
m_RegistrationFilter = static_cast<RegistrationType*>(
registrator.GetPointer() );
m_MovingImagePyramid = MovingImagePyramidType::New();
m_FixedImagePyramid = FixedImagePyramidType::New();
m_FieldExpander = FieldExpanderType::New();
m_InitialDeformationField = NULL;
m_NumberOfLevels = 3;
m_NumberOfIterations.resize( m_NumberOfLevels );
m_FixedImagePyramid->SetNumberOfLevels( m_NumberOfLevels );
m_MovingImagePyramid->SetNumberOfLevels( m_NumberOfLevels );
unsigned int ilevel;
for( ilevel = 0; ilevel < m_NumberOfLevels; ilevel++ )
{
m_NumberOfIterations[ilevel] = 10;
}
m_CurrentLevel = 0;
m_StopRegistrationFlag = false;
}
/*
* Set the moving image image.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::SetMovingImage(
const MovingImageType * ptr )
{
this->ProcessObject::SetNthInput( 2, const_cast< MovingImageType * >( ptr ) );
}
/*
* Get the moving image image.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
const typename MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::MovingImageType *
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::GetMovingImage(void) const
{
return dynamic_cast< const MovingImageType * >
( this->ProcessObject::GetInput( 2 ) );
}
/*
* Set the fixed image.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::SetFixedImage(
const FixedImageType * ptr )
{
this->ProcessObject::SetNthInput( 1, const_cast< FixedImageType * >( ptr ) );
}
/*
* Get the fixed image.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
const typename MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::FixedImageType *
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::GetFixedImage(void) const
{
return dynamic_cast< const FixedImageType * >
( this->ProcessObject::GetInput( 1 ) );
}
/*
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
std::vector<SmartPointer<DataObject> >::size_type
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::GetNumberOfValidRequiredInputs() const
{
typename std::vector<SmartPointer<DataObject> >::size_type num = 0;
if (this->GetFixedImage())
{
num++;
}
if (this->GetMovingImage())
{
num++;
}
return num;
}
/*
* Set the number of multi-resolution levels
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::SetNumberOfLevels(
unsigned int num )
{
if( m_NumberOfLevels != num )
{
this->Modified();
m_NumberOfLevels = num;
m_NumberOfIterations.resize( m_NumberOfLevels );
}
if( m_MovingImagePyramid && m_MovingImagePyramid->GetNumberOfLevels() != num )
{
m_MovingImagePyramid->SetNumberOfLevels( m_NumberOfLevels );
}
if( m_FixedImagePyramid && m_FixedImagePyramid->GetNumberOfLevels() != num )
{
m_FixedImagePyramid->SetNumberOfLevels( m_NumberOfLevels );
}
}
/*
* Standard PrintSelf method.
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "NumberOfLevels: " << m_NumberOfLevels << std::endl;
os << indent << "CurrentLevel: " << m_CurrentLevel << std::endl;
os << indent << "NumberOfIterations: [";
unsigned int ilevel;
for( ilevel = 0; ilevel < m_NumberOfLevels - 1; ilevel++ )
{
os << m_NumberOfIterations[ilevel] << ", ";
}
os << m_NumberOfIterations[ilevel] << "]" << std::endl;
os << indent << "RegistrationFilter: ";
os << m_RegistrationFilter.GetPointer() << std::endl;
os << indent << "MovingImagePyramid: ";
os << m_MovingImagePyramid.GetPointer() << std::endl;
os << indent << "FixedImagePyramid: ";
os << m_FixedImagePyramid.GetPointer() << std::endl;
os << indent << "StopRegistrationFlag: ";
os << m_StopRegistrationFlag << std::endl;
}
/*
* Perform a the deformable registration using a multiresolution scheme
* using an internal mini-pipeline
*
* ref_pyramid -> registrator -> field_expander --|| tempField
* test_pyramid -> | |
* | |
* --------------------------------
*
* A tempField image is used to break the cycle between the
* registrator and field_expander.
*
*/
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::GenerateData()
{
//DeformationFieldPointer NullDeformationFieldPointer = DeformationFieldType::New();
// Check for NULL images and pointers
MovingImageConstPointer movingImage = this->GetMovingImage();
FixedImageConstPointer fixedImage = this->GetFixedImage();
if( !movingImage || !fixedImage )
{
itkExceptionMacro( << "Fixed and/or moving image not set" );
}
if( !m_MovingImagePyramid || !m_FixedImagePyramid )
{
itkExceptionMacro( << "Fixed and/or moving pyramid not set" );
}
if( !m_RegistrationFilter )
{
itkExceptionMacro( << "Registration filter not set" );
}
// Create the image pyramids.
m_MovingImagePyramid->SetInput( movingImage );
m_MovingImagePyramid->UpdateLargestPossibleRegion();
m_FixedImagePyramid->SetInput( fixedImage );
m_FixedImagePyramid->UpdateLargestPossibleRegion();
// Initializations
m_CurrentLevel = 0;
m_StopRegistrationFlag = false;
unsigned int movingLevel = vnl_math_min( (int) m_CurrentLevel,
(int) m_MovingImagePyramid->GetNumberOfLevels() );
unsigned int fixedLevel = vnl_math_min( (int) m_CurrentLevel,
(int) m_FixedImagePyramid->GetNumberOfLevels() );
DeformationFieldPointer tempField = this->m_InitialDeformationField;
bool lastShrinkFactorsAllOnes = false;
while ( !this->Halt() )
{
if( tempField.IsNull() )
{
m_RegistrationFilter->SetInitialDeformationField( NULL );
}
else
{
// Resample the field to be the same size as the fixed image
// at the current level
m_FieldExpander->SetInput( tempField );
typename FloatImageType::Pointer fi =
m_FixedImagePyramid->GetOutput( fixedLevel );
m_FieldExpander->SetSize(
fi->GetLargestPossibleRegion().GetSize() );
m_FieldExpander->SetOutputStartIndex(
fi->GetLargestPossibleRegion().GetIndex() );
m_FieldExpander->SetOutputOrigin( fi->GetOrigin() );
m_FieldExpander->SetOutputSpacing( fi->GetSpacing());
m_FieldExpander->UpdateLargestPossibleRegion();
m_FieldExpander->SetInput( NULL );
tempField = m_FieldExpander->GetOutput();
tempField->DisconnectPipeline();
m_RegistrationFilter->SetInitialDeformationField( tempField );
}
// setup registration filter and pyramids
m_RegistrationFilter->SetMovingImage( m_MovingImagePyramid->GetOutput(movingLevel) );
m_RegistrationFilter->SetFixedImage( m_FixedImagePyramid->GetOutput(fixedLevel) );
m_RegistrationFilter->SetNumberOfIterations(
m_NumberOfIterations[m_CurrentLevel] );
// cache shrink factors for computing the next expand factors.
lastShrinkFactorsAllOnes = true;
for( unsigned int idim = 0; idim < ImageDimension; idim++ )
{
if ( m_FixedImagePyramid->GetSchedule()[fixedLevel][idim] > 1 )
{
lastShrinkFactorsAllOnes = false;
break;
}
}
// compute new deformation field
m_RegistrationFilter->UpdateLargestPossibleRegion();
tempField = m_RegistrationFilter->GetOutput();
tempField->DisconnectPipeline();
// Increment level counter.
m_CurrentLevel++;
movingLevel = vnl_math_min( (int) m_CurrentLevel,
(int) m_MovingImagePyramid->GetNumberOfLevels() );
fixedLevel = vnl_math_min( (int) m_CurrentLevel,
(int) m_FixedImagePyramid->GetNumberOfLevels() );
// Invoke an iteration event.
this->InvokeEvent( IterationEvent() );
// We can release data from pyramid which are no longer required.
if ( movingLevel > 0 )
{
m_MovingImagePyramid->GetOutput( movingLevel - 1 )->ReleaseData();
}
if( fixedLevel > 0 )
{
m_FixedImagePyramid->GetOutput( fixedLevel - 1 )->ReleaseData();
}
} // while not Halt()
if( !lastShrinkFactorsAllOnes )
{
// Some of the last shrink factors are not one
// graft the output of the expander filter to
// to output of this filter
// resample the field to the same size as the fixed image
m_FieldExpander->SetInput( tempField );
m_FieldExpander->SetSize(
fixedImage->GetLargestPossibleRegion().GetSize() );
m_FieldExpander->SetOutputStartIndex(
fixedImage->GetLargestPossibleRegion().GetIndex() );
m_FieldExpander->SetOutputOrigin( fixedImage->GetOrigin() );
m_FieldExpander->SetOutputSpacing( fixedImage->GetSpacing());
m_FieldExpander->UpdateLargestPossibleRegion();
this->GraftOutput( m_FieldExpander->GetOutput() );
}
else
{
// all the last shrink factors are all ones
// graft the output of registration filter to
// to output of this filter
this->GraftOutput( tempField );
}
// Release memory
m_FieldExpander->SetInput( NULL );
m_FieldExpander->GetOutput()->ReleaseData();
m_RegistrationFilter->SetInput( NULL );
m_RegistrationFilter->GetOutput()->ReleaseData();
}
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::StopRegistration()
{
m_RegistrationFilter->StopRegistration();
m_StopRegistrationFlag = true;
}
template <class TFixedImage, class TMovingImage, class TDeformationField>
bool
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::Halt()
{
// Halt the registration after the user-specified number of levels
if (m_NumberOfLevels != 0)
{
this->UpdateProgress( static_cast<float>( m_CurrentLevel ) /
static_cast<float>( m_NumberOfLevels ) );
}
if ( m_CurrentLevel >= m_NumberOfLevels )
{
return true;
}
if ( m_StopRegistrationFlag )
{
return true;
}
else
{
return false;
}
}
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::GenerateOutputInformation()
{
typename DataObject::Pointer output;
if( this->GetInput(0) )
{
// Initial deformation field is set.
// Copy information from initial field.
this->Superclass::GenerateOutputInformation();
}
else if( this->GetFixedImage() )
{
// Initial deforamtion field is not set.
// Copy information from the fixed image.
for (unsigned int idx = 0; idx <
this->GetNumberOfOutputs(); ++idx )
{
output = this->GetOutput(idx);
if (output)
{
output->CopyInformation(this->GetFixedImage());
}
}
}
}
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::GenerateInputRequestedRegion()
{
// call the superclass's implementation
Superclass::GenerateInputRequestedRegion();
// request the largest possible region for the moving image
MovingImagePointer movingPtr =
const_cast< MovingImageType * >( this->GetMovingImage() );
if( movingPtr )
{
movingPtr->SetRequestedRegionToLargestPossibleRegion();
}
// just propagate up the output requested region for
// the fixed image and initial deformation field.
DeformationFieldPointer inputPtr =
const_cast< DeformationFieldType * >( this->GetInput() );
DeformationFieldPointer outputPtr = this->GetOutput();
FixedImagePointer fixedPtr =
const_cast< FixedImageType *>( this->GetFixedImage() );
if( inputPtr )
{
inputPtr->SetRequestedRegion( outputPtr->GetRequestedRegion() );
}
if( fixedPtr )
{
fixedPtr->SetRequestedRegion( outputPtr->GetRequestedRegion() );
}
}
template <class TFixedImage, class TMovingImage, class TDeformationField>
void
MultiResolutionPDEDeformableRegistration<TFixedImage,TMovingImage,TDeformationField>
::EnlargeOutputRequestedRegion(
DataObject * ptr )
{
// call the superclass's implementation
Superclass::EnlargeOutputRequestedRegion( ptr );
// set the output requested region to largest possible.
DeformationFieldType * outputPtr;
outputPtr = dynamic_cast<DeformationFieldType*>( ptr );
if( outputPtr )
{
outputPtr->SetRequestedRegionToLargestPossibleRegion();
}
}
} // end namespace itk
#endif
|