spyrit.core.warp.AffineDeformationField

class spyrit.core.warp.AffineDeformationField(func, time_vector: tensor, img_shape: tuple)[source]

Bases: DeformationField

Stores an affine deformation field as a 3x3 matrix.

Warps a batch of images according to an inverse affine deformation field \(u\), i.e. the field that maps the deformed image pixel coordinates to the original image pixel coordinates.

It is constructed from a function of one parameter (time) that returns a tensor of shape \((3,3)\) representing a 2D affine homogeneous transformation matrix. The homogeneous transformation matrix corresponds to the inverse deformation field \(u\), i.e. the field that maps the pixels of the deformed image to the pixels of the original image.

To warp an image, the affine transformation matrix is evaluated at each time corresponding to the frames of the animation. The inverse deformation field \(u\) is then computed from the affine transformation matrix, and the image is warped according to the inverse deformation field \(u\).

The image size is requested upon construction, but the warping can be done with images of different sizes. The grid is simply interpolated to match the image size. It is also possible to change the image size after construction by setting the attribute img_shape, or the attributes img_h and img_w.

Important

The coordinates are given in the range [-1;1]. When referring to a pixel, its position is the position of its center. The position [-1;-1] corresponds to the center of the top-left pixel.

Args:

func (Callable: float -> torch.tensor): Function of one parameter (time) that returns a tensor of shape \((3,3)\) representing a 2D affine homogeneous transformation matrix, the inverse deformation field \(u\), i.e. the field that maps the pixels of the deformed image to the pixels of the original image.

Attributes:

self.func (function of one parameter): Function of one parameter (time) that returns a tensor of shape \((3,3)\) representing a 2D affine homogeneous transformation matrix.

self.field (torch.tensor): Inverse deformation field \(u\) of shape \((n\_frames,h,w,2)\).

time_vector (torch.tensor): List of the times at which the function is evaluated to generate the inverse deformation field.

self.n_frames (int): Number of frames in the animation.

self.img_shape (tuple): Shape of the image to be warped, i.e. \((h,w)\), where \(h\) and \(w\) are the height and width of the image respectively. This attribute can be set to change the image size.

img_h (int): Height of the image to be warped in pixels. This attribute can be set to change the image size.

img_w (int): Width of the image to be warped in pixels. This attribute can be set to change the image size.

Example 1: Progressive zooming in
>>> def u(t):
...     return torch.tensor([[1-t/10, 0, 0], [0, 1-t/10, 0], [0, 0, 1]])
>>> field = AffineDeformationField(u)
Example 2: Rotation of an image counter-clockwise, at a frequency of 1Hz
>>> import numpy as np
>>> def s(t):
...     return np.sin(2*np.pi*t)
>>> def c(t):
...     return np.cos(2*np.pi*t)
>>> def u(t):
...     return torch.tensor([[c(t), s(t), 0], [-s(t), c(t), 0], [0, 0, 1]])
>>> field = AffineDeformationField(u)

Methods

forward(img[, n0, n1, mode])

Warps a batch of 2D images with the stored inverse deformation field \(u\).

grid_sample(img_frames, inverse_grid_frames, ...)

Used to warp frames of 2D images with a deformation field.