[Insight-users] Helo with bspline/mutual information/gradient descent

J.X.J. wat.a.phony at gmail.com
Sun Aug 9 17:03:37 EDT 2009


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

-- 
View this message in context: http://www.nabble.com/Helo-with-bspline-mutual-information-gradient-descent-tp24768056p24891166.html
Sent from the ITK - Users mailing list archive at Nabble.com.



More information about the Insight-users mailing list