[Insight-users] MRIRegistration example has problems

cspl affable at hd2 . dot . net . in
Mon, 29 Jul 2002 20:21:21 +0530


Dear Friends,
 I am working on MRIRegistration example given with ITK.When I 

execute the application, It is giving an Error. I found that 'fixed' 

and 'moving' imges are not getting updated .When I tried to update 

those images using UPDATE(),I got runtime error "Debug error".When I 

removed this update statement, I got problem at Start 

Registration().Error is not informative.It is showing only "Debug 

Error" Please send me some information regarding method and setting 

registration parameters. I am include a part of the code below.
 

#include <iostream>
#include <fstream>
#include <afxwin.h>

#include "vtk2itk2vtk.h"

// vtk classes
#include "vtkImageReader.h"
#include "vtkImageExport.h"
#include "vtkTransform.h"

// itk classes
#include "itkExceptionObject.h"
#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkNormalizeImageFilter.h"
#include "itkChangeInformationImageFilter.h"
#include "itkShrinkImageFilter.h"
#include "itkQuaternionRigidTransform.h"
#include "itkQuaternionRigidTransformGradientDescentOptimizer.h"
#include "itkMutualInformationImageToImageMetric.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkImageRegistrationMethod.h"
#include "vnl/vnl_math.h"
#include "OptionList.h"

void print_usage();

