[Insight-users] How I can write to a file.txt???

javier silva bravo javier_silva_bravo at hotmail.com
Tue Apr 26 15:03:54 EDT 2005


An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20050426/f12449ac/attachment.htm
-------------- next part --------------
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: DeformableRegistration6.cxx,v $
  Language:  C++
  Date:      $Date: 2004/12/28 14:42:48 $
  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.

=========================================================================*/

// Software Guide : BeginLatex
//
// This example illustrates the use of the 
\doxygen{BSplineDeformableTransform}
// class in a manually controlled multi-resolution scheme. Here we define 
two
// transforms at two different resolution levels. A first registration is
// perfomed with the spline grid of low resolution, and the results are then
// used for initializing a higher resolution grid. Since this example is 
quite
// similar to the previous example on the use of the
// \code{BSplineDeformableTransform} we omit here most of the details 
already
// discussed and will focus on the aspects related to the multi-resolution
// approach.
//
// \index{itk::BSplineDeformableTransform}
// \index{itk::BSplineDeformableTransform!DeformableRegistration}
// \index{itk::LBFGSOptimizer}
//
//
// Software Guide : EndLatex

#include "itkImageRegistrationMethod.h"
#include "itkMeanSquaresImageToImageMetric.h"
//#include <itkNormalizedCorrelationImageToImageMetric.h>
//#include <itkGradientDifferenceImageToImageMetric.h>
//#include "itkLinearInterpolateImageFunction.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkImage.h"

//  Software Guide : BeginLatex
//
//  We include the header files for the transform and the optimizer.
//
//  \index{itk::BSplineDeformableTransform!header}
//  \index{itk::LBFGSOptimizer!header}
//
//  Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
#include "itkBSplineDeformableTransform.h"
//#include "itkLBFGSOptimizer.h"
#include "itkLBFGSBOptimizer.h"
//#include <itkConjugateGradientOptimizer.h>
//#include <itkGradientDescentOptimizer.h>
//#include <itkRegularStepGradientDescentOptimizer.h>
//#include <itkQuaternionRigidTransformGradientDescentOptimizer.h>

// Software Guide : EndCodeSnippet


#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkResampleImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkSquaredDifferenceImageFilter.h"

#include "itkBSplineResampleImageFunction.h"
#include "itkIdentityTransform.h"
#include "itkBSplineDecompositionImageFilter.h"
#include "itkTimeProbesCollectorBase.h"

#include "itkCommand.h"
class CommandIterationUpdate : public itk::Command
{
public:
  typedef  CommandIterationUpdate   Self;
  typedef  itk::Command             Superclass;
  typedef itk::SmartPointer<Self>  Pointer;
  itkNewMacro( Self );
protected:
  CommandIterationUpdate() {};
public:
  typedef itk::LBFGSBOptimizer     OptimizerType;
  typedef   const OptimizerType   *    OptimizerPointer;

/*
  typedef itk::MeanSquaresImageToImageMetric     MetricType;
  typedef   const MetricType   *    MetricPointer;

  typedef itk::BSplineDeformableTransform     TransformType;
  typedef   const TransformType   *    TransformPointer;*/


  void Execute(itk::Object *caller, const itk::EventObject & event)
    {
      Execute( (const itk::Object *)caller, event);
    }

  void Execute(const itk::Object * object, const itk::EventObject & event)
    {
      OptimizerPointer optimizer =
        dynamic_cast< OptimizerPointer >( object );
/*
  TransformType::Pointer  transformLow = TransformType::New();

  typedef TransformType::ParametersType     ParametersType;

  const unsigned int numberOfParameters =
               transformLow->GetNumberOfParameters();

  ParametersType parametersLow( numberOfParameters );


	  MetricPointer metric =
        dynamic_cast< MetricPointer >( object );*/

	  if( typeid( event ) != typeid( itk::IterationEvent ) )
        {
        return;
        }
      std::cout << optimizer->GetCurrentIteration() << "            ";
      std::cout << optimizer->GetValue() << "        ";
      //std::cout <<  << "        ";
	  std::cout << optimizer->GetInfinityNormOfProjectedGradient() << 
std::endl;
      //std::cout << metric->GetValue(parametersLow) << std::endl;
    }
};




// NOTE: the LBFGSOptimizer does not invoke events


