[Insight-users] Question about addObserver to monitor registration

Alex Taylor Alex.Taylor at mathworks.com
Tue Jan 25 16:58:15 EST 2011


Hello,

I'm a new ITK user and I'm working my way through the Registration section of the Users Guide. The problem I'm having is the Execute method of my observer doesn't seem to be called.

I started with the following code example:

http://www.itk.org/Wiki/ITK/Examples/Registration/ImageRegistrationMethod

And I've modified it to call the addObserver method of the optimizer.

   CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
   optimizer->AddObserver( itk::IterationEvent(), observer );

   // Run gradient descent optimization to obtain estimates of transform parameters
   try {
       registrator->StartRegistration ();
   } catch( itk::ExceptionObject & excp ){
       // This call to registrator was try/catched in ppt.
       // Why? Under what conditions is an exception thrown by object?
       // mxAssert(false,"Error during registration.");
std::cout << "Registration failed. itk::ExceptionObject thrown by registrator." << std::endl;
std::cout << excp << std::endl;
   }


Here is my definition for the CommandIterationUpdate class:

#include "itkCommand.h"
#include "itkRegularStepGradientDescentOptimizer.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::RegularStepGradientDescentOptimizer OptimizerType;
   typedef const OptimizerType *OptimizerPointer;

   void Execute(itk::Object *caller, const itk::EventObject & event)
   {
       std::cout << "Non const flavor of execute called." << std::endl;
       Execute( (const itk::Object *)caller, event);
   }

   void Execute(const itk::Object * object, const itk::EventObject & event)
   {
       std::cout << "Const flavor of execute called." << std::endl;
       OptimizerPointer optimizer = dynamic_cast< OptimizerPointer >( object );
       if( ! itk::IterationEvent().CheckEvent( &event ) ) {
           return;
       }

       std::cout << optimizer->GetCurrentIteration() << " = ";
       std::cout << optimizer->GetValue() << " : ";
       std::cout << optimizer->GetCurrentPosition() << std::endl;

   }

};

When I run this code using the Examples/Data images BrainProtonDensitySliceBorder20.png and BrainProtonDensitySliceShifted13x17y.png, the registration is successful at resolving the misalignment:

tform =

   16.9979   13.0740

So I know the optimizer is running.

However, I receive no output from the cout commands in my observer. I don't even see the display statements I added to the top of each of the Execute methods. Is there something else I need to do? Is the itk::IterationEvent() the right type of event to be observing?

Thanks for your help,

Alex Taylor
Image and Geospatial Group
MathWorks
Alex.Taylor at mathworks.com<mailto:Alex.Taylor at mathworks.com>






More information about the Insight-users mailing list