# transform center is reset to origin with composite transform during registration

**URL:** https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457
**Category:** Beginner Questions
**Created:** [November 27, 2017, 12:57pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457 "2017-11-27T12:57:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [November 27, 2017, 12:57pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457/1 "2017-11-27T12:57:20Z")

</div>

Dear all,

In my multilevel registration application, I’m using a specific initialization. So I just give parameters (scale rotation translation) + center to the transform being optimized (in place) before the registration start. This works well.

However, when I’m adding a moving initial transform (through `SetMovingInitialTransform()`, or using directly a composite transform), the center of all the transforms get reseted to `[0,0]`. All other parameters are kept as initialized. So my “superb” initialization doesn’t work as expected… I just have to uncomment the `SetMovingInitialTransform()` line, whatever the transform is (identity or not) to observe the undesired behavior.

Do anyone has any clue on why this is happening. I have started browsing itk source, but I haven’t found a possible explanation for this yet.

I’m using ITK v4, a similarity 2d transform, mean square difference metric, regular step gradient descend optimizer.

Any help would be appreciated !  
Best  
Yann

---

<div class="post-metadata">

### Author: ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)
#### Post date: [November 27, 2017, 1:55pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457/2 "2017-11-27T13:55:54Z")

</div>

Hello Yann,

It sounds more like a problem of transform composition than a problem with initial transform setting.  
Can you post a sample of your registration setup ?

Tim

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [November 27, 2017, 2:20pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457/3 "2017-11-27T14:20:52Z")

</div>

You may want to look into the `InitializeCenterOfLinearOutputTransform` IVAR.

It was introduced in the following commit:  
[https://github.com/InsightSoftwareConsortium/ITK/commit/c0f3cbb64e872a22eb046c64585795dfd973b402](https://github.com/InsightSoftwareConsortium/ITK/commit/c0f3cbb64e872a22eb046c64585795dfd973b402)

> <https://github.com/InsightSoftwareConsortium/ITK/blob/75b770992f76f605f69d1da0a908d821fc2a6016/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.hxx#L1038-L1082>

The difficulty with using composite transforms and transform centers is the space of the center of the transform is not the image space but the space of the prior composite transform stack. None of the transform initializers support a prior transform stack.

---

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [November 27, 2017, 2:44pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457/4 "2017-11-27T14:44:21Z")

</div>

Thanks both of you for your help.  
I agree with you, Tim, it seems like a composition problem.

Bradley, thanks, I’m reading this!

Here is a sample of code. I tried to make as concise as possible, and remove anything unnecessary.  
I’m not using any transform initializer, but I have written “a custom” initialization (not really fancy, based on principal moments), which gives a center for both image, as well as rotation, scaling and translation. I need composite transform to allow image flipping (mirroring) before the registration.

```
using InternalType = double;
using InternalPixelType = float;
static const unsigned int Dimension = 2;

using InternalImageType = itk::Image< InternalPixelType, Dimension > ;

using FlipTransformType = itk::AffineTransform< InternalType, Dimension >;
using TransformType = itk::Similarity2DTransform< InternalType >;

using OptimizerType = itk::RegularStepGradientDescentOptimizerv4< InternalType >;
using MetricType = itk::MeanSquaresImageToImageMetricv4 < InternalImageType, InternalImageType >;
using ScalesEstimatorType = itk::RegistrationParameterScalesFromPhysicalShift<MetricType>;
using RegistrationType = itk::ImageRegistrationMethodv4 < InternalImageType, InternalImageType, TransformType >;

// I haven't put the implementation here
observer = CommandIterationObserver::New();
command = RegistrationInterfaceCommand::New();

flipTransform = FlipTransformType::New();
optimizedTransform = TransformType::New();

metric = MetricType::New();

scalesEstimator = ScalesEstimatorType::New();
scalesEstimator->SetMetric( metric );
scalesEstimator->SetTransformForward( true );

optimizer = OptimizerType::New();
optimizer->AddObserver( itk::IterationEvent(), observer );
optimizer->SetScalesEstimator( scalesEstimator );

registration = RegistrationType::New();
registration->AddObserver( itk::MultiResolutionIterationEvent(), command );
registration->SetOptimizer( optimizer );
registration->SetMetric( metric );
registration->SetInitialTransform( optimizedTransform );

command->setInitialLearningRate(... );
command->setInitialMinimumStepLength( ... );
command->setMinimumStepLengthShrinkFactor( ... );

registration->SetFixedImage( rescalerFixed->GetOutput() );
registration->SetMovingImage( rescalerMoving->GetOutput() );

movingCenter = ...; // calculated from moments
fixedCenter = ...; // calculated from moments

// affine transform from moving image space to the same moving image space 
MomentsCalculatorType::VectorType centerAffine;
centerAffine = movingCenter;
flipTransform->SetCenter( centerAffine );
// remove some code here for the sake of conciseness
flipTransform->SetIdentity();

registration->SetMovingInitialTransform(flipTransform); // if I comment this line, everything is fine

optimizedTransform->SetCenter( fixedCenter );
optimizedTransform->SetScale( initScale );
optimizedTransform->SetAngle( initAngle );
optimizedTransform->SetTranslation( initTrans );

optimizer->SetNumberOfIterations( ... );
optimizer->SetRelaxationFactor( ... );
optimizer->SetDoEstimateLearningRateAtEachIteration( ... );
optimizer->SetDoEstimateLearningRateOnce( ... );
optimizer->SetMaximumStepSizeInPhysicalUnits( ... );
optimizer->SetGradientMagnitudeTolerance( ... );

registration->SetNumberOfLevels ( ... );
registration->SetShrinkFactorsPerLevel( ... );
registration->SetSmoothingSigmasPerLevel( ... );

// here the optimizedTransform has the proper center
// as soon as the registration begins (in the observer), center is to [0 0]
registration->Update();

```

thanks again for your help !  
Yann

---

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [November 27, 2017, 3:19pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457/5 "2017-11-27T15:19:30Z")

</div>

You had a good feeling Bradley, when I set `registration->SetInitializeCenterOfLinearOutputTransform(false);`, the center remains the same and is not reseted. So this “solves” my problem, great!

I’m wondering whether it is a desired behavior to reset the center. In this experiment, both the center of affine and the similarity transform seemed to be reset.

Anyway, as always, great thanks for your help!

---

<div class="post-metadata">

### Author: ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)
#### Post date: [November 27, 2017, 3:20pm UTC](https://discourse.itk.org/t/transform-center-is-reset-to-origin-with-composite-transform-during-registration/457/6 "2017-11-27T15:20:10Z")

</div>

If nor the automatic composition in the Registration Method nor setting manually a composed transform works, I would suggest to embed the transformations directly in the initial images i.e. resampling both your fixed and moving images to have their centers on [0,0] with the help of the pre-calculated transforms, then go for the registration process with a composite transform with center [0,0]. That should do the trick.

Edit: didn’t see the question was already solved 🙂
