Skip to content

affine transformation is not working as expected (monai0.4.0) #1600

@YufanHe

Description

@YufanHe

The parameters on the affine transformation is not corresponding to the generated image

image = np.zeros((128,128))
image[30:80, 30:80] = 1 
'''The affine transformations are applied in rotate, shear, translate, scale order.'''
fixed = Affine(rotate_params=0, translate_params=[0,0], scale_params=[0.5,0.5], shear_params=[0,0], \
                mode='bilinear', padding_mode='zeros')(image[np.newaxis], spatial_size=image.shape)[0]
plt.imshow(image);plt.show();
plt.imshow(fixed);plt.show();

image

The scale is 0.5 but the generated image is larger. The problem comes from not inversing the affine matrix for the affine_grid
as in
https://kornia.readthedocs.io/en/latest/_modules/kornia/geometry/transform/imgwarp.html#warp_affine

Also the documentation says 'The affine transformations are applied in rotate, shear, translate, scale order but this is reversed.
The source code from the AffineGrid in monai:

from monai.transforms.utils import (
    create_control_grid,
    create_grid,
    create_rotate,
    create_scale,
    create_shear,
    create_translate,
)
def get_affine_monai(rotate_params, shear_params, translate_params, scale_params, spatial_dims=2):
    affine = np.eye(spatial_dims + 1)
    affine = affine @ create_rotate(spatial_dims, rotate_params)
    affine = affine @ create_shear(spatial_dims, shear_params)
    affine = affine @ create_translate(spatial_dims, translate_params)
    affine = affine @ create_scale(spatial_dims, scale_params)
    return affine 
print(get_affine_monai(0, [0,0],[40,40],2))

image

This affine matrix shows that the scaling is not affecting the translation, and from the code we can see that the scaling is before translation (Affine = rotate * shear * translate * scale) * [x, y ,1]

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions