# How to get intermediate transform parameter?

**URL:** https://discourse.itk.org/t/how-to-get-intermediate-transform-parameter/6631
**Category:** Beginner Questions
**Created:** [May 13, 2024, 9:24am UTC](https://discourse.itk.org/t/how-to-get-intermediate-transform-parameter/6631 "2024-05-13T09:24:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Vidya](https://discourse.itk.org/user_avatar/discourse.itk.org/vidya/32/3986_2.png) [@Vidya](https://discourse.itk.org/u/Vidya)
#### Post date: [May 13, 2024, 9:24am UTC](https://discourse.itk.org/t/how-to-get-intermediate-transform-parameter/6631/1 "2024-05-13T09:24:33Z")

</div>

Hi,  
I am following the example DeformableRegistration12 : [ITK: Examples/RegistrationITKv4/DeformableRegistration12.cxx](https://itk.org/Doxygen/html/Examples_2RegistrationITKv4_2DeformableRegistration12_8cxx-example.html#_a14) , to register two images. Is there any way to get the intermediate transform parameters or intermediate registered image after each iteration.

Thanks,

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 13, 2024, 12:29pm UTC](https://discourse.itk.org/t/how-to-get-intermediate-transform-parameter/6631/2 "2024-05-13T12:29:01Z")

</div>

Have you tried adding `OptimizerType::ParametersType finalParameters = transform->GetParameters();` or [writing the transform to file](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/IO/TransformReadWrite.cxx) somwhere [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/RegistrationITKv4/DeformableRegistration12.cxx#L109)? You will need access to `transform`, either by making a global parameter or saving a pointer to it in `CommandIterationUpdate` class.

---

<div class="post-metadata">

### Author: ![Vidya](https://discourse.itk.org/user_avatar/discourse.itk.org/vidya/32/3986_2.png) [@Vidya](https://discourse.itk.org/u/Vidya)
#### Post date: [May 14, 2024, 5:57am UTC](https://discourse.itk.org/t/how-to-get-intermediate-transform-parameter/6631/3 "2024-05-14T05:57:31Z")

</div>

> [@dzenanz](#):
>
> Have you tried adding `OptimizerType::ParametersType finalParameters = transform->GetParameters();`

I tried to add it in [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/RegistrationITKv4/DeformableRegistration12.cxx#L109) as follows.

```auto
class CommandIterationUpdate : public itk::Command
{
public:
  using Self = CommandIterationUpdate;
  using Superclass = itk::Command;
  using Pointer = itk::SmartPointer<Self>;
  itkNewMacro(Self);

protected:
  CommandIterationUpdate() = default;

public:
  using OptimizerType = itk::LBFGSBOptimizerv4;
  using OptimizerPointer = const OptimizerType *;
  using TransformType = itk::BSplineTransform<double, 2, 3>;
  using TransformPointer = const TransformType *;

  void
  Execute(itk::Object * caller, const itk::EventObject & event) override
  {
    Execute((const itk::Object *)caller, event);
  }

  void
  Execute(const itk::Object * object, const itk::EventObject & event) override
  {
    auto optimizer = static_cast<OptimizerPointer>(object);
    auto transform = static_cast<TransformPointer>(object);
    if (!(itk::IterationEvent().CheckEvent(&event)))
    {
      return;
    }
    std::cout << optimizer->GetCurrentIteration() << " ";
    std::cout << optimizer->GetCurrentMetricValue() << " ";
    std::cout << optimizer->GetInfinityNormOfProjectedGradient() << std::endl;
    auto finalParameters = transform->GetParameters();
    std::cout <<finalParameters;
  }
};

```

But it is throwing the following error.

```auto
terminate called after throwing an instance of 'std::bad_alloc'
  what(): std::bad_alloc
Aborted (core dumped)

```

And i tried to create another class `CommandIterationUpdate` for transform. But it is not giving printing the parameter value.

```auto
class CommandIterationUpdate1 : public itk::Command
{
public:
  using Self = CommandIterationUpdate1;
  using Superclass = itk::Command;
  using Pointer = itk::SmartPointer<Self>;
  itkNewMacro(Self);

protected:
  CommandIterationUpdate1() = default;

public:
  using TransformType = itk::BSplineTransform<double, 2, 3>;
  using TransformPointer = const TransformType *;

  void
  Execute(itk::Object * caller, const itk::EventObject & event) override
  {
    Execute((const itk::Object *)caller, event);
  }

  void
  Execute(const itk::Object * object, const itk::EventObject & event) override
  {
(object);
    auto transform = static_cast<TransformPointer>(object);
    if (!(itk::IterationEvent().CheckEvent(&event)))
    {
      return;
    }
    auto finalParameters = transform->GetParameters();
    std::cout <<finalParameters;
  }
};

```
