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 = 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><div class="gmail_quote">On Sun, Aug 9, 2009 at 5:17 AM, 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>
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 the<br>
metric into a text file but it only stores the final metric value<br>
(optimizer->GetMetric()) after the registration->update() is finished. I've<br>
tried introducing ofstream into the observer but I cannot use the 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>
<div class="im">'itk::ImageRegistrationMethod<TFixedImage,TMovingImage>::SetOptimizer' :<br>
cannot convert parameter 1 from 'itk::LevenbergMarquardtOptimizer::Pointer'<br>
to 'itk::ImageRegistrationMethod<TFixedImage,TMovingImage>::OptimizerType *'<br>
</div>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>
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>
<div><div></div><div class="h5"><br>
J.X.J.<br>
<br>
<br>
Luis Ibanez wrote:<br>
><br>
> Hi J.X.J.<br>
><br>
> If the Metric is oscillating, this may be a symptom that your optimizer<br>
> is taking jumps that are too large.<br>
><br>
> I usually plot the metric values by using GNUPlot, but you could do the<br>
> same with the spreadsheet of OpenOffice, or even with Excel.<br>
><br>
> ---<br>
><br>
> About the compilation error, please post also the sentence that<br>
> follows the one that you posted. The next sentence to that one<br>
> tells what the problem is.<br>
><br>
><br>
> Thanks<br>
><br>
><br>
> Luis<br>
><br>
><br>
> ----------------------<br>
> On Sat, Aug 8, 2009 at 7:45 PM, 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>
>> thanks for your reply. I have added an observe to the code, the metric<br>
>> value<br>
>> output seems to oscillate rather than a general increase for decrease in<br>
>> value. How do you plot the metric values? Is there a way to output the<br>
>> metric value of each iteration into a text or similar file and I could<br>
>> use<br>
>> MATLAB to plot it, or is there a way to do that directly from ITK?<br>
>><br>
>> Also I've noticed that using Viola and Wells MI is REALLY slow when<br>
>> coupled<br>
>> with BSpline Deformable Transform. I've tried using Mattes MI which is<br>
>> much<br>
>> much faster in comparison in the order of 5-10 fold, why is that the<br>
>> case?<br>
>><br>
>> And finally (I'm sorry for all the questions, programming in C is not my<br>
>> forte), I tried using Levenberg Marquardt Optimizer but I get this error<br>
>> but<br>
>> I compile the code: 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>
>><br>
>> Any help is appreciated<br>
>> J.X.J.<br>
>><br>
>><br>
>> Luis Ibanez wrote:<br>
>> ><br>
>> > Hi J.X.J.<br>
>> ><br>
>> > The first thing that you want to do is to instantiate a<br>
>> Command/Observer<br>
>> > and to print out the values of the Metric at every iteration of the<br>
>> > optimizer.<br>
>> ><br>
>> > Plot this values and/or send the output to the mailing list.<br>
>> ><br>
>> > From the progression of the metric values it will be possible to<br>
>> determine<br>
>> > if parameters of the optimizer need to be adjusted, or whether you may<br>
>> > have to review other components of your registration structure.<br>
>> ><br>
>> > You will find examples on how to use Command Observers in almost<br>
>> > all the files in the directory:<br>
>> ><br>
>> > Insight/Examples/Registration<br>
>> ><br>
>> ><br>
>> > Regards,<br>
>> ><br>
>> ><br>
>> > Luis<br>
>> ><br>
>> ><br>
>> > --------------------------------------------------------------------<br>
>> > On Sat, Aug 1, 2009 at 6:49 AM, J.X.J. <<a href="mailto:wat.a.phony@gmail.com">wat.a.phony@gmail.com</a>> wrote:<br>
>> ><br>
>> >><br>
>> >> Hi everyone,<br>
>> >><br>
>> >> I'm current using itk::BSplineDeformableTransform,<br>
>> >> itk::MutualInformationImageToImageMetric and<br>
>> >> itk::GradientDescentOptimizer<br>
>> >> to register 2D MRI images. My problem is that when running 2 phantom<br>
>> >> images<br>
>> >> the output image is almost exactly the same as the original moving<br>
>> image<br>
>> >> (nothing like the fixed image).<br>
>> >><br>
>> >> I have normalized and smoothed the 2 input images using<br>
>> >> itk::NormalizeImageFilter and itk::DiscreteGaussianImageFilter with<br>
>> >> SetVariance(2.0). For mutual information the standard deviation is<br>
>> 0.4.<br>
>> >> Optimizer learning rate is 10.0, 200 iteration and with maximum on.<br>
>> This<br>
>> >> set<br>
>> >> up is kind of a mixture of a number of deformableregistration examples<br>
>> on<br>
>> >> default (as in I copied and pasted all parameter settings etc<br>
>> directly).<br>
>> >><br>
>> >> Does anyone know what the problem could be? The code runs all 200<br>
>> >> iterations<br>
>> >> is thats an indication, also the metric value of each iteration is<br>
>> around<br>
>> >> 0.2 - 0.4 if that's any help.<br>
>> >><br>
>> >> J.X.J.<br>
>> >> --<br>
>> >> View this message in context:<br>
>> >><br>
>> <a href="http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24768056.html" target="_blank">http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24768056.html</a><br>
>> >> Sent from the ITK - Users mailing list archive at Nabble.com.<br>
>> >><br>
>> >> _____________________________________<br>
>> >> Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>> >><br>
>> >> Visit other Kitware open-source projects at<br>
>> >> <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>> >><br>
>> >> Please keep messages on-topic and check the ITK FAQ at:<br>
>> >> <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
>> >><br>
>> >> Follow this link to subscribe/unsubscribe:<br>
>> >> <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
>> >><br>
>> ><br>
>> > _____________________________________<br>
>> > Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>> ><br>
>> > Visit other Kitware open-source projects at<br>
>> > <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>> ><br>
>> > Please keep messages on-topic and check the ITK FAQ at:<br>
>> > <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
>> ><br>
>> > Follow this link to subscribe/unsubscribe:<br>
>> > <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
>> ><br>
>> ><br>
>><br>
>> --<br>
>> View this message in context:<br>
>> <a href="http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24883109.html" target="_blank">http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24883109.html</a><br>
>> Sent from the ITK - Users mailing list archive at Nabble.com.<br>
>><br>
>> _____________________________________<br>
>> Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>><br>
>> Visit other Kitware open-source projects at<br>
>> <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>><br>
>> Please keep messages on-topic and check the ITK FAQ at:<br>
>> <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
>><br>
>> Follow this link to subscribe/unsubscribe:<br>
>> <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
>><br>
><br>
> _____________________________________<br>
> Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
><br>
> Visit other Kitware open-source projects at<br>
> <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
><br>
> Please keep messages on-topic and check the ITK FAQ at:<br>
> <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
><br>
> Follow this link to subscribe/unsubscribe:<br>
> <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
><br>
><br>
<br>
--<br>
</div></div>View this message in context: <a href="http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24885406.html" target="_blank">http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24885406.html</a><br>
<div><div></div><div class="h5">Sent from the ITK - Users mailing list archive at Nabble.com.<br>
<br>
_____________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at: <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
</div></div></blockquote></div><br>