int main( int argc, char *argv[] )
{
  if( argc < 4 )
    {
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " fixedImageFile  movingImageFile outputImagefile  ";
    std::cerr << " [differenceOutputfile] [differenceBeforeRegistration] ";
    std::cerr << " [deformationField] ";
    return 1;
    }

  const    unsigned int    ImageDimension = 2;
  typedef  float           PixelType;

  typedef itk::Image< PixelType, ImageDimension >  FixedImageType;
  typedef itk::Image< PixelType, ImageDimension >  MovingImageType;


  //  Software Guide : BeginLatex
  //
  //  We instantiate now the type of the \code{BSplineDeformableTransform} 
using
  //  as template parameters the type for coordinates representation, the
  //  dimension of the space, and the order of the BSpline.
  //
  //  \index{BSplineDeformableTransform|New}
  //  \index{BSplineDeformableTransform|Instantiation}
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  const unsigned int SpaceDimension = ImageDimension;
  const unsigned int SplineOrder = 3;
  typedef double CoordinateRepType;

  typedef itk::BSplineDeformableTransform<
                            CoordinateRepType,
                            SpaceDimension,
                            SplineOrder >     TransformType;
  // Software Guide : EndCodeSnippet


  //typedef itk::LBFGSOptimizer       OptimizerType;
  typedef itk::LBFGSBOptimizer       OptimizerType;
  //typedef itk::GradientDescentOptimizer       OptimizerType;
  //typedef itk::QuaternionRigidTransformGradientDescentOptimizer       
OptimizerType;
  /*
  typedef itk::NormalizedCorrelationImageToImageMetric<
                                    FixedImageType,
                                    MovingImageType >    MetricType;*/
  /*
  typedef itk::GradientDifferenceImageToImageMetric<
                                    FixedImageType,
                                    MovingImageType >    MetricType;*/

  typedef itk::MeanSquaresImageToImageMetric<
                                    FixedImageType,
                                    MovingImageType >    MetricType;

/*
  typedef itk:: LinearInterpolateImageFunction<
                                    MovingImageType,
                                    double          >    InterpolatorType;*/

  typedef itk::BSplineInterpolateImageFunction<
	                                 FixedImageType,
									 double,
									 double> InterpolatorType;

  typedef itk::ImageRegistrationMethod<
                                    FixedImageType,
                                    MovingImageType >    RegistrationType;



  MetricType::Pointer         metric        = MetricType::New();
  OptimizerType::Pointer      optimizer     = OptimizerType::New();
  InterpolatorType::Pointer   interpolator  = InterpolatorType::New();
  RegistrationType::Pointer   registration  = RegistrationType::New();

  std::cout << " 
////////////////////////////////////////////////////////////////////////////// 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " /////////// Se estable el orden al interpolador 
////////////////////////////// " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
////////////////////////////////////////////////////////////////////////////// 
" << std::endl;

  interpolator->SetSplineOrder(SplineOrder);
  metric->ComputeGradientOn();
  metric->SetInterpolator(interpolator);

  std::cout << " 
------------------------------------------------------------------------------ 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " ----------- Se estable la metrica,optimizador e 
interpolador al registro ----- " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
------------------------------------------------------------------------------ 
" << std::endl;


  registration->SetMetric(        metric        );
  registration->SetOptimizer(     optimizer     );
  registration->SetInterpolator(  interpolator  );


  //  Software Guide : BeginLatex
  //
  //  We construct two transform objects, each one will be configured for a 
resolution level.
  //  Notice than in this multi-resolution scheme we are not modifying the
  //  resolution of the image, but rather the flexibility of the deformable
  //  transform itself.
  //
  //  \index{itk::RegistrationMethod!SetTransform()}
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  TransformType::Pointer  transformLow = TransformType::New();
  registration->SetTransform( transformLow );
// metric->SetTransform(transformLow);


  std::cout << " 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " ++ Se establece La transformacion en bajo tanto en el 
regitro ++++++++++++++++ " << std::endl;
  std::cout << "             como en la metrica    " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
" << std::endl;


  // Software Guide : EndCodeSnippet

  typedef itk::ImageFileReader< FixedImageType  > FixedImageReaderType;
  typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;

  FixedImageReaderType::Pointer  fixedImageReader  = 
FixedImageReaderType::New();
  MovingImageReaderType::Pointer movingImageReader = 
MovingImageReaderType::New();

  fixedImageReader->SetFileName(  argv[1] );
  movingImageReader->SetFileName( argv[2] );

  FixedImageType::ConstPointer fixedImage = fixedImageReader->GetOutput();
  try{
  registration->SetFixedImage(  fixedImage   );
  registration->SetMovingImage(   movingImageReader->GetOutput()   );

  interpolator->SetInputImage( fixedImage ); //fixedImage
/*
  metric->SetFixedImage(  fixedImage   );
  metric->SetMovingImage(   movingImageReader->GetOutput()   );*/
  }
  catch(itk::ExceptionObject & err)
  {
    std::cerr << "ExceptionObject caught !" << std::endl;
	std::cerr << "Error al establecer la Imagen fija y movil en la metrica" << 
std::endl;
	std::cerr << "y en el interpolador" << std::endl;
    std::cerr << err << std::endl;
    return -1;
  }
  std::cout << " 
·············································································· 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " ·· Se establece Imagen fija y movil 
registro,metrica,interpolador ·· " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
·············································································· 
" << std::endl;


  try{
  fixedImageReader->Update();
  }
  catch(itk::ExceptionObject & err)
  {
    std::cerr << "ExceptionObject caught !" << std::endl;
	std::cerr << "Error al leer Imagen fija" << std::endl;
    std::cerr << err << std::endl;
    return -1;
  }

  FixedImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();

  registration->SetFixedImageRegion( fixedRegion );
  //metric->SetFixedImageRegion(fixedRegion);


  //  Software Guide : BeginLatex
  //
  //  Here we define the parameters of the BSplineDeformableTransform grid.  
We
  //  arbitrarily decide to use a grid with $5 \times 5$ nodes within the 
image.
  //  The reader should note that the BSpline computation requires a
  //  finite support region ( 1 grid node at the lower borders and 2
  //  grid nodes at upper borders). Therefore in this example, we set
  //  the grid size to be $8 \times 8$ and place the grid origin such that
  //  grid node (1,1) coinicides with the first pixel in the fixed image.
  //
  //  \index{BSplineDeformableTransform}
  //
  //  Software Guide : EndLatex


  typedef TransformType::RegionType RegionType;
  RegionType bsplineRegionLow;
  RegionType::SizeType   gridBorderSize;
  RegionType::SizeType   totalGridSize;

  gridBorderSize.Fill( 3 );    // Border for spline order = 3 ( 1 lower, 2 
upper )

   //  Software Guide : BeginLatex
  //
  //  Here we define the parameters of the BSpline transform at low 
