Skip to content

Affine transformation order in AffineGrid wrong? #6062

Open
@Spenhouet

Description

@Spenhouet

Describe the bug
I thought that the transformation order when applying affine transformations matters since the transformations can effect each other.

I thought the correct order is:

  • Scale
  • Rotate
  • Translate

The order in AffineGrid is:

  • Scale
  • Translate
  • Shear
  • Rotate
affine = torch.eye(spatial_dims + 1, device=_device)
if self.rotate_params:
    affine = affine @ create_rotate(spatial_dims, self.rotate_params, device=_device, backend=_b)
if self.shear_params:
    affine = affine @ create_shear(spatial_dims, self.shear_params, device=_device, backend=_b)
if self.translate_params:
    affine = affine @ create_translate(spatial_dims, self.translate_params, device=_device, backend=_b)
if self.scale_params:
    affine = affine @ create_scale(spatial_dims, self.scale_params, device=_device, backend=_b)

Scaling affects the size of the object, which in turn affects the position and orientation of the object. Therefore, scaling should be applied first.
Rotation affects the orientation of the object, but not its position. Therefore, it should be applied after scaling.
Translation affects the position of the object, but not its size or orientation. Therefore, it should be applied last.
(not quite sure what to do with the order of shear and rotate)

Shouldn't this then be:

affine = torch.eye(spatial_dims + 1, device=_device)
if self.translate_params:
    affine = affine @ create_translate(spatial_dims, self.translate_params, device=_device, backend=_b)
if self.rotate_params:
    affine = affine @ create_rotate(spatial_dims, self.rotate_params, device=_device, backend=_b)
if self.shear_params:
    affine = affine @ create_shear(spatial_dims, self.shear_params, device=_device, backend=_b)
if self.scale_params:
    affine = affine @ create_scale(spatial_dims, self.scale_params, device=_device, backend=_b)

To Reproduce
N/A

Expected behavior
Correct order of affine transformations.

Screenshots
N/A

Environment
N/A

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions