[Insight-users] Helo with bspline/mutual information/gradient descent
Luis Ibanez
luis.ibanez at kitware.com
Sun Aug 9 18:45:34 EDT 2009
Hi J.X.J.
Thanks for posting your code.
Can you please indicate in your code,
in what *exact* line of code you are getting that compilation error ?
Your code snippet is not complete enough for me
to try to compile it ... :-/
Thanks
Luis
---------------------------------------------------------------------------------------------------
On Sun, Aug 9, 2009 at 5:03 PM, J.X.J. <wat.a.phony at gmail.com> wrote:
>
> Hi Luis,
>
> I'll give your method a try. At the momemnt this is how I've got mine
> output
> metric, it's not very eligant but it works.
>
> #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::GradientDescentOptimizer OptimizerType;
> typedef const OptimizerType * OptimizerPointer;
>
> std::fstream infile;
>
> 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 );
> if( ! itk::IterationEvent().CheckEvent( &event ) )
> {
> return;
> }
> std::cout << "Iteration : ";
> std::cout << optimizer->GetCurrentIteration() << " ";
> std::cout << optimizer->GetValue() << " ";
> std::cout << std::endl;
> infile.open("metric.txt",std::ios::out | std::ios::app);
> infile << optimizer->GetValue();
> infile << std::endl;
> infile.close();
> }
> };
>
>
>
> As for the Levenberg Marquardt, here what I've wrote:
>
> //Define internal variable types
> typedef float InputPixelType;
> typedef double CoordinateType;
>
> //Define image types and read in the fixed and moving images
> typedef itk::Image<InputPixelType, Dimension> FixedImageType;
> typedef itk::Image<InputPixelType, Dimension> MovingImageType;
> typedef itk::Image<InputPixelType, Dimension> InternalImageType;
> typedef itk::ImageFileReader<FixedImageType> FixedReaderType;
> typedef itk::ImageFileReader<MovingImageType> MovingReaderType;
> typedef itk::CastImageFilter<FixedImageType, InternalImageType>
> FixedCasterType;
> typedef itk::CastImageFilter<MovingImageType, InternalImageType>
> MovingCasterType;
>
> typedef itk::GDCMImageIO ImageIOType;
>
> ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
>
> FixedReaderType::Pointer fixedImageReader = FixedReaderType::New();
> MovingReaderType::Pointer movingImageReader =
> MovingReaderType::New();
>
> fixedImageReader->SetFileName(argv[1]);
> movingImageReader->SetFileName(argv[2]);
>
> fixedImageReader->SetImageIO(gdcmImageIO);
> movingImageReader->SetImageIO(gdcmImageIO);
>
> FixedCasterType::Pointer fixedCaster = FixedCasterType::New();
> MovingCasterType::Pointer movingCaster = MovingCasterType::New();
> fixedCaster->SetInput(fixedImageReader->GetOutput());
> movingCaster->SetInput(movingImageReader->GetOutput());
> fixedCaster->Update();
> movingCaster->Update();
>
> FixedImageType::Pointer fixedImage = fixedCaster->GetOutput();
> MovingImageType::Pointer movingImage = movingCaster->GetOutput();
>
> //Define the registration method
> typedef itk::ImageRegistrationMethod<InternalImageType,
> InternalImageType>
> RegistrationType;
> typedef itk::LevenbergMarquardtOptimizer OptimizerType;
> typedef itk::MutualInformationImageToImageMetric<InternalImageType,
> InternalImageType> MetricType;
> typedef itk::BSplineDeformableTransform<CoordinateType, Dimension,
> SplineOrder> TransformType;
> typedef
> itk::LinearInterpolateImageFunction<InternalImageType,CoordinateType>
> InterpolatorType;
>
> RegistrationType::Pointer registration = RegistrationType::New();
> OptimizerType::Pointer optimizer = OptimizerType::New();
> MetricType::Pointer metric = MetricType::New();
> TransformType::Pointer transform = TransformType::New();
> InterpolatorType::Pointer interpolator = InterpolatorType::New();
>
> registration->SetOptimizer(optimizer);
> registration->SetMetric(metric);
> registration->SetTransform(transform);
> registration->SetInterpolator(interpolator);
>
> registration->SetFixedImage(fixedImage);
> registration->SetMovingImage(movingImage);
>
>
>
> Luis Ibanez wrote:
> >
> > Hi J.X.J
> >
> >
> > You could do something like the following:
> >
> >
> >
> > 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::LevenbergMarquardtOptimizer OptimizerType
> > typedef const OptimizerType * OptimizerPointer;
> >
> > void Open( const char * filename )
> > {
> > m_Output.open( filename );
> > }
> > void Close()
> > {
> > m_Output.close();
> > }
> > 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 );
> > if( ! itk::IterationEvent().CheckEvent( &event ) )
> > {
> > return;
> > }
> > m_Output << optimizer->GetCurrentIteration() << " ";
> > m_Output << optimizer->GetCachedValue() << " ";
> > m_Output << optimizer->GetCurrentPosition() << std::endl;
> > }
> > private:
> > std::ofstream m_Output;
> > };
> >
> > Then you connect it to the optimizer as:
> >
> >
> > CommandIterationUpdate::Pointer observer =
> > CommandIterationUpdate::New();
> > optimizer->AddObserver( itk::IterationEvent(), observer );
> > observer->Open( "myOutputFile.txt");
> > registration->StartRegistration();
> > observer->Close();
> >
> >
> > Note that the actual methods to call will have to match the API
> > of the optimizer that you are using.
> >
> >
> > About the compilation error... please post the source code
> > of your test. You seem to be mixing two incompatible types.
> >
> >
> >
> > Regards,
> >
> >
> > Luis
> >
> >
> > ------------------------------------------------------
> > On Sun, Aug 9, 2009 at 5:17 AM, J.X.J. <wat.a.phony at gmail.com> wrote:
> >
> >>
> >> Hi Luis,
> >>
> >> How can I store the metric value of each iteration into a file or
> >> variable/array within the observer? I've tried using ofstream to output
> >> the
> >> metric into a text file but it only stores the final metric value
> >> (optimizer->GetMetric()) after the registration->update() is finished.
> >> I've
> >> tried introducing ofstream into the observer but I cannot use the
> >> following
> >> command inside the observer (if that makes sense).
> >>
> >> std::ofstream infile;
> >> infile.open(argv[4]);
> >> infile<<optimizer->GetMetric();
> >> infile.close;
> >>
> >> As for the Levenberg Marquardt Optimizer this is the whole error message
> >> when compiling.
> >>
> >> 1>BSpline_ITK_DICOMM.cxx
> >> 1>.\BSpline_ITK_DICOMM.cxx(242) : error C2664:
> >> 'itk::ImageRegistrationMethod<TFixedImage,TMovingImage>::SetOptimizer' :
> >> cannot convert parameter 1 from
> >> 'itk::LevenbergMarquardtOptimizer::Pointer'
> >> to
> 'itk::ImageRegistrationMethod<TFixedImage,TMovingImage>::OptimizerType
> >> *'
> >> 1> with
> >> 1> [
> >> 1> TFixedImage=InternalImageType,
> >> 1> TMovingImage=InternalImageType
> >> 1> ]
> >> 1> No user-defined-conversion operator available that can perform
> >> this conversion, or the operator cannot be called
> >> 1>.\BSpline_ITK_DICOMM.cxx(366) : error C2039: 'GetCurrentIteration' :
> is
> >> not a member of 'itk::LevenbergMarquardtOptimizer'
> >> 1> D:\University -
> >>
> >>
> Engineering\Project\InsightToolkit-3.12.0\Code\Numerics\itkLevenbergMarquardtOptimizer.h(31)
> >> : see declaration of 'itk::LevenbergMarquardtOptimizer'
> >> 1>.\BSpline_ITK_DICOMM.cxx(367) : error C2440: 'initializing' : cannot
> >> convert from 'itk::MultipleValuedNonLinearOptimizer::MeasureType' to
> >> 'double'
> >> 1> No user-defined-conversion operator available that can perform
> >> this conversion, or the operator cannot be called
> >>
> >> J.X.J.
> >>
> >>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090809/88390f3f/attachment-0001.htm>
More information about the Insight-users
mailing list