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