<html><div style='background-color:'><DIV class=RTE>When I run the outputimage is like movingimage and i hope the outputimage like fixedimage I dont know what happend does it?.</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE><FONT color=#ff3300>#include "itkImageRegistrationMethod.h"<BR></FONT><FONT color=#336600>#include "itkMeanSquaresImageToImageMetric.h"</FONT><BR><FONT color=#3333ff>#include "itkBSplineInterpolateImageFunction.h"</FONT><BR>#include "itkImage.h"<BR>#include "itkTimeProbesCollectorBase.h"<BR><FONT color=#660000>#include "itkBSplineDeformableTransform.h"</FONT><BR><FONT color=#ff6600>#include "itkLBFGSBOptimizer.h"</FONT><BR>#include "itkImageFileReader.h"<BR>#include "itkImageFileWriter.h"<BR>#include "itkResampleImageFilter.h"<BR>#include "itkCastImageFilter.h"<BR>#include "itkCommand.h"<BR>#include <fstream></DIV>
<DIV class=RTE>class CommandIterationUpdate : public itk::Command <BR>{<BR>public:<BR> typedef CommandIterationUpdate Self;<BR> typedef itk::Command Superclass;<BR> typedef itk::SmartPointer<Self> Pointer;<BR> itkNewMacro( Self );<BR>protected:<BR> CommandIterationUpdate() {};<BR> std::ofstream m_file;</DIV>
<DIV class=RTE>public:<BR> typedef itk::LBFGSBOptimizer OptimizerType;<BR> typedef const OptimizerType * OptimizerPointer;</DIV>
<DIV class=RTE> void SetFileName(const char* str)<BR> {<BR> try{<BR> m_file.open(str, std::ios::trunc); <BR> }<BR> catch(itk::ExceptionObject & err)<BR> {<BR> std::cerr << "ExceptionObject caught !" << std::endl; <BR> std::cerr << "Error al tratar de leer el archivo de los valores" << std::endl; <BR> std::cerr << err << std::endl; <BR> };<BR> }</DIV>
<DIV class=RTE><BR> void Execute(itk::Object *caller, const itk::EventObject & event)<BR> {<BR> Execute( (const itk::Object *)caller, event);<BR> }</DIV>
<DIV class=RTE> void Execute(const itk::Object * object, const itk::EventObject & event)<BR> {<BR> OptimizerPointer optimizer = <BR> dynamic_cast< OptimizerPointer >( object );<BR> if( typeid( event ) != typeid( itk::IterationEvent ) )<BR> {<BR> return;<BR> }<BR> std::cout << optimizer->GetCurrentIteration() << " ";<BR> std::cout << optimizer->GetValue() << " ";<BR> std::cout << optimizer->GetInfinityNormOfProjectedGradient() << std::endl;</DIV>
<DIV class=RTE> m_file << optimizer->GetCurrentIteration() << " ";<BR> m_file << optimizer->GetValue() << " ";<BR> m_file << optimizer->GetInfinityNormOfProjectedGradient() <<std::endl;</DIV>
<DIV class=RTE> }<BR>};</DIV>
<DIV class=RTE><BR>int main( int argc, char *argv[] )<BR>{<BR><BR> if( argc < 3 )<BR> {<BR> std::cerr << "Missing Parameters " << std::endl;<BR> std::cerr << "Usage: " << argv[0];<BR> std::cerr << " fixedImageFile movingImageFile outputImagefile ";<BR> std::cerr << " [ValuesField] ";<BR> return 1;<BR> }<BR> <BR> <BR> std::cout << "Inicio del programa " << std::endl;<BR> const unsigned int ImageDimension = 2;<BR> typedef float PixelType;</DIV>
<DIV class=RTE> typedef itk::Image< PixelType, ImageDimension > FixedImageType;<BR> typedef itk::Image< PixelType, ImageDimension > MovingImageType;</DIV>
<DIV class=RTE><BR> const unsigned int SpaceDimension = ImageDimension;<BR> const unsigned int SplineOrder = 3;<BR> typedef double CoordinateRepType;</DIV>
<DIV class=RTE> <FONT color=#660000>typedef itk::BSplineDeformableTransform<<BR> CoordinateRepType,<BR> SpaceDimension,<BR> SplineOrder > TransformType;</FONT></DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE><FONT color=#ff6600> typedef itk::LBFGSBOptimizer OptimizerType;</FONT></DIV>
<DIV class=RTE><BR> <FONT color=#336600> typedef itk::MeanSquaresImageToImageMetric< <BR> FixedImageType, <BR> MovingImageType > MetricType;<BR></FONT><BR><FONT color=#3333ff> typedef itk::BSplineInterpolateImageFunction<<BR>
MovingImageType,<BR> double,<BR> double> InterpolatorType;</FONT></DIV>
<DIV class=RTE><BR><FONT color=#ff3300> typedef itk::ImageRegistrationMethod< <BR> FixedImageType, <BR> MovingImageType > RegistrationType;</FONT></DIV>
<DIV class=RTE><FONT color=#336600> MetricType::Pointer metric = MetricType::New();</FONT><BR><FONT color=#ff6600> OptimizerType::Pointer optimizer = OptimizerType::New();</FONT><BR><FONT color=#3333ff> InterpolatorType::Pointer interpolator = InterpolatorType::New();</FONT><BR><FONT color=#ff3300> RegistrationType::Pointer registration = RegistrationType::New();</FONT><BR> <BR><FONT color=#3333ff> interpolator->SetSplineOrder(SplineOrder);</FONT><BR> <BR><FONT color=#ff3300> registration->SetMetric( <FONT color=#336600>metric</FONT> );</FONT><BR><FONT color=#ff3300>
registration->SetOptimizer( <FONT color=#ff6600>optimizer </FONT> );</FONT><BR><FONT color=#ff3300> registration->SetInterpolator( <FONT color=#3333ff>interpolator</FONT> );</FONT></DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE> <FONT color=#990000>TransformType::Pointer transform = TransformType::New();<BR></FONT><FONT color=#ff3300> registration->SetTransform( <FONT color=#990000>transform</FONT> );</FONT></DIV>
<DIV class=RTE><BR> typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;<BR> typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;</DIV>
<DIV class=RTE> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();<BR> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();</DIV>
<DIV class=RTE> fixedImageReader->SetFileName( argv[1] );<BR> movingImageReader->SetFileName( argv[2] );</DIV>
<DIV class=RTE> FixedImageType::ConstPointer fixedImage = fixedImageReader->GetOutput();</DIV>
<DIV class=RTE> <FONT color=#ff3300>registration->SetFixedImage( fixedImage );<BR></FONT><FONT color=#ff3300> registration->SetMovingImage( movingImageReader->GetOutput() );<BR></FONT> try{<BR> fixedImageReader->Update();<BR> }<BR> catch(itk::ExceptionObject & err)<BR> {<BR> std::cerr<<"Error al leer Archivo"<<std::endl;<BR> std::cerr<<err<<std::endl;<BR> return -1;<BR> }<BR> FixedImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();<BR> <BR> registration->SetFixedImageRegion( fixedRegion );</DIV>
<DIV class=RTE> typedef TransformType::RegionType RegionType;<BR> RegionType bsplineRegion;<BR> RegionType::SizeType gridSizeOnImage;<BR> RegionType::SizeType gridBorderSize;<BR> RegionType::SizeType totalGridSize;</DIV>
<DIV class=RTE> gridSizeOnImage.Fill( 15 );<BR> gridBorderSize.Fill( 3 ); // Border for spline order = 3 ( 1 lower, 2 upper )<BR> totalGridSize = gridSizeOnImage + gridBorderSize;</DIV>
<DIV class=RTE> bsplineRegion.SetSize( totalGridSize );</DIV>
<DIV class=RTE> typedef TransformType::SpacingType SpacingType;<BR> SpacingType spacing = fixedImage->GetSpacing();</DIV>
<DIV class=RTE> typedef TransformType::OriginType OriginType;<BR> OriginType origin = fixedImage->GetOrigin();;</DIV>
<DIV class=RTE> FixedImageType::SizeType fixedImageSize = fixedRegion.GetSize();</DIV>
<DIV class=RTE> for(unsigned int r=0; r<ImageDimension; r++)<BR> {<BR> spacing[r] *= floor( static_cast<double>(fixedImageSize[r] - 1) / <BR> static_cast<double>(gridSizeOnImage[r] - 1) );<BR> origin[r] -= spacing[r]; <BR> }</DIV>
<DIV class=RTE> <FONT color=#990000>transform->SetGridSpacing( spacing );<BR> transform->SetGridOrigin( origin );<BR> transform->SetGridRegion( bsplineRegion );<BR></FONT> </DIV>
<DIV class=RTE> typedef TransformType::ParametersType ParametersType;</DIV>
<DIV class=RTE> const unsigned int numberOfParameters =<BR> transform->GetNumberOfParameters();<BR> <BR> ParametersType parameters( numberOfParameters );</DIV>
<DIV class=RTE> parameters.Fill( 0.0 );</DIV>
<DIV class=RTE> transform->SetParameters( parameters );</DIV>
<DIV class=RTE> <FONT color=#ff3300>registration->SetInitialTransformParameters( <FONT color=#990000>transform->GetParameters()</FONT> );</FONT></DIV>
<DIV class=RTE> <FONT color=#ff6600>OptimizerType::BoundSelectionType boundSelect( <FONT color=#990000>transform->GetNumberOfParameters() </FONT>);<BR> OptimizerType::BoundValueType upperBound( <FONT color=#990000>transform->GetNumberOfParameters() </FONT>);<BR> OptimizerType::BoundValueType lowerBound( <FONT color=#990000>transform->GetNumberOfParameters()</FONT> );</FONT></DIV>
<DIV class=RTE><FONT color=#ff6600> boundSelect.Fill( 0 );<BR> upperBound.Fill( 0.0 );<BR> lowerBound.Fill( 0.0 );</FONT></DIV>
<DIV class=RTE><FONT color=#ff6600> optimizer->SetBoundSelection( boundSelect );<BR> optimizer->SetUpperBound( upperBound );<BR> optimizer->SetLowerBound( lowerBound );</FONT></DIV>
<DIV class=RTE><FONT color=#ff6600> optimizer->SetCostFunctionConvergenceFactor( 1e+1 );<BR> optimizer->SetProjectedGradientTolerance( 0.155555 );//1.0<BR> optimizer->SetMaximumNumberOfIterations( 100 );<BR> optimizer->SetMaximumNumberOfEvaluations( 500 );<BR> optimizer->SetMaximumNumberOfCorrections( 12 );<BR> std::cout << " SetProjectedGradientTolerance 0.155555 " << std::endl;</FONT></DIV>
<DIV class=RTE> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();<BR> try{<BR> observer->SetFileName(argv[4]);<BR> }<BR> catch(itk::ExceptionObject & err)<BR> {<BR> std::cerr <<" Error al leer Archivo "<<std::endl;<BR> std::cerr <<err<<std::endl;<BR> }<BR> optimizer->AddObserver( itk::IterationEvent(), observer );</DIV>
<DIV class=RTE> </DIV>
<DIV class=RTE> itk::TimeProbesCollectorBase collector;</DIV>
<DIV class=RTE> std::cout << std::endl << "Starting Registration" << std::endl;<BR> std::cout << " Iteracion Valor InfinityNormOfProjectedGradient " << std::endl;</DIV>
<DIV class=RTE> try <BR> { <BR> collector.Start( "Registration" );<BR> <FONT color=#ff3300>registration->StartRegistration(); <BR></FONT> collector.Stop( "Registration" );<BR> } <BR> catch( itk::ExceptionObject & err ) <BR> { <BR> std::cerr << "ExceptionObject caught !" << std::endl; <BR> std::cerr << err << std::endl; <BR> return -1;<BR> } <BR> collector.Report();<BR> <BR> OptimizerType::ParametersType finalParameters = <BR> registration->GetLastTransformParameters();</DIV>
<DIV class=RTE> <FONT color=#990000>transform->SetParameters( finalParameters );</FONT></DIV>
<DIV class=RTE> typedef itk::ResampleImageFilter< <BR> MovingImageType, <BR> FixedImageType> ResampleFilterType;</DIV>
<DIV class=RTE> ResampleFilterType::Pointer resample = ResampleFilterType::New();<BR> <BR> resample->SetTransform( <FONT color=#990000>transform</FONT> );<BR> resample->SetInput( movingImageReader->GetOutput() );<BR> resample->SetInterpolator(<FONT color=#3333ff>interpolator</FONT>);<BR> resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );<BR> resample->SetOutputOrigin( fixedImage->GetOrigin() );<BR> resample->SetOutputSpacing( fixedImage->GetSpacing() );<BR> resample->SetDefaultPixelValue( 100 );<BR> <BR> typedef unsigned char OutputPixelType;</DIV>
<DIV class=RTE> typedef itk::Image< OutputPixelType, ImageDimension > OutputImageType;<BR> <BR> typedef itk::CastImageFilter< <BR> FixedImageType,<BR> OutputImageType > CastFilterType;<BR> <BR> typedef itk::ImageFileWriter< OutputImageType > WriterType;</DIV>
<DIV class=RTE><BR> WriterType::Pointer writer = WriterType::New();<BR> CastFilterType::Pointer caster = CastFilterType::New();</DIV>
<DIV class=RTE><BR> writer->SetFileName( argv[3] );</DIV>
<DIV class=RTE><BR> caster->SetInput( resample->GetOutput() );<BR> writer->SetInput( caster->GetOutput() );</DIV>
<DIV class=RTE> std::cout << "Creando Imagen De Salida ..." << std::endl;<BR> try<BR> {<BR> writer->Update();<BR> }<BR> catch( itk::ExceptionObject & err ) <BR> { <BR> std::cerr << "ExceptionObject caught !" << std::endl; <BR> std::cerr << err << std::endl; <BR> return -1;<BR> } std::cout << "El programa termino satisfactoriamente" << std::endl;<BR> return 0;<BR>}<BR></DIV></div><br clear=all><hr>Visita MSN Latino Noticias: Todo lo que pasa en el mundo y en tu país, ¡en tu idioma! <a href="http://g.msn.com/8HMBESUS/2731??PS=47575" target="_top">Clic aquí</a> </html>