Skip to content

Wrong matrix calculation for shear angle in AffineTransform #3239

Closed

Description

@UlrichBernert68

Description

The convenience method of constructing an affine transform by providing a shear angle creates a wrong internal transform matrix - the diagonal elements should be 1.0 and in the second column it isn't. The -tan(angle) value is also wrong (apparently mis-scaled the same way as the diagola element.

Way to reproduce

import numpy as np
from skimage import transform
import math as m

angle = 0.2

shear_transform = transform.AffineTransform(shear=angle)
print("shear-convenience-constructor:")
print(shear_transform.params)
print()

matrix = np.array([[1.0, -m.tan(angle), 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
affine_transform = transform.AffineTransform(matrix=matrix)
print("correct matrix constructor:")
print(affine_transform.params)

Activity

  1. hmaarrfk commented on Aug 6, 2018

    @hmaarrfk
    Member

    Wolfram would say you are right:
    http://mathworld.wolfram.com/Shear.html

    This line of code and the documentation say it is explicit @ahojnnes

    Has the following form::
    X = a0*x + a1*y + a2 =
    = sx*x*cos(rotation) - sy*y*sin(rotation + shear) + a2
    Y = b0*x + b1*y + b2 =
    = sx*x*sin(rotation) + sy*y*cos(rotation + shear) + b2

    self.params = np.array([
    [sx * math.cos(rotation), -sy * math.sin(rotation + shear), 0],
    [sx * math.sin(rotation), sy * math.cos(rotation + shear), 0],
    [ 0, 0, 1]

    Shear should not be a 1D parameter should it? I feel like you should have shear_x shear_y? or shear (y, x) (well (x, y) because the transform module is anti-python)

    For reference, opencv doesn't provide a shortcut for shear
    https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html

  2. thomasaarholt commented on Dec 31, 2020

    @thomasaarholt

    I've written a notebook (HTML IPYNB that demonstrates how the shear transform is incorrect (as well as a bunch of other peculiarities with how Affine Transforms are applied in python).

    The relevant discussion is here (scroll up one paragraph for some discussion). The following image shows the problem, scikit-image implementation at the bottom:

    image

  3. thomasaarholt commented on Dec 31, 2020

    @thomasaarholt

    The matplotlib.transforms.Affine2D() function works very well as a substitute. Since skimage has matplotlib as a dependency, maybe one should just use that library instead? As documented in my notebook above, with the exception of the erroneous shear function, the implementation is identical (and supports chaining methods, which is nice).

  4. locked and limited conversation to collaborators on Oct 18, 2021
  5. added this to the skimage2 milestone on Mar 30, 2022
  6. unlocked this conversation on Mar 30, 2022
  7. modified the milestones: skimage2, 0.21 on Mar 13, 2023
  8. changed the title [-]shear transform bug[/-] [+]Wrong matrix calculation for shear angle in `AffineTransform`[/+] on Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions