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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.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 notice for more information.
=========================================================================*/
/* perform a pixel-wise intensity transformation using a Normalized Correlation
* function */
#include "vtkVVPluginAPI.h"
#include "itkCenteredTransformInitializer.h"
#include "itkImage.h"
#include "itkImageRegistrationMethod.h"
#include "itkImportImageFilter.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkNormalizedCorrelationImageToImageMetric.h"
#include "itkVersorRigid3DTransform.h"
#include "itkVersorRigid3DTransformOptimizer.h"
#include "itkResampleImageFilter.h"
#include "itkShrinkImageFilter.h"
#include <cstdio> // sprintf()
// use itkRigid3DTransform instead?
// use VerserRigid3DTransform (not centered ?)
// use versor transform optimizer?
// =======================================================================
// The main class definition
// =======================================================================
template <class PixelType> class ImageRegistrationRunner
{
public:
// define our typedefs
typedef itk::Image< PixelType, 3 > ImageType;
typedef itk::ImportImageFilter< PixelType, 3> ImportFilterType;
typedef itk::ShrinkImageFilter<ImageType, ImageType> ShrinkFilterType;
typedef itk::VersorRigid3DTransform< double > TransformType;
typedef itk::VersorRigid3DTransformOptimizer OptimizerType;
typedef itk::NormalizedCorrelationImageToImageMetric<
ImageType, ImageType> MetricType;
typedef itk::LinearInterpolateImageFunction<ImageType, double>
InterpolatorType;
typedef itk::ImageRegistrationMethod< ImageType, ImageType> RegistrationType;
typedef itk::ResampleImageFilter< ImageType, ImageType > ResampleFilterType;
typedef itk::ImageRegion<3> RegionType;
typedef itk::Index<3> IndexType;
typedef itk::Size<3> SizeType;
// Command/Observer intended to update the progress
typedef itk::MemberCommand< ImageRegistrationRunner > CommandType;
typedef itk::CenteredTransformInitializer<TransformType,
ImageType, ImageType >
TransformInitializerType;
// Description:
// The funciton to call for progress of the optimizer
void ProgressUpdate( itk::Object * caller, const itk::EventObject & event );
// Description:
// The constructor
ImageRegistrationRunner();
// Description:
// Imports the two input images from Volview into ITK
virtual void ImportPixelBuffer( vtkVVPluginInfo *info,
const vtkVVProcessDataStruct * pds );
// Description:
// Copies the resulting data into the output image
virtual void CopyOutputData( vtkVVPluginInfo *info,
const vtkVVProcessDataStruct * pds );
// Description:
// Sets up the pipeline and invokes the registration process
int Execute( vtkVVPluginInfo *info, vtkVVProcessDataStruct *pds );
private:
// delare out instance variables
typename MetricType::Pointer m_Metric;
typename TransformType::Pointer m_Transform;
typename OptimizerType::Pointer m_Optimizer;
typename InterpolatorType::Pointer m_Interpolator;
typename RegistrationType::Pointer m_Registration;
typename ImportFilterType::Pointer m_ImportFilter;
typename ImportFilterType::Pointer m_ImportFilter2;
typename ResampleFilterType::Pointer m_Resample;
typename CommandType::Pointer m_CommandObserver;
vtkVVPluginInfo *m_Info;
};
// =======================================================================
// progress Callback
template <class PixelType>
void ImageRegistrationRunner<PixelType>::
ProgressUpdate( itk::Object * caller, const itk::EventObject & event )
{
char tstr[1024];
if( typeid( itk::IterationEvent ) == typeid( event ) )
{
if (m_Registration->GetFixedImage()->
GetLargestPossibleRegion().GetNumberOfPixels() > 0.03 *
m_ImportFilter->GetOutput()->
GetLargestPossibleRegion().GetNumberOfPixels())
{
sprintf(tstr,"Half Resolution Iteration : %i Value: %g",
m_Optimizer->GetCurrentIteration(),
m_Optimizer->GetValue());
}
else
{
sprintf(tstr,"Quarter Resolution Iteration : %i Value: %g",
m_Optimizer->GetCurrentIteration(),
m_Optimizer->GetValue());
}
m_Info->UpdateProgress(m_Info,
0.8*m_Optimizer->GetCurrentIteration()/
m_Optimizer->GetNumberOfIterations() , tstr);
}
if( typeid( itk::ProgressEvent ) == typeid( event ) )
{
m_Info->UpdateProgress(m_Info,0.8 + 0.2*m_Resample->GetProgress(),
"Resampling...");
}
}
// =======================================================================
// Constructor
template <class PixelType>
ImageRegistrationRunner<PixelType>::ImageRegistrationRunner()
{
m_CommandObserver = CommandType::New();
m_CommandObserver->SetCallbackFunction(
this, &ImageRegistrationRunner::ProgressUpdate );
m_ImportFilter = ImportFilterType::New();
m_ImportFilter2 = ImportFilterType::New();
m_Metric = MetricType::New();
m_Transform = TransformType::New();
m_Optimizer = OptimizerType::New();
m_Optimizer->MinimizeOn();
m_Interpolator = InterpolatorType::New();
m_Registration = RegistrationType::New();
m_Resample = ResampleFilterType::New();
m_Resample->AddObserver( itk::ProgressEvent(), m_CommandObserver );
m_Registration->SetMetric( m_Metric );
m_Registration->SetOptimizer( m_Optimizer );
m_Registration->SetTransform( m_Transform );
m_Registration->SetInterpolator( m_Interpolator );
m_Optimizer->AddObserver( itk::IterationEvent(), m_CommandObserver );
}
// =======================================================================
// Import data
template <class PixelType>
void ImageRegistrationRunner<PixelType>::
ImportPixelBuffer( vtkVVPluginInfo *info, const vtkVVProcessDataStruct * pds )
{
SizeType size;
IndexType start;
double origin[3];
double spacing[3];
size[0] = info->InputVolumeDimensions[0];
size[1] = info->InputVolumeDimensions[1];
size[2] = info->InputVolumeDimensions[2];
for(unsigned int i=0; i<3; i++)
{
origin[i] = info->InputVolumeOrigin[i];
spacing[i] = info->InputVolumeSpacing[i];
start[i] = 0;
}
RegionType region;
region.SetIndex( start );
region.SetSize( size );
m_ImportFilter->SetSpacing( spacing );
m_ImportFilter->SetOrigin( origin );
m_ImportFilter->SetRegion( region );
unsigned int totalNumberOfPixels = region.GetNumberOfPixels();
unsigned int numberOfComponents = info->InputVolumeNumberOfComponents;
unsigned int numberOfPixelsPerSlice = size[0] * size[1];
PixelType *dataBlockStart = static_cast< PixelType * >( pds->inData );
m_ImportFilter->SetImportPointer( dataBlockStart,
totalNumberOfPixels, false);
size[0] = info->InputVolume2Dimensions[0];
size[1] = info->InputVolume2Dimensions[1];
size[2] = info->InputVolume2Dimensions[2];
for(unsigned int i=0; i<3; i++)
{
origin[i] = info->InputVolume2Origin[i];
spacing[i] = info->InputVolume2Spacing[i];
start[i] = 0;
}
region.SetIndex( start );
region.SetSize( size );
m_ImportFilter2->SetSpacing( spacing );
m_ImportFilter2->SetOrigin( origin );
m_ImportFilter2->SetRegion( region );
totalNumberOfPixels = region.GetNumberOfPixels();
numberOfComponents = info->InputVolume2NumberOfComponents;
numberOfPixelsPerSlice = size[0] * size[1];
dataBlockStart = static_cast< PixelType * >( pds->inData2 );
m_ImportFilter2->SetImportPointer( dataBlockStart,
totalNumberOfPixels, false);
}
// =======================================================================
// Copy the output data into the volview data structure
template <class PixelType>
void ImageRegistrationRunner<PixelType>::
CopyOutputData( vtkVVPluginInfo *info, const vtkVVProcessDataStruct * pds )
{
// get some useful info
unsigned int numberOfComponents = info->OutputVolumeNumberOfComponents;
typedef itk::ImageRegionConstIterator< ImageType > OutputIteratorType;
PixelType * outData = static_cast< PixelType * >( pds->outData );
// do we append or replace
const char *result = info->GetGUIProperty(info, 1, VVP_GUI_VALUE);
if (result && !strcmp(result,"Append The Volumes"))
{
// Copy the data (with casting) to the output buffer
typename ImageType::ConstPointer fixedImage = m_ImportFilter->GetOutput();
OutputIteratorType ot( fixedImage, fixedImage->GetBufferedRegion() );
// copy the input image
ot.GoToBegin();
while( !ot.IsAtEnd() )
{
*outData = ot.Get();
++ot;
outData += numberOfComponents;
}
outData = static_cast< PixelType * >( pds->outData ) + 1;
}
// Copy the data (with casting) to the output buffer
typename ImageType::ConstPointer sampledImage = m_Resample->GetOutput();
OutputIteratorType ot2( sampledImage,
sampledImage->GetBufferedRegion() );
// copy the registered image
ot2.GoToBegin();
while( !ot2.IsAtEnd() )
{
*outData = ot2.Get();
++ot2;
outData += numberOfComponents;
}
}
// =======================================================================
// Main execute method
template <class PixelType>
int ImageRegistrationRunner<PixelType>::
Execute( vtkVVPluginInfo *info, vtkVVProcessDataStruct *pds )
{
m_Info = info;
m_Optimizer->SetNumberOfIterations(
atoi( info->GetGUIProperty(info, 0, VVP_GUI_VALUE )));
this->ImportPixelBuffer( info, pds );
// Execute the filter
m_ImportFilter->Update();
m_ImportFilter2->Update();
// first do it on reduced resolution images
typename ShrinkFilterType::Pointer shrink1 = ShrinkFilterType::New();
typename ShrinkFilterType::Pointer shrink2 = ShrinkFilterType::New();
shrink1->SetInput( m_ImportFilter->GetOutput() );
shrink1->SetShrinkFactors(4);
shrink1->Update();
shrink2->SetInput( m_ImportFilter2->GetOutput() );
shrink2->SetShrinkFactors(4);
shrink2->Update();
m_Registration->SetFixedImage( shrink1->GetOutput() );
m_Registration->SetMovingImage( shrink2->GetOutput() );
// setup the initializer
typename TransformInitializerType::Pointer initializer =
TransformInitializerType::New();
m_Transform->SetIdentity();
initializer->SetTransform( m_Transform );
initializer->SetFixedImage( shrink1->GetOutput() );
initializer->SetMovingImage( shrink2->GetOutput() );
initializer->MomentsOn();
initializer->InitializeTransform();
typename RegistrationType::ParametersType initParams =
m_Transform->GetParameters();
m_Registration->SetInitialTransformParameters( initParams );
typename OptimizerType::ScalesType optimizerScales(
m_Transform->GetNumberOfParameters());
optimizerScales[0] = 1.0;
optimizerScales[1] = 1.0;
optimizerScales[2] = 1.0;
optimizerScales[3] = 1.0/
(10.0*info->InputVolumeSpacing[0]*info->InputVolumeDimensions[0]);
optimizerScales[4] = 1.0/
(10.0*info->InputVolumeSpacing[1]*info->InputVolumeDimensions[1]);
optimizerScales[5] = 1.0/
(10.0*info->InputVolumeSpacing[2]*info->InputVolumeDimensions[2]);
m_Optimizer->SetScales(optimizerScales);
m_Optimizer->SetMaximumStepLength(1.0);
m_Optimizer->SetMinimumStepLength(0.01);
info->UpdateProgress(info,0.0,"Starting Registration ...");
try
{
m_Registration->StartRegistration();
}
catch( itk::ExceptionObject )
{
return 1;
}
// if we converged without using all of the iterations then increase he
// resolution to a half resolution volume and continue
int totalIterations = m_Optimizer->GetCurrentIteration();
if (m_Optimizer->GetCurrentIteration() <
m_Optimizer->GetNumberOfIterations())
{
info->UpdateProgress(info,0.8*m_Optimizer->GetCurrentIteration()/
m_Optimizer->GetNumberOfIterations(),
"Starting Half Resolution Registration ...");
shrink1->SetShrinkFactors(2);
shrink1->Update();
shrink2->SetShrinkFactors(2);
shrink2->Update();
m_Registration->SetInitialTransformParameters(
m_Registration->GetLastTransformParameters() );
m_Optimizer->SetMaximumStepLength(0.2);
m_Optimizer->SetMinimumStepLength(0.002);
m_Optimizer->SetNumberOfIterations(
atoi( info->GetGUIProperty(info, 0, VVP_GUI_VALUE )) -
m_Optimizer->GetCurrentIteration());
try
{
m_Registration->StartRegistration();
}
catch( itk::ExceptionObject )
{
return 1;
}
totalIterations += m_Optimizer->GetCurrentIteration();
}
// now get the resulting parameters
typename RegistrationType::ParametersType finalParameters =
m_Registration->GetLastTransformParameters();
typename TransformType::Pointer finalTransform = TransformType::New();
finalTransform->SetParameters( finalParameters );
finalTransform->SetCenter(m_Transform->GetCenter());
m_Resample->SetTransform( finalTransform );
m_Resample->SetInput( m_ImportFilter2->GetOutput() );
m_Resample->SetSize(
m_ImportFilter->GetOutput()->GetLargestPossibleRegion().GetSize());
m_Resample->SetOutputOrigin( m_ImportFilter->GetOutput()->GetOrigin() );
m_Resample->SetOutputSpacing( m_ImportFilter->GetOutput()->GetSpacing());
m_Resample->SetDefaultPixelValue(0);
info->UpdateProgress(info,0.8,"Starting Resample ...");
m_Resample->Update();
this->CopyOutputData( info, pds );
// set some output information,
char results[1024];
typedef TransformType::VersorType VersorType;
VersorType versor = finalTransform->GetVersor();
TransformType::OffsetType offset = finalTransform->GetOffset();
typedef VersorType::VectorType AxisType;
AxisType axis = versor.GetAxis();
sprintf(results,"Number of Iterations Used: %d\nTranslation: %g %g %g\nRotation Axis %f %f %f %f\nOffset: %g %g %g",
m_Optimizer->GetCurrentIteration(),
finalParameters[3],
finalParameters[4],
finalParameters[5],
axis[0],
axis[1],
axis[2],
versor.GetAngle(),
offset[0],
offset[1],
offset[2]
);
info->SetProperty(info, VVP_REPORT_TEXT, results);
return 0;
}
static int ProcessData(void *inf, vtkVVProcessDataStruct *pds)
{
vtkVVPluginInfo *info = (vtkVVPluginInfo *)inf;
// do some error checking
if (info->InputVolumeScalarType != info->InputVolume2ScalarType)
{
info->SetProperty(
info, VVP_ERROR,
"The two inputs do not appear to be of the same data type.");
return 1;
}
if (info->InputVolumeNumberOfComponents != 1 ||
info->InputVolume2NumberOfComponents != 1)
{
info->SetProperty(
info, VVP_ERROR, "The two input volumes must be single component.");
return 1;
}
int result = 0;
try
{
switch( info->InputVolumeScalarType )
{
case VTK_CHAR:
{
ImageRegistrationRunner<signed char> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_UNSIGNED_CHAR:
{
ImageRegistrationRunner<unsigned char> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_SHORT:
{
ImageRegistrationRunner<signed short> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_UNSIGNED_SHORT:
{
ImageRegistrationRunner<unsigned short> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_INT:
{
ImageRegistrationRunner<signed int> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_UNSIGNED_INT:
{
ImageRegistrationRunner<unsigned int> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_LONG:
{
ImageRegistrationRunner<signed long> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_UNSIGNED_LONG:
{
ImageRegistrationRunner<unsigned long> runner;
result = runner.Execute( info, pds );
break;
}
case VTK_FLOAT:
{
ImageRegistrationRunner<float> runner;
result = runner.Execute( info, pds );
break;
}
}
}
catch( itk::ExceptionObject & except )
{
info->SetProperty( info, VVP_ERROR, except.what() );
return -1;
}
return result;
}
static int UpdateGUI(void *inf)
{
char tmp[1024];
vtkVVPluginInfo *info = (vtkVVPluginInfo *)inf;
info->SetGUIProperty(info, 0, VVP_GUI_LABEL, "Maximum Total Iterations");
info->SetGUIProperty(info, 0, VVP_GUI_TYPE, VVP_GUI_SCALE);
info->SetGUIProperty(info, 0, VVP_GUI_DEFAULT, "30");
info->SetGUIProperty(info, 0, VVP_GUI_HELP, "How many iterations to run for the ");
info->SetGUIProperty(info, 0, VVP_GUI_HINTS , "20 300 1");
info->SetGUIProperty(info, 1, VVP_GUI_LABEL, "Output Format");
info->SetGUIProperty(info, 1, VVP_GUI_TYPE, VVP_GUI_CHOICE);
info->SetGUIProperty(info, 1, VVP_GUI_DEFAULT , "Append The Volumes");
info->SetGUIProperty(info, 1, VVP_GUI_HELP,
"How do you want the output stored? There are two choices here. Appending creates a single output volume that has two components, the first component from the input volume and the second component is from the registered second input. The second choice is to Relace the current volume. In this case the Registered second input replaces the original volume.");
info->SetGUIProperty(info, 1, VVP_GUI_HINTS, "2\nAppend The Volumes\nReplace The Current Volume");
info->OutputVolumeScalarType = info->InputVolumeScalarType;
memcpy(info->OutputVolumeDimensions,info->InputVolumeDimensions,
3*sizeof(int));
memcpy(info->OutputVolumeSpacing,info->InputVolumeSpacing,
3*sizeof(float));
memcpy(info->OutputVolumeOrigin,info->InputVolumeOrigin,
3*sizeof(float));
// really the memory consumption is one copy of the resampled output for
// the resample filter plus the gradient for the 1/8th res volume plus the
// two 1/8th res resampled inputs
sprintf(tmp,"%f",
info->InputVolumeScalarSize + 3.0*sizeof(float)/8.0 + 0.5);
info->SetProperty(info, VVP_PER_VOXEL_MEMORY_REQUIRED, tmp);
// what output format is selected
const char *result = info->GetGUIProperty(info, 1, VVP_GUI_VALUE);
if (result && !strcmp(result,"Append The Volumes"))
{
info->OutputVolumeNumberOfComponents =
info->InputVolumeNumberOfComponents +
info->InputVolume2NumberOfComponents;
}
else
{
info->OutputVolumeNumberOfComponents =
info->InputVolume2NumberOfComponents;
}
return 1;
}
extern "C" {
void VV_PLUGIN_EXPORT vvITKImageRegistrationInit(vtkVVPluginInfo *info)
{
vvPluginVersionCheck();
// setup information that never changes
info->ProcessData = ProcessData;
info->UpdateGUI = UpdateGUI;
info->SetProperty(info, VVP_NAME, "Correlation based Registration: Rigid");
info->SetProperty(info, VVP_GROUP, "Registration");
info->SetProperty(info, VVP_TERSE_DOCUMENTATION,
"Register two images using Normalized Correlation metric.");
info->SetProperty(info, VVP_FULL_DOCUMENTATION,
"This filter takes two volumes and registers them. There are two choices for the output format. Appending creates a single output volume that has two components, the first component is from the input volume and the second component is from the registered and resampled second input volume. The second choice is to Replace the current volume. In this case the registered and resampled second input replaces the original volume. The two input volumes must have one component and be of the same data type. The registration is done on quarter resolution volumes first (one quarter on each axis) and then if that converges the registration continues with one half resolution volumes. The optimization is done using a regular gradient descent optimizer with a centered quaternion and rigid translation based transform. The error metric is a normalized correlation metric.");
info->SetProperty(info, VVP_SUPPORTS_IN_PLACE_PROCESSING, "0");
info->SetProperty(info, VVP_SUPPORTS_PROCESSING_PIECES, "0");
info->SetProperty(info, VVP_NUMBER_OF_GUI_ITEMS, "2");
info->SetProperty(info, VVP_REQUIRED_Z_OVERLAP, "0");
info->SetProperty(info, VVP_PER_VOXEL_MEMORY_REQUIRED, "0");
info->SetProperty(info, VVP_REQUIRES_SECOND_INPUT, "1");
info->SetProperty(info, VVP_REQUIRES_SERIES_INPUT, "0");
info->SetProperty(info, VVP_SUPPORTS_PROCESSING_SERIES_BY_VOLUMES, "0");
info->SetProperty(info, VVP_PRODUCES_OUTPUT_SERIES, "0");
info->SetProperty(info, VVP_PRODUCES_PLOTTING_OUTPUT, "0");
}
}
|