Image Transformations¶
The functions in this section perform various geometrical transformations of 2D images.
-
warp_perspective
(src, M, dsize, flags='bilinear', border_mode=None, border_value=0)[source]¶ Applies a perspective transformation to an image.
The function warp_perspective transforms the source image using the specified matrix:
\[\text{dst} (x, y) = \text{src} \left( \frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} , \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}} \right )\]Parameters: - src (torch.Tensor) – input image.
- M (Tensor) – transformation matrix.
- dsize (tuple) – size of the output image (height, width).
Returns: the warped input image.
Return type: Tensor
- Shape:
- Input: \((B, C, H, W)\) and \((B, 3, 3)\)
- Output: \((B, C, H, W)\)
Note
See a working example here.
-
warp_affine
(src: torch.Tensor, M: torch.Tensor, dsize: Tuple[int, int], flags: Optional[str] = 'bilinear', padding_mode: Optional[str] = 'zeros') → torch.Tensor[source]¶ Applies an affine transformation to a tensor.
The function warp_affine transforms the source tensor using the specified matrix:
\[\text{dst}(x, y) = \text{src} \left( M_{11} x + M_{12} y + M_{13} , M_{21} x + M_{22} y + M_{23} \right )\]Parameters: - src (torch.Tensor) – input tensor of shape \((B, C, H, W)\).
- M (torch.Tensor) – affine transformation of shape \((B, 2, 3)\).
- dsize (Tuple[int, int]) – size of the output image (height, width).
- mode (Optional[str]) – interpolation mode to calculate output values ‘bilinear’ | ‘nearest’. Default: ‘bilinear’.
- padding_mode (Optional[str]) – padding mode for outside grid values ‘zeros’ | ‘border’ | ‘reflection’. Default: ‘zeros’.
Returns: the warped tensor.
Return type: - Shape:
- Output: \((B, C, H, W)\)
Note
See a working example here.
-
get_perspective_transform
(src, dst)[source]¶ Calculates a perspective transform from four pairs of the corresponding points.
The function calculates the matrix of a perspective transform so that:
\[\begin{split}\begin{bmatrix} t_{i}x_{i}^{'} \\ t_{i}y_{i}^{'} \\ t_{i} \\ \end{bmatrix} = \textbf{map_matrix} \cdot \begin{bmatrix} x_{i} \\ y_{i} \\ 1 \\ \end{bmatrix}\end{split}\]where
\[dst(i) = (x_{i}^{'},y_{i}^{'}), src(i) = (x_{i}, y_{i}), i = 0,1,2,3\]Parameters: - src (Tensor) – coordinates of quadrangle vertices in the source image.
- dst (Tensor) – coordinates of the corresponding quadrangle vertices in the destination image.
Returns: the perspective transformation.
Return type: Tensor
- Shape:
- Input: \((B, 4, 2)\) and \((B, 4, 2)\)
- Output: \((B, 3, 3)\)
-
get_rotation_matrix2d
(center, angle, scale)[source]¶ Calculates an affine matrix of 2D rotation.
The function calculates the following matrix:
\[\begin{split}\begin{bmatrix} \alpha & \beta & (1 - \alpha) \cdot \text{x} - \beta \cdot \text{y} \\ -\beta & \alpha & \beta \cdot \text{x} + (1 - \alpha) \cdot \text{y} \end{bmatrix}\end{split}\]where
\[\begin{split}\alpha = \text{scale} \cdot cos(\text{angle}) \\ \beta = \text{scale} \cdot sin(\text{angle})\end{split}\]The transformation maps the rotation center to itself If this is not the target, adjust the shift.
Parameters: - center (Tensor) – center of the rotation in the source image.
- angle (Tensor) – rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
- scale (Tensor) – isotropic scale factor.
Returns: the affine matrix of 2D rotation.
Return type: Tensor
- Shape:
- Input: \((B, 2)\), \((B)\) and \((B)\)
- Output: \((B, 2, 3)\)
Example
>>> center = torch.zeros(1, 2) >>> scale = torch.ones(1) >>> angle = 45. * torch.ones(1) >>> M = tgm.get_rotation_matrix2d(center, angle, scale) tensor([[[ 0.7071, 0.7071, 0.0000], [-0.7071, 0.7071, 0.0000]]])