[Insight-users] call metric in Observer code

Luis Ibanez luis.ibanez at kitware.com
Thu Nov 26 13:56:05 EST 2009


Serena,

The "caller" argument of the Execute method is an Observer
is the pointer to the object that invoked the event.

In your particular case, this is the Optimizer.

As Bill explained,
you can only cast "caller" to the OptimizerType.


For all the other elements of the registration framework, you have
to pass them through other means to the command.

One way of doing this is to have them as Member variables,
and to have "Set" methods for them.

Like

> 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;
>
>        typedef itk::OrientedImage< float, 3 >  FixedImageType;
>        typedef   const  FixedImageType*  FixedImagePointer;
>
>        typedef itk::OrientedImage< float, 3 >  MovingImageType;
>
>        typedef itk::MattesMutualInformationImageToImageMetric<
> FixedImageType, MovingImageType
>>
>> MetricType;
>
>        typedef   const MetricType*  MetricPointer;
>
>        typedef itk::BSplineDeformableTransform< double,3,3 >
> DeformableTransformType;
>        typedef   const DeformableTransformType*  TransformPointer;
>
MetricPointer            m_Metric;
TransformPointer    m_Transform;
FixedImagePointer    m_FixedImage;
MovingImagePointer    m_MovingImage;


public:

   SetTransform( TransformType * transform ) {  m_Transform = transform; }
   SetFixedImage( FixedImageType * fixed ) {  m_FixedImage = fixed; }
   SetMovingImage( MovingImageType * moving ) {  m_MovingImage = moving; }


and then you can use them in your Execute method.

Also,
when you connect the Command to the optimizer,
you should also do:


    observer->SetFixedImage( fixedImage );
    observer->SetMovingImage( movingImage );
    observer->SetTransform( transform );

    optimizer->AddObserver( itk::IterationEvent(), observer );

In this way, the components of the registration framework
will be available to the internals of your observer class.


 Regards,


          Luis


-------------------------------------------------------------------------------
On Wed, Nov 25, 2009 at 8:51 PM, Serena Fabbri <fabbri at u.washington.edu> wrote:
>
> Hi All,
>
> I am registering CT image and MRI image with Mattes MI and Bspline
> Transformation.
> I'd like to know the value of MI of a little area of the fixed image during
> the registration process.
> I call the metric in the observer code but i get a bus error.
>
> could anybody suggest me where the error is?
>
> Thank you very much.
>
> Serena
>
>
>
>
>
> 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;
>
>        typedef itk::OrientedImage< float, 3 >  FixedImageType;
>        typedef   const  FixedImageType*  FixedImagePointer;
>
>        typedef itk::OrientedImage< float, 3 >  MovingImageType;
>
>        typedef itk::MattesMutualInformationImageToImageMetric<
> FixedImageType, MovingImageType
>>
>> MetricType;
>
>        typedef   const MetricType*  MetricPointer;
>
>        typedef itk::BSplineDeformableTransform< double,3,3 >
> DeformableTransformType;
>        typedef   const DeformableTransformType*  TransformPointer;
>
>
>
>
>  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 );
>                MetricPointer metric = dynamic_cast< MetricPointer >( object
> );
>                TransformPointer transform = dynamic_cast< TransformPointer
>>( object );
>            FixedImagePointer fixedImage = dynamic_cast< FixedImagePointer>(
> object );
>                FixedImageType::IndexType start;
>                FixedImageType::SizeType size;
>
>                 start[0]=43;
>                 start[1]=30;
>                 start[2]=0;
>
>                 size[0]=10;
>                 size[1]=17;
>                 size[2]=20;
>
>
>                 FixedImageType::RegionType fixedRegionMetric ;
>                 fixedRegionMetric.SetIndex(start);
>                 fixedRegionMetric.SetSize(size);
>
>                OptimizerType::ParametersType parameters;
>
>
>      if( !(itk::IterationEvent().CheckEvent( &event )) )
>        {
>        return;
>        }
>            metric->SetFixedImageRegion(fixedRegionMetric);
>                metric->SetUseAllPixels( true );
>            parameters = transform->GetParameters();
>
>
>          std::cout << optimizer->GetCurrentIteration() << "MIglobal   "<<
> optimizer->GetValue() << " MIlocal  "<<metric->GetValue(parameters);
>      std::cout << std::endl;
>
>
>
>    }
> };
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.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
>


More information about the Insight-users mailing list