int main(int argc, char **argv)
{
  unsigned int sf[3];
  if (argc < 2)
    {
    print_usage();
    exit(1);
    }

  // Process Options
  OptionList options(argc, argv) ;
  std::string study1Prefix;
  std::vector<int> study1Resolution(3);
  
  std::vector<double> study1Spacing(3);
 
  std::string study2Prefix;
  std::vector<int> study2Resolution(3);
  
  std::vector<double> study2Spacing(3);
  std::vector<int> shrinkFactors;
  float translateScale;
  float standardDeviation;
  float learningRate;
  int numberOfIterations;
  int numberOfSamples;
  try
    {
    options.GetStringOption("study1Prefix", &study1Prefix, true);
    options.GetMultiDoubleOption("study1Spacing", &study1Spacing, 

true);
    options.GetMultiIntOption("study1Resolution", &study1Resolution, 

true);
    options.GetStringOption("study2Prefix", &study2Prefix, true);
    options.GetMultiDoubleOption("study2Spacing", &study2Spacing, 

true);
    options.GetMultiIntOption("study2Resolution", &study2Resolution, 

true);
    options.GetMultiIntOption("shrink", &shrinkFactors, true);
    translateScale = options.GetDoubleOption("translateScale", true);
    standardDeviation = options.GetDoubleOption("standardDeviation", 

true);
    learningRate = options.GetDoubleOption("learningRate", true);
    numberOfIterations = options.GetIntOption("numberOfIterations", 

true);
    numberOfSamples = options.GetIntOption("numberOfSamples", true);
    }
  catch(OptionList::RequiredOptionMissing e)
    {
    std::cerr << "Error: The '" << e.OptionTag
              << "' option is required but missing." 
              << std::endl ;
    exit(0) ;
    }
  
  // Declare all the filters
  typedef itk::Image<unsigned short,3> InputType;
  typedef itk::Image<float,3> OutputType;
  typedef itk::NormalizeImageFilter<InputType,OutputType> 

NormalizeFilter;
  typedef itk::ChangeInformationImageFilter<OutputType> 

ChangeInformationFilter;
  typedef itk::ShrinkImageFilter<OutputType,OutputType> ShrinkFilter;

  // Registration types
  typedef itk::QuaternionRigidTransform<double> TransformType;
  typedef itk::QuaternionRigidTransformGradientDescentOptimizer 

OptimizerType;
  typedef itk::MutualInformationImageToImageMetric<OutputType, 

OutputType> MetricType;
  typedef itk:: LinearInterpolateImageFunction<OutputType, double> 

InterpolatorType;
  typedef itk::ImageRegistrationMethod<OutputType,OutputType> 

RegistrationType;


  // Moving ----------------
  vtkImageReader *movingReader = vtkImageReader::New();
    movingReader->SetDataScalarTypeToUnsignedShort();
    movingReader->SetFilePrefix(study1Prefix.c_str());
  //"E:\\outprojects\\Registration\\Images\\");
    movingReader->SetDataByteOrderToBigEndian();
    movingReader->SetDataExtent(0,study1Resolution[0] - 

1,0,study1Resolution[1] - 1,1,study1Resolution[2]);// 

study1Resolution[0] - 1,
                                
    

movingReader->SetDataSpacing(study1Spacing[0],study1Spacing[1],study1Sp

acing[2]);//study1Spacing[0],
                                 
  vtkImageExport *movingVtkExporter = vtkImageExport::New();
    movingVtkExporter->SetInput(movingReader->GetOutput());

  typedef itk::VTKImageImport<InputType> ImageImportType;
  ImageImportType::Pointer movingItkImporter = ImageImportType::New();

  ConnectPipelines(movingVtkExporter, movingItkImporter);

  NormalizeFilter::Pointer movingNormalize = NormalizeFilter::New();
    movingNormalize->SetInput(movingItkImporter->GetOutput());

  sf[0] = shrinkFactors[0];
  sf[1] = shrinkFactors[1];
  sf[2] = shrinkFactors[2];
  ShrinkFilter::Pointer movingShrink = ShrinkFilter::New();
    movingShrink->SetInput(movingNormalize->GetOutput());
    movingShrink->SetShrinkFactors(sf);

  ChangeInformationFilter::Pointer movingChange = 

ChangeInformationFilter::New();
    movingChange->SetInput(movingShrink->GetOutput());
    movingChange->CenterImageOn();

  // Fixed ----------------
   vtkImageReader *fixedReader = vtkImageReader::New();
    fixedReader->SetDataScalarTypeToUnsignedShort();
    fixedReader->SetFilePrefix(study2Prefix.c_str());
    fixedReader->SetDataByteOrderToBigEndian();
    fixedReader->SetDataExtent(0,study1Resolution[0] - 

1,0,study1Resolution[1] - 1,1,study1Resolution[2]);// 

study1Resolution[0] - 1,
                                
    

fixedReader->SetDataSpacing(study1Spacing[0],study1Spacing[1],study1Spa

cing[2]);//study1Spacing[0],
    
    fixedReader->SetDataByteOrderToBigEndian();
    
  vtkImageExport *fixedVtkExporter = vtkImageExport::New();
    fixedVtkExporter->SetInput(fixedReader->GetOutput());

  ImageImportType::Pointer fixedItkImporter = ImageImportType::New();

  ConnectPipelines(fixedVtkExporter, fixedItkImporter);

  NormalizeFilter::Pointer fixedNormalize = NormalizeFilter::New();
    fixedNormalize->SetInput(fixedItkImporter->GetOutput());

  sf[0] = shrinkFactors[0];
  sf[1] = shrinkFactors[1];
  sf[2] = shrinkFactors[2];
  ShrinkFilter::Pointer fixedShrink = ShrinkFilter::New();
    fixedShrink->SetInput(fixedNormalize->GetOutput());
    fixedShrink->SetShrinkFactors(sf);

  ChangeInformationFilter::Pointer fixedChange = 

ChangeInformationFilter::New();
    fixedChange->SetInput(fixedShrink->GetOutput());
    fixedChange->CenterImageOn();
 AfxMessageBox("Before update");
  try
    {
    fixedChange->Update();
    movingChange->Update();
    }
  catch (itk::ExceptionObject& e)
    {
    std::cerr << "Exception detected: "  << e;
    return -1;
    }
    AfxMessageBox("After update");
//-----------------------------------------------------------
// Set up the registrator
//-----------------------------------------------------------
  MetricType::Pointer         metric        = MetricType::New();
  TransformType::Pointer      transform     = TransformType::New();
  OptimizerType::Pointer      optimizer     = OptimizerType::New();
  InterpolatorType::Pointer   interpolator  = InterpolatorType::New();
  RegistrationType::Pointer   registration  = RegistrationType::New();
  RegistrationType::ParametersType 

guess(transform->GetNumberOfParameters() );
  guess.Fill(0); guess[1] = 1.0;

  // The guess is: a quaternion followed by a translation
  std::cerr << "Enter a 7 tuple guess: ";
  std::cin >> guess;
  std::cerr << guess << std::endl;
  registration->SetInitialTransformParameters (guess);
  
  // Set translation scale
  typedef OptimizerType::ScalesType ScaleType;

  ScaleType scales(transform->GetNumberOfParameters());
  scales.Fill( 1.0 );
  for( unsigned j = 4; j < 7; j++ )
    {
    scales[j] = 1.0 / vnl_math_sqr(translateScale);
    }

  // Set metric related parameters
  metric->SetMovingImageStandardDeviation( standardDeviation );
  metric->SetFixedImageStandardDeviation( standardDeviation );
  metric->SetNumberOfSpatialSamples( numberOfSamples );

  // Connect up the components
  registration->SetMetric(metric);
  registration->SetOptimizer(optimizer);
  registration->SetTransform(transform);
  registration->SetInterpolator(interpolator);
  registration->SetFixedImage(fixedChange->GetOutput());
  registration->SetMovingImage(movingChange->GetOutput());

  // Setup the optimizer
  optimizer->SetScales(scales);
  //optimizer->MaximizeOn();

  optimizer->SetNumberOfIterations( numberOfIterations );
  optimizer->SetLearningRate( learningRate );

  // Start registration

  registration->StartRegistration();


Regards,
CSPL

----- Original Message ----- 
From: "Lorensen, William E (Research)" <lorensen@crd.ge.com>
To: "'cspl'" <affable@hd2.dot.net.in>; <insight-users@public.kitware.com>
Cc: "Luis Ibanez" <luis.ibanez@kitware.com>
Sent: Monday, July 29, 2002 4:55 PM
Subject: RE: [Insight-users] MRIRegistration example has problems


> What problems are you having?
>  
> 
> -----Original Message-----
> From: cspl [mailto:affable@hd2.dot.net.in]
> Sent: Saturday, July 27, 2002 9:53 AM
> To: insight-users@public.kitware.com
> Cc: Luis Ibanez
> Subject: [Insight-users] MRIRegistration example has problems
> 
> 
> Dear friends,
> 
> I am working on MRIRegistration example.When I tried to execute 
> the  application i got problem at StartRegistration().Please send me 
> some information regarding method and setting registration parameters. 
>  
> Regards,
> CSPL
> 
>