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