resolution
  //
  //  \index{BSplineDeformableTransform}
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  RegionType::SizeType   gridLowSizeOnImage;
  gridLowSizeOnImage.Fill( 15 );//5
  totalGridSize = gridLowSizeOnImage + gridBorderSize;

  std::cout << " 
############################################################################## 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "             Se crea la regilla con 15    " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
############################################################################## 
" << std::endl;



  RegionType bsplineRegion;
  bsplineRegion.SetSize( totalGridSize );

  typedef TransformType::SpacingType SpacingType;
  SpacingType spacingLow = fixedImage->GetSpacing();

  typedef TransformType::OriginType OriginType;
  OriginType originLow = fixedImage->GetOrigin();;

  FixedImageType::SizeType fixedImageSize = fixedRegion.GetSize();

  std::cout << "  
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "  Se establece el tamaño de la imagen fija origen y espacio 
en trasnformlow    " << std::endl;
  std::cout << spacingLow    << " Espacio ";
  std::cout << originLow << " Origen ";
  std::cout << bsplineRegion << " BSplineRegion "<<std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "  
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
" << std::endl;
  /*std::cout << spacingLow  << std::endl;
  std::cout << originLow << std::endl;
  std::cout << bsplineRegion << std::endl;*/


  for(unsigned int r=0; r<ImageDimension; r++)
    {
    spacingLow[r] *= floor( static_cast<double>(fixedImageSize[r] - 1)  /
                            static_cast<double>(gridLowSizeOnImage[r] - 1) 
);
    //originLow[r]  -=  spacingLow[r];
    }

  transformLow->SetGridSpacing( spacingLow );
  transformLow->SetGridOrigin( originLow );
  transformLow->SetGridRegion( bsplineRegion );
  std::cout << "  
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
" << std::endl;
  std::cout <<spacingLow    << " Espacio ";
  std::cout << originLow << " Origen ";
  std::cout << bsplineRegion << " BSplineRegion "<< std::endl;
  std::cout << "                 " << std::endl;

  /*std::cout << spacingLow  << std::endl;
  std::cout << originLow << std::endl;
  std::cout << bsplineRegion << std::endl;*/

  typedef TransformType::ParametersType     ParametersType;

  const unsigned int numberOfParameters =
               transformLow->GetNumberOfParameters();

  ParametersType parametersLow( numberOfParameters );

  parametersLow.Fill( 0.0 );

  transformLow->SetParameters( parametersLow );
  //  Software Guide : EndCodeSnippet



  //  Software Guide : BeginLatex
  //
  //  We now pass the parameters of the current transform as the initial
  //  parameters to be used when the registration process starts.
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  registration->SetInitialTransformParameters( transformLow->GetParameters() 
);
/* metric->SetTransformParameters(transformLow->GetParameters());
  metric->SetInterpolator(  interpolator  );*/

  std::cout << " 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "Se establecen los parametros iniciales en metrica y registro 
de la transformacion en bajo    " << std::endl;
  std::cout << "  asi como el interpolador en la metrica    " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
" << std::endl;
/* try{
  metric->Initialize();
  }
  catch(itk::ExceptionObject & err)
  {
  std::cout << " Se encontro la siguiente Excepcion " << std::endl;
  std::cout << err << std::endl;
  return -1;
  }*/

  /* LBFGSOptimizer
  optimizer->SetCostFunction(metric);
  optimizer->SetGradientConvergenceTolerance( 0.05 );
  optimizer->SetLineSearchAccuracy( 1.01);//0.9
  optimizer->SetDefaultStepLength( 0.25 );//1.5
  optimizer->TraceOn();
  optimizer->SetMaximumNumberOfFunctionEvaluations( 1000 );*/

  /* LBFGSBOptimizer*/
  OptimizerType::BoundSelectionType boundSelect( 
transformLow->GetNumberOfParameters() );
  OptimizerType::BoundValueType upperBound( 
transformLow->GetNumberOfParameters() );
  OptimizerType::BoundValueType lowerBound( 
transformLow->GetNumberOfParameters() );

  boundSelect.Fill( 0 );
  upperBound.Fill( 0.0 );
  lowerBound.Fill( 0.0 );

  optimizer->SetBoundSelection( boundSelect );
  optimizer->SetUpperBound( upperBound );
  optimizer->SetLowerBound( lowerBound );

  optimizer->SetCostFunctionConvergenceFactor( 1e+7 );//1e+7
  optimizer->SetProjectedGradientTolerance( 0.04 );//1e-4
  optimizer->SetMaximumNumberOfIterations( 500 );
  optimizer->SetMaximumNumberOfEvaluations( 500 );
  optimizer->SetMaximumNumberOfCorrections( 10 );


  /*  QuaternionRigidTransformGradientDescentOptimizer
  //optimizer->MaximizeOn();
  optimizer->SetLearningRate(1.5);
  optimizer->SetNumberOfIterations(100);*/

  /* GradientDescentOptimizer
  optimizer->SetCostFunction(metric);
  //optimizer->MinimizeOn();
  optimizer->SetLearningRate(20.0);
  optimizer->SetNumberOfIterations(100);
  optimizer->MaximizeOn();*/

  CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
  optimizer->AddObserver( itk::IterationEvent(), observer );


  std::cout << " 
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "           Se establecen los parametros para el optimizador 
LBFGSBOptimizer     " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
" << std::endl;


  std::cout << "Starting Registration with low resolution transform" << 
std::endl;
  std::cout << "Iteracion    Valor    InfinityNormGradient" << std::endl;
  itk::TimeProbesCollectorBase collector;

  try
    {

	collector.Start( "Registration" );
    registration->StartRegistration();
	collector.Stop( "Registration" );
//	optimizer->AdvanceOneStep();
    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
	std::cerr << "Error durante el Registro en bajo" << std::endl;
    std::cerr << err << std::endl;
    return -1;
    }
  // Software Guide : EndCodeSnippet

  std::cout << " 
============================================================================== 
" << std::endl;
  std::cout << " ==                                                          
                 == " << std::endl;
  std::cout << " ==       REPORTE DEL REGISTRO                               
                 == " << std::endl;
  std::cout << " ==                                                          
                 == " << std::endl;
  collector.Report();
  std::cout << optimizer->GetCostFunctionConvergenceFactor() << " Factor de 
