<jupyter_start><jupyter_text>9. Simple Transformix After image registrations it is often useful to apply the transformation as found by the registration to another image. Maybe you want to apply the transformation to an original (larger) image to gain resolution. Or maybe you need the transformation to apply it to a label image (segmentation). Transformix is an image filter that was developed together with elastix, that can be used to do these transformations.  Elastix<jupyter_code># First two imports are currently necessary to run ITKElastix on MacOs
from itk import itkElastixRegistrationMethodPython
from itk import itkTransformixFilterPython
import itk
from itkwidgets import compare, checkerboard
# Import Images
fixed_image = itk.imread('data/CT_2D_head_fixed.mha', itk.F)
moving_image = itk.imread('data/CT_2D_head_moving.mha', itk.F)

# Import Default Parameter Map
parameter_object = itk.ParameterObject.New()
parameter_map_rigid = parameter_object.GetDefaultParameterMap('rigid')
parameter_object.AddParameterMap(parameter_map_rigid)<jupyter_output><empty_output><jupyter_text>Registration with the registration function...<jupyter_code># Call registration function
result_image, result_transform_parameters = itk.elastix_registration_method(
    fixed_image, moving_image,
    parameter_object=parameter_object)<jupyter_output><empty_output><jupyter_text>TransformixThe transform parameters that elastix outputs can be given to transformix as input for the transformations. The output transform parameters from elastix are mappings from the fixed image to the moving image domain. Transformix therefore uses a backwards mapping to obtain a registered version of the moving image (moving -> fixed domain).<jupyter_code># Import Image to transform
# In this example the same moving image is used, that was used for elastix,
# this however will result in the same image as was already given by the 
# first elastix registration.
moving_image_transformix = itk.imread('data/CT_2D_head_moving.mha', itk.F)<jupyter_output><empty_output><jupyter_text>Transformation can either be done in one line with the transformix function...<jupyter_code>result_image_transformix = itk.transformix_filter(
    moving_image=moving_image,
    transform_parameter_object=result_transform_parameters)<jupyter_output><empty_output><jupyter_text>.. or by initiating an transformix image filter object similar to the elastix algorithm.<jupyter_code># Load Transformix Object
transformix_object = itk.TransformixFilter.New()
transformix_object.SetMovingImage(moving_image_transformix)
transformix_object.SetTransformParameterObject(result_transform_parameters)

# Update object (required)
transformix_object.UpdateLargestPossibleRegion()

# Results of Transformation
result_image_transformix = transformix_object.GetOutput()<jupyter_output><empty_output><jupyter_text>VisualizationThe results of the image transform can be visualized with widgets from the itkwidget library such as the checkerboard and compare widgets.<jupyter_code>checkerboard(fixed_image, result_image_transformix, pattern=5)
compare(fixed_image, result_image_transformix, link_cmap=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.056954059156702326, 'fraction_numerical': 0.0015733165512901196, 'mean_word_length': 7.685792349726776, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '221717', 'license': 'Apache-2.0', 'n_tokens_mistral': 897, 'n_tokens_neox': 863, 'n_words': 284, 'path': 'examples/ITK_Example9_SimpleTransformix.ipynb', 'repo_name': 'N-Dekker/ITKElastix', 'size': '3178'}