spyrit.core.warp.AffineDeformationField
- class spyrit.core.warp.AffineDeformationField(func, time_vector: tensor, img_shape: tuple)[source]
Bases:
DeformationFieldStores an affine deformation field andn uses it to compute a discrete deformation field
DeformationField.Warps an image or 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 inverse of the affine transformation matrix, and the image is warped according to the inverse deformation field \(u\).
Contrary to
DeformationField, this class can warp images of variable sizes, as the inverse deformation field \(u\) is computed from the affine transformation matrix at the desired spatial resolution.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.img_h(int): Height of the image to be warped in pixels.img_w(int): Width of the image to be warped in pixels.- 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 vectorized image or batch of vectorized images with the stored inverse deformation field \(u\).