Skip to content

Affine

Affine

Bases: ndarray

Affine transformation

The constructor builds a unity transformation if given no arguments, or uses the optional array for source data

Methods:

Name Description
__imatmul__

Compose two affine transformations in place

__matmul__

Compose two affine transformations

__mul__

Apply affine transformation to a point or several points

angle

Approximate rotation angle

apply

Apply an affine transformation to an image

centerofrotation

Approximate center of rotation and scaling

inverse

Inverse of an affine transformation

invert

Invert affine transformation in place

magnification

Approximate linear scaling factor

rotator

An affine transformation that represents a rotation

scaler

An affine transformation that represents a linear scaling

shift

Add a translation to an affine transformation in place

shifted

Add a translation to an affine transformation

translation

Translational part of the transformation

translator

An affine transformation that represents a translation

__imatmul__(afm)

Compose two affine transformations in place

afm1 @= afm2 incorporates afm2 into afm1, such that afm1 becomes the affine transformation afm₁ ∘ afm₂ that applies afm₁ after afm₂.

__matmul__(afm)

Compose two affine transformations

(afm1 @ afm2) returns the affine transformation afm₁ ∘ afm₂ that applies afm₁ after afm₂.

__mul__(xy)

Apply affine transformation to a point or several points

afm * pt, where pt is a 2-vector, returns the point after applying the affine transformation.

afm * pts, where pts is a 2×N array, applies the affine transformation to multiple points at once.

angle(refine=False)

Approximate rotation angle

Parameters:

Name Type Description Default
refine bool

if true, use scipy to optimize

False

Returns:

Type Description
float

angle in radians

We calculate the arctangent of the ratio between off-diagonal and diagonal elements of the linear part of the transformation. If refine is true, we then further optimize using scipy.

The value is exact if the transformation is pure scaling combined with rotation and translation.

apply(img, rect=None)

Apply an affine transformation to an image

Parameters:

Name Type Description Default
img Image

an image to transform

required
rect Optional[ArrayLike]

optional rectangle of model space

None

Returns:

Type Description
Image

the transformed image

res = afm.apply(img) returns an image the same size of img looking up pixels in the original using affine transformation.

res = afm.apply(img, rect), where rect is an (x0,y0,w,h)-tuple returns the given rectangle of model space.

Note that afm must map from image space to model space, not the other way around.

centerofrotation()

Approximate center of rotation and scaling

Returns:

Type Description
ndarray

center of rotation and scaling

Any affine transformation T: xA x + b can be written as T: xA (xc) + c. This function calculates that c.

Note that the calculation is numerically unstable near A = 𝟙.

inverse()

Inverse of an affine transformation

Returns:

Type Description
Affine

the resulting transformation

afm.INVERSE() returns an affine transformation that is the inverse of the given transformation.

invert()

Invert affine transformation in place

afm.invert() inverts the transformation.

magnification()

Approximate linear scaling factor

Returns:

Type Description
float

the scaling factor

We calculate this as the square root of the determinant of the linear part of the transformation.

The value is exact if the transformation is pure scaling combined with rotation and translation.

rotator(phi, xy0=[0, 0])

An affine transformation that represents a rotation

Parameters:

Name Type Description Default
phi float

the rotation in radians

required
xy0 ArrayLike

the point around which to rotate

[0, 0]

Returns:

Type Description
Affine

the constructed affine transformation

If xy0 is not given, the rotation is around the origin.

scaler(s, xy0=[0, 0])

An affine transformation that represents a linear scaling

Parameters:

Name Type Description Default
s float

the scale factor

required
xy0 ArrayLike

the point around which to scale

[0, 0]

Returns:

Type Description
Affine

the constructed affine transformation

If xy0 is not given, the scaling is around the origin.

The scaling is always isotropic, that is, we do not support different scale factors for x and y.

shift(dxy)

Add a translation to an affine transformation in place

Parameters:

Name Type Description Default
dxy ArrayLike

(x,y)-pair specifying the shift to be applied

required

Returns:

Type Description
Affine

self

afm.shift(dx) composes the transformation xx + dx after the affine transformation afm.

shifted(dxy)

Add a translation to an affine transformation

Parameters:

Name Type Description Default
dxy ArrayLike

(x,y)-pair specifying the shift to be applied

required

Returns:

Type Description
Affine

the new affine transformation

afm.shifted(dx) composes the transformation xx + dx after the affine transformation afm and returns the result.

translation()

Translational part of the transformation

Returns:

Type Description
ndarray

the shift as a (dx, dy)-pair.

The value is always exact.

translator(dxy)

An affine transformation that represents a translation

Parameters:

Name Type Description Default
dxy ArrayLike

a (dx, dy) pair

required

Returns:

Type Description
Affine

the constructed affine transformation