# Setting an Initial Transform for Elastix

**URL:** <https://discourse.itk.org/t/setting-an-initial-transform-for-elastix/6532>\
**Category:** Beginner Questions\
**Tags:** initialization, python, elastix\
**Created:** [March 19, 2024, 7:06pm UTC](https://discourse.itk.org/t/setting-an-initial-transform-for-elastix/6532 "2024-03-19T19:06:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lschuring](https://discourse.itk.org/letter_avatar_proxy/v4/letter/l/439d5e/32.png) [@lschuring](https://discourse.itk.org/u/lschuring)\
**Post date:** [March 19, 2024, 7:06pm UTC](https://discourse.itk.org/t/setting-an-initial-transform-for-elastix/6532/1 "2024-03-19T19:06:05Z")

</div>

Hi ITK community!

I am setting up a pipeline in python to register MRI image stacks which tend to have poor initial alignment. I would like to provide an elastix image filter (sitk.ElastixImageFilter()) an initial transform determined via manual inspection (I have an 1x6 array of euler rotations, followed by translations).

I can see that I need to provide my elastixImageFilter a text file in a specific format that will define the initial transform (SetInitialTransformParameterFileName), but I am finding it difficult to find information on what the content/formatting of that file should be. And in the Elastix manual it states, “The transformation is defined as a mapping from the fixed image to the moving image.” Does that still apply to the initial transform that I will provide it in the required text file?

Eventually, I aim to remove the need for manual initial alignment, but for now, I want to focus on improving the elastix transform filter portion of my pipeline before I tackle initial alignment automation.

Thank you!

---

<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:** [March 19, 2024, 8:33pm UTC](https://discourse.itk.org/t/setting-an-initial-transform-for-elastix/6532/2 "2024-03-19T20:33:51Z")

</div>

Take a look at [this comment](https://github.com/InsightSoftwareConsortium/ITKElastix/issues/79#issuecomment-878602486), in particular [this](https://github.com/KitwareMedical/HASI/blob/d58acf4cda836bdedd1b7531dddc8ca7b20b396a/src/hasi/mouse_femur_tibia_ct_morphometry.py#L155-L159) and [this](https://github.com/KitwareMedical/HASI/blob/d58acf4cda836bdedd1b7531dddc8ca7b20b396a/src/hasi/mouse_femur_tibia_ct_morphometry.py#L180) (code below):

```py
    itk.transformwrite([affine_pose_to_case], atlas_to_case_filename)
    out_elastix_transform = open(atlas_to_case_filename + '.txt', "w")
    out_elastix_transform.writelines(['(Transform "File")\n',
                                      '(TransformFileName "' + case + '-' + atlas + '.tfm")'])
    out_elastix_transform.close()

    registered, elastix_transform = itk.elastix_registration_method(
        case_bone_image.astype(itk.F), # fixed image is used as primary input to the filter
        moving_image=atlas_aa_image.astype(itk.F),
        # moving_mask=atlas_aa_segmentation,
        parameter_object=parameter_object,
        initial_transform_parameter_file_name=atlas_to_case_filename + '.txt',
        # log_to_console=True,
        output_directory=root_dir + bone + '/',
        log_file_name=case + '-' + atlas + '-elx.log'
    )

```
