medusa.transforms#

Module with functions to implement some often used transformations in Medusa.

Module Contents#

medusa.transforms.create_viewport_matrix(nx, ny, device=DEVICE)[source]#

Creates a viewport matrix that transforms vertices in NDC [-1, 1] space to viewport (screen) space. Based on a blogpost by Mauricio Poppe: https://www.mauriciopoppe.com/notes/computer-graphics/viewing/viewport- transform/ except that I added the minus sign at [1, 1], which makes sure that the viewport (screen) space origin is in the top left.

Parameters:
  • nx (int) – Number of pixels in the x dimension (width)

  • ny (int) – Number of pixels in the y dimension (height)

Returns:

mat – A 4x4 tensor representing the viewport transform

Return type:

torch.tensor

medusa.transforms.create_ortho_matrix(nx, ny, znear=0.05, zfar=100.0, device=DEVICE)[source]#

Creates an orthographic projection matrix, as used by EMOCA/DECA. Based on the pyrender implementation. Assumes an xmag and ymag of 1.

Parameters:
  • nx (int) – Number of pixels in the x-dimension (width)

  • ny (int) – Number of pixels in the y-dimension (height)

  • znear (float) – Near clipping plane distance (from eye/camera)

  • zfar (float) – Far clipping plane distance (from eye/camera)

Returns:

mat – A 4x4 affine matrix

Return type:

torch.tensor

medusa.transforms.crop_matrix_to_3d(mat_33)[source]#

Transforms a 3x3 matrix used for cropping (on 2D coordinates) into a 4x4 matrix that can be used to transform 3D vertices. It assumes that there is no rotation element.

Parameters:

mat_33 (torch.tensor) – A 3x3 affine matrix

Returns:

mat_44 – A 4x4 affine matrix

Return type:

torch.tensor

medusa.transforms.apply_perspective_projection(v, mat)[source]#

” Applies a perspective projection of v into NDC space.

Parameters:
  • v (torch.tensor) – A 2D (vertices x XYZ) tensor with vertex data

  • mat (torch.tensor) – A 4x4 perspective projection matrix

medusa.transforms.embed_points_in_mesh(v, f, p)[source]#

Embed points in an existing mesh by finding the face it is contained in and computing its barycentric coordinates. Works with either 2D or 3D data.

Parameters:
  • v (np.ndarray) – Vertices of the existing mesh (a 2D vertices x [2 or 3] array)

  • f (np.ndarray) – Faces (polygons) of the existing mesh (a 2D faces x [2 or 3] array)

  • p (np.ndarray) – Points (vertices) to embed (a 2D vertices x [2 or 3] array)

Returns:

  • triangles (np.ndarray) – A 1D array with faces corresponding to the vertices of p

  • bcoords (np.ndarray) – A 2D array (vertices x 3) array with barycentric coordinates

medusa.transforms.project_points_from_embedding(v, f, triangles, bcoords)[source]#

Project points (vertices) from an existing embedding into a different space.

Parameters:
  • v (np.ndarray) – Points (vertices) to project (\(N imes 3\))

  • f (np.ndarray) – Faces of original mesh

  • triangles (np.ndarray) –

medusa.transforms.estimate_similarity_transform(src, dst, estimate_scale=True)[source]#

Estimate a similarity transformation matrix for two batches of points with N observations and M dimensions; reimplementation of the _umeyama function of the scikit-image package.

Parameters:
  • src (torch.Tensor) – A tensor with shape batch_size x N x M

  • dst (torch.Tensor) – A tensor with shape batch_size x N x M

  • estimate_scale (bool) – Whether to also estimate a scale parameter

Raises:

ValueError – When N (number of points) < M (number of dimensions)

medusa.transforms.resize_with_pad(imgs, output_size=(224, 224), out_dtype=torch.uint8, **kwargs)[source]#

Resizes image with right-bottom padding (with zeros), as used in insightface’s SCRFD detector.

Parameters:
  • imgs (torch.tensor) – Tensor of shape b x 3 x h x w (batch dimension is optional)

  • output_size (tuple) – Desired output shape (width, heigth)

  • out_dtype (torch dtype) – Output datatype

  • kwargs (dict) – Keyword arguments passed to kornia.geometry.transform.resize

Returns:

imgs – Resized images of shape b x 3 x new_h x new_w

Return type:

torch.tensor

medusa.transforms.euler_matrix(ai, aj, ak, axes='sxyz')[source]#

Return homogeneous rotation matrix from Euler angles and axis sequence.

medusa.transforms.compose_matrix(scale=None, shear=None, angles=None, translate=None, perspective=None)[source]#

Return transformation matrix from sequence of transformations.

medusa.transforms.vector_norm(data, axis=None, out=None)[source]#

Return length, i.e. Euclidean norm, of ndarray along axis.

medusa.transforms.decompose_matrix(matrix)[source]#

Return sequence of transformations from transformation matrix.