Convergencia "<< std::endl;
  std::cout << " ==                                                          
                 == " << std::endl;
  std::cout << " ==                                                          
                 == " << std::endl;
  std::cout << " 
============================================================================== 
" << std::endl;

  //  Software Guide : BeginLatex
  //
  //  Once the registration has finished with the low resolution grid, we
  //  proceed to instantiate a higher resolution
  //  \code{BSplineDeformableTransform}.
  //
  //  Software Guide : EndLatex
/*
  TransformType::Pointer  transformHigh = TransformType::New();

  std::cout << " 
{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "             Se crea la transformacion en alto    " << 
std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ 
" << std::endl;

  RegionType::SizeType   gridHighSizeOnImage;
  gridHighSizeOnImage.Fill( 100);
  totalGridSize = gridHighSizeOnImage + gridBorderSize;

  std::cout << " 
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << "             Se crea la rejilla con 100    " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
" << std::endl;

  bsplineRegion.SetSize( totalGridSize );

  SpacingType spacingHigh = fixedImage->GetSpacing();
  OriginType  originHigh  = fixedImage->GetOrigin();;



  for(unsigned int rh=0; rh<ImageDimension; rh++)
    {
    spacingHigh[rh] *= floor( static_cast<double>(fixedImageSize[rh] - 1)  /
                            static_cast<double>(gridHighSizeOnImage[rh] - 1) 
);
    originHigh[rh]  -=  spacingHigh[rh];
    }

  transformHigh->SetGridSpacing( spacingHigh );
  transformHigh->SetGridOrigin( originHigh );
  transformHigh->SetGridRegion( bsplineRegion );

  ParametersType parametersHigh( transformHigh->GetNumberOfParameters() );
  parametersHigh.Fill( 0.0 );

  std::cout << " 
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " Se establece el origen, espacio, Bspline region, asi como   
     " << std::endl;
  std::cout << " Se llenan los parametros de la transformacion en alto con 0 
(cero)" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
" << std::endl;
  std::cout << spacingHigh  << std::endl;
  std::cout << originHigh << std::endl;
  std::cout << bsplineRegion << std::endl;


  //  Software Guide : BeginLatex
  //
  //  Now we need to initialize the BSpline coefficients of the higher 
resolution
  //  transform. This is done by first computing the actual deformation 
field
  //  at the higher resolution from the lower resolution BSpline 
coefficients.
  //  Then a BSpline decomposition is done to obtain the BSpline coefficient 
of
  //  the higher resolution transform.
  //
  //  Software Guide : EndLatex


  unsigned int counter = 0;

  for ( unsigned int k = 0; k < SpaceDimension; k++ )
    {
    typedef TransformType::ImageType ParametersImageType;
    typedef 
itk::ResampleImageFilter<ParametersImageType,ParametersImageType> 
ResamplerType;
    ResamplerType::Pointer upsampler = ResamplerType::New();

    typedef itk::BSplineResampleImageFunction<ParametersImageType,double> 
FunctionType;
    FunctionType::Pointer function = FunctionType::New();

    typedef itk::IdentityTransform<double,SpaceDimension> 
IdentityTransformType;
    IdentityTransformType::Pointer identity = IdentityTransformType::New();

    upsampler->SetInput( transformLow->GetCoefficientImage()[k] );
    upsampler->SetInterpolator( function );
    upsampler->SetTransform( identity );
    upsampler->SetSize( transformHigh->GetGridRegion().GetSize() );
    upsampler->SetOutputSpacing( transformHigh->GetGridSpacing() );
    upsampler->SetOutputOrigin( transformHigh->GetGridOrigin() );

    typedef 
itk::BSplineDecompositionImageFilter<ParametersImageType,ParametersImageType>
      DecompositionType;
    DecompositionType::Pointer decomposition = DecompositionType::New();

    decomposition->SetSplineOrder( SplineOrder );
    decomposition->SetInput( upsampler->GetOutput() );
    decomposition->Update();

    ParametersImageType::Pointer newCoefficients = 
decomposition->GetOutput();

    // copy the coefficients into the parameter array
    typedef itk::ImageRegionIterator<ParametersImageType> Iterator;
    Iterator it( newCoefficients, transformHigh->GetGridRegion() );
    while ( !it.IsAtEnd() )
      {
      parametersHigh[ counter++ ] = it.Get();
      ++it;
      }

    }

  transformHigh->SetParameters( parametersHigh );

  std::cout << " 
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " Se establecen los parametros iniciales para la 
transformacion en alto     " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ 
" << std::endl;


  //  Software Guide : BeginLatex
  //
  //  We now pass the parameters of the high resolution transform as the 
initial
  //  parameters to be used in a second stage of the registration process.
  //
  //  Software Guide : EndLatex

  std::cout << "Starting Registration with high resolution transform" << 
std::endl;

  // Software Guide : BeginCodeSnippet
  std::cout << " 
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] 
" << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " Se establecen en el registro y en la metrica la 
trasnformacion en alto y parametros     " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] 
" << std::endl;

  registration->SetTransform( transformHigh );
  registration->SetInitialTransformParameters( 
transformHigh->GetParameters() );


  metric->SetTransform(transformHigh);
  metric->SetTransformParameters(transformHigh->GetParameters());

  metric->SetInterpolator(  interpolator  );

  //  Software Guide : BeginLatex
  //
  //  Typically, we will also want to tighten the optimizer parameters
  //  when we move from lower to higher resolution grid.
  //
  //  Software Guide : EndLatex

  /* LBFGSOptimizer
  optimizer->SetCostFunction(metric);
  optimizer->SetGradientConvergenceTolerance( 0.02 );
  optimizer->SetDefaultStepLength( 0.025 );*/

  /* LBFGSBOptimizer
   boundSelect( transformHigh->GetNumberOfParameters() );
   upperBound( transformHigh->GetNumberOfParameters() );
   lowerBound( transformHigh->GetNumberOfParameters() );

  boundSelect.Fill( 0 );
  upperBound.Fill( 0.0 );
  lowerBound.Fill( 0.0 );

  optimizer->SetBoundSelection( boundSelect );
  optimizer->SetUpperBound( upperBound );
  optimizer->SetLowerBound( lowerBound );

  optimizer->SetCostFunctionConvergenceFactor( 1e+7 );//1e+7
  optimizer->SetProjectedGradientTolerance( 0.02 );//1e-4
  optimizer->SetMaximumNumberOfIterations( 500 );
  optimizer->SetMaximumNumberOfEvaluations( 500 );
  optimizer->SetMaximumNumberOfCorrections( 12 );

  /* GradientDescentOptimizer
  optimizer->SetCostFunction(metric);
  optimizer->SetLearningRate(20.0);
  optimizer->SetNumberOfIterations(100);
  //optimizer->MinimizeOn();
  optimizer->MaximizeOn();

  std::cout << " 
****************************************************************************** 
" << std::endl;
  std::cout << " 
****************************************************************************** 
" << std::endl;
  std::cout << " *Se establecen los parametros para el optimizador 
LBFGSBOptimizer  ** " << std::endl;
  std::cout << " *en alto 
********************************************************************* " << 
std::endl;
  std::cout << " 
****************************************************************************** 
" << std::endl;


std::cout << "Iteracion    Valor    InfinityNormGradient" << std::endl;

  try
    {
	metric->Initialize();
	collector.Start( "Registration" );
    registration->StartRegistration();
	collector.Stop( "Registration" );
    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
    return -1;
    }
  // Software Guide : EndCodeSnippet


  std::cout << " 
====================================================================== " << 
std::endl;
  std::cout << " ==                                                          
         == " << std::endl;
  std::cout << " ==          REPORTE DEL REGISTRO                            
         == " << std::endl;
  std::cout << " ==                                                          
         == " << std::endl;
  std::cout << " ==                                                          
         == " << std::endl;
  collector.Report();
  std::cout << " 
====================================================================== " << 
std::endl;


  // Finally we use the last transform parameters in order to resample the 
image.
  //
  transformHigh->SetParameters( registration->GetLastTransformParameters() 
);
*/
  std::cout << " 
