mt.geo2d.affine

Functions

mt.geo2d.affine.transform_Aff2d_on_Moments2d(aff_tfm, moments)

Transform a Moments2d using a 2D affine transformation.

Parameters:
  • aff_tfm (Aff2d) – 2D affine transformation

  • moments (Moments2d) – 2D moments

Returns:

affined-transformed 2D moments

Return type:

Moments2d

mt.geo2d.affine.transform_Aff2d_on_PointList2d(aff_tfm, point_list)

Transform a 2D point list using a 2D affine transformation.

Parameters:
  • aff_tfm (Aff2d) – a 2D affine transformation

  • point_list (PointList2d) – a 2D point list

Returns:

affine-transformed point list

Return type:

PointList2d

mt.geo2d.affine.transform_Aff2d_on_ndarray(aff_tfm, point_array)

Transform an array of 2D points using a 2D affine transformation.

Parameters:
  • aff_tfm (Aff) – a 2D affine transformation

  • point_array (numpy.ndarray with last dimension having the same length as the dimensionality of the transformation) – an array of 2D points

Returns:

affine-transformed point array

Return type:

numpy.ndarray

mt.geo2d.affine.transform_Aff2d_on_Polygon(aff_tfm, poly)

Transform a polygon using a 2D affine transformation.

Parameters:
  • aff_tfm (Aff2d) – a 2D affine transformation

  • poly (Polygon) – a 2D polygon

Returns:

affine-transformed polygon

Return type:

Polygon

mt.geo2d.affine.swapAxes2d()

Returns the affine transformation that swaps the x-axis with the y-axis.

mt.geo2d.affine.flipLR2d(width)

Returns a left-right flip for a given width.

mt.geo2d.affine.flipUD2d(height)

Returns a up-down flip for a given height.

mt.geo2d.affine.shearX2d(h)

Returns the shearing along the x-axis.

mt.geo2d.affine.shearY2d(h)

Returns the shearing along the y-axis.

mt.geo2d.affine.originate2d(tfm, x, y)

Tweaks a 2D affine transformation so that it acts as if it originates at (x,y) instead of (0,0).

mt.geo2d.affine.rotate2d(theta, x, y)

Returns the rotation about a reference point (x,y). Theta is in radian.

mt.geo2d.affine.translate2d(x, y)

Returns the translation.

mt.geo2d.affine.scale2d(scale_x=1, scale_y=None)

Returns the scaling.

mt.geo2d.affine.crop2d(tl, br=None)

Transforms an axis-aligned rectangle into [(0,0),(1,1)].

Parameters:
  • tl (2d point (x,y)) – coordinates to be mapped to (0,0) if br is specified. If br is not specified, then the transformation is 2d scaling. In other words, (0,0) is mapped to (0,0) and tl is mapped to (1,1).

  • br (2d point (x,y), optional) – If specified, coordinates to be mapped to (1,1).

Returns:

A transformation that maps points in [(0,0),(1,1)] to the crop given by tl and br.

Return type:

Aff2d

mt.geo2d.affine.crop_rect(r: Rect) Aff2d

Transforms an-axis-aligned Rect into [(0,0),(1,1)].

Parameters:

r (Rect) – the input rectangle

Returns:

A transformation that maps points in [(0,0),(1,1)] to the crop given by tl and br.

Return type:

Aff2d

mt.geo2d.affine.uncrop_rect(tfm: Aff2d) Rect

Transforms a 2D affine transformation back to a crop, ignoring shearing and rotation.

Parameters:

tfm (Aff2d) – A 2D affine transformation supposedly transforms a crop to [(0,0),(1,1)]

Returns:

the recovered rectangle

Return type:

Rect

mt.geo2d.affine.rect2rect(src_rect: Rect, dst_rect: Rect, eps=1e-07) Aff2d

Returns an Aff2d that transforms pixels in a source Rect to pixels in a destination Rect.

The transformation ensures that the source corner pixels match with the destination corner pixels.

Parameters:
  • src_rect (Rect) – source rectangle

  • dst_rect (Rect) – destination rectangle

  • eps (float) – epsilon to detect zero source width or height

Returns:

the output transformation

Return type:

Aff2d

mt.geo2d.affine.rect2rect_tf(src_rects, dst_rects)

A tensorflow version of rect2rect() where inputs and outputs are replaced by tensors.

dst_rectstf.Tensor

destination rectangles of shape [batch, 4] where each batch item is [min_x, min_y, max_x, max_y]

Returns:

tfm_array – the output transformations of shape [batch, 3, 3] where each item is a 3x3 affine transformation matrix mapping source coordinates to destination coordinates

Return type:

tf.Tensor

Classes

  • Aff2d: Affine transformation in 2D.

class mt.geo2d.affine.Aff2d(offset=array([0., 0.]), linear: ndarray | mat2x2 | vec4 = mat2x2((1, 0), (0, 1)))

Affine transformation in 2D.

The 2D affine transformation defined here consists of a linear/weight part and an offset/bias part.

offset

the bias vector

Type:

glm.vec2

bias

the numpy view of offset

Type:

numpy.ndarray

linear

the weight matrix

Type:

glm.mat2

weight

the numpy view of linear

Type:

numpy.ndarray

References

Inheritance

digraph inheritance8f01ef993f { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "Aff" [URL="../mt.geond/mt.geond.affine.html#mt.geond.affine.Aff",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A transformer to perform affine transformations using the same transformation matrix in n-dim space."]; "LieTransformer" -> "Aff" [arrowsize=0.5,style="setlinewidth(0.5)"]; "GeometricObject" -> "Aff" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Aff2d" [URL="#mt.geo2d.affine.Aff2d",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Affine transformation in 2D."]; "TwoD" -> "Aff2d" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Aff" -> "Aff2d" [arrowsize=0.5,style="setlinewidth(0.5)"]; "GeometricObject" [URL="../mt.geo/mt.geo.object.html#mt.geo.object.GeometricObject",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A geometric object which lives in a d-dimensional Euclidean space."]; "InvertibleTransformer" [URL="../mt.geo/mt.geo.transformation.html#mt.geo.transformation.InvertibleTransformer",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A mixin asserting that this is a class of invertible transformers. "]; "Transformer" -> "InvertibleTransformer" [arrowsize=0.5,style="setlinewidth(0.5)"]; "LieTransformer" [URL="../mt.geo/mt.geo.transformation.html#mt.geo.transformation.LieTransformer",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A mixin asserting that this is a class of transformers living in a Lie group, supporting the associative multiplication operator and the inversion operator. "]; "InvertibleTransformer" -> "LieTransformer" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Transformer" [URL="../mt.geo/mt.geo.transformation.html#mt.geo.transformation.Transformer",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A mixin asserting that this is a class of transformers."]; "TwoD" [URL="../mt.geo/mt.geo.object.html#mt.geo.object.TwoD",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Mixin to assert that the geometric object lives in 2D Euclidean space."]; }
__init__(offset=array([0., 0.]), linear: ndarray | mat2x2 | vec4 = mat2x2((1, 0), (0, 1)))
__repr__()

Return repr(self).

property bias

The bias component of the affine transformation matrix.

property bias_dim

Returns the dimension of the bias vector.

property det

Returns the determinant of the transformation matrix.

static from_matrix(mat: ndarray)

Obtains an Aff2d instance from a non-singular affine transformation matrix.

Parameters:

mat (a 3x3 array) – a non-singular affine transformation matrix

Returns:

An instance representing the transformation

Return type:

Aff2d

Notes

For speed reasons, no checking is involved.

invert()

Lie inverse

property matrix

Returns the transformation matrix.

multiply(other)

a*b = Lie operator

property ndim

the dimensionality, which is 2

property weight

The weight/linear component of the affine transformation matrix.

property weight_shape

Returns the shape of the weight matrix.