###################################################################### " << 
std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " Se recrea la imagen con los parametros de la transformacion 
en alto    " << std::endl;
  std::cout << "                 " << std::endl;
  std::cout << " 
###################################################################### " << 
std::endl;


  typedef itk::ResampleImageFilter<
                            MovingImageType,
                            FixedImageType,
							double>    ResampleFilterType;

  ResampleFilterType::Pointer resample = ResampleFilterType::New();

  resample->SetTransform( transformLow );//transformHigh
  resample->SetInput( movingImageReader->GetOutput() );

  resample->SetSize(    fixedImage->GetLargestPossibleRegion().GetSize() );
  resample->SetOutputOrigin(  fixedImage->GetOrigin() );
  resample->SetOutputSpacing( fixedImage->GetSpacing() );
  resample->SetDefaultPixelValue( 100 );

  typedef  unsigned char  OutputPixelType;

  typedef itk::Image< OutputPixelType, ImageDimension > OutputImageType;

  typedef itk::CastImageFilter<
                        FixedImageType,
                        OutputImageType > CastFilterType;

  typedef itk::ImageFileWriter< OutputImageType >  WriterType;


  WriterType::Pointer      writer =  WriterType::New();
  CastFilterType::Pointer  caster =  CastFilterType::New();


  writer->SetFileName( argv[3] );


  caster->SetInput( resample->GetOutput() );
  writer->SetInput( caster->GetOutput()   );


  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
    return -1;
    }



  typedef itk::SquaredDifferenceImageFilter<
                                  FixedImageType,
                                  FixedImageType,
                                  OutputImageType > DifferenceFilterType;

  DifferenceFilterType::Pointer difference = DifferenceFilterType::New();

  WriterType::Pointer writer2 = WriterType::New();
  writer2->SetInput( difference->GetOutput() );


  // Compute the difference image between the
  // fixed and resampled moving image.
  if( argc >= 5 )
    {
    difference->SetInput1( fixedImageReader->GetOutput() );
    difference->SetInput2( resample->GetOutput() );
    writer2->SetFileName( argv[4] );
    try
      {
      writer2->Update();
      }
    catch( itk::ExceptionObject & err )
      {
      std::cerr << "ExceptionObject caught !" << std::endl;
      std::cerr << err << std::endl;
      return -1;
      }
    }


  // Compute the difference image between the
  // fixed and moving image before registration.
  if( argc >= 6 )
    {
    writer2->SetFileName( argv[5] );
    difference->SetInput1( fixedImageReader->GetOutput() );
    difference->SetInput2( movingImageReader->GetOutput() );
    try
      {
      writer2->Update();
      }
    catch( itk::ExceptionObject & err )
      {
      std::cerr << "ExceptionObject caught !" << std::endl;
      std::cerr << err << std::endl;
      return -1;
      }
    }



  // Generate the explicit deformation field resulting from
  // the registration.

  typedef itk::Vector< float, ImageDimension >  VectorType;
  typedef itk::Image< VectorType, ImageDimension >  DeformationFieldType;

  DeformationFieldType::Pointer field = DeformationFieldType::New();
  field->SetRegions( fixedRegion );
  field->SetOrigin( fixedImage->GetOrigin() );
  field->SetSpacing( fixedImage->GetSpacing() );
  field->Allocate();

  typedef itk::ImageRegionIterator< DeformationFieldType > FieldIterator;
  FieldIterator fi( field, fixedRegion );

  fi.GoToBegin();

  TransformType::InputPointType  fixedPoint;
  TransformType::OutputPointType movingPoint;
  DeformationFieldType::IndexType index;

  VectorType displacement;

  while( ! fi.IsAtEnd() )
    {
    index = fi.GetIndex();
    field->TransformIndexToPhysicalPoint( index, fixedPoint );
    movingPoint = transformLow->TransformPoint( fixedPoint );//transformHigh
    displacement[0] = movingPoint[0] - fixedPoint[0];
    displacement[1] = movingPoint[1] - fixedPoint[1];
    fi.Set( displacement );
    ++fi;
    }



  typedef itk::ImageFileWriter< DeformationFieldType >  FieldWriterType;
  FieldWriterType::Pointer fieldWriter = FieldWriterType::New();

  fieldWriter->SetInput( field );

  if( argc >= 7 )
    {
    fieldWriter->SetFileName( argv[6] );
    try
      {
      fieldWriter->Update();
      }
    catch( itk::ExceptionObject & excp )
      {
      std::cerr << "Exception thrown " << std::endl;
      std::cerr << excp << std::endl;
      return EXIT_FAILURE;
      }
    }

  return 0;
}




More information about the Insight-users mailing list