r"""Applies perspective transformation to batched 2D images. A perspective transformation is a projection of a image onto a new view plane. The input images are transformed to the output images by the transformation matrix: .. math:: \text{output}(n, c, h, w) = \text{input} \le
(
inp: Tensor,
mat: Tensor,
out_shape: Union[Tuple[int, int], int, Tensor],
mat_idx: Optional[Union[Iterable[int], Tensor]] = None,
border_mode: str = "replicate",
border_val: float = 0.0,
format: str = "NCHW",
interp_mode: str = "linear",
)
| 393 | |
| 394 | |
| 395 | def warp_perspective( |
| 396 | inp: Tensor, |
| 397 | mat: Tensor, |
| 398 | out_shape: Union[Tuple[int, int], int, Tensor], |
| 399 | mat_idx: Optional[Union[Iterable[int], Tensor]] = None, |
| 400 | border_mode: str = "replicate", |
| 401 | border_val: float = 0.0, |
| 402 | format: str = "NCHW", |
| 403 | interp_mode: str = "linear", |
| 404 | ) -> Tensor: |
| 405 | r"""Applies perspective transformation to batched 2D images. A perspective transformation is a projection of a image onto a new view plane. |
| 406 | |
| 407 | The input images are transformed to the output images by the transformation matrix: |
| 408 | |
| 409 | .. math:: |
| 410 | \text{output}(n, c, h, w) = \text{input} \left( n, c, |
| 411 | \frac{M_{00}w + M_{01}h + M_{02}}{M_{20}w + M_{21}h + M_{22}}, |
| 412 | \frac{M_{10}w + M_{11}h + M_{12}}{M_{20}w + M_{21}h + M_{22}} |
| 413 | \right) |
| 414 | |
| 415 | Optionally, we can set ``mat_idx`` to assign different transformations to the same image, |
| 416 | otherwise the input images and transformations should be one-to-one correnspondence. |
| 417 | |
| 418 | Args: |
| 419 | inp: input image. |
| 420 | mat: ``(batch, 3, 3)`` transformation matrix. |
| 421 | out_shape: ``(h, w)`` size of the output image. |
| 422 | mat_idx: image batch idx assigned to each matrix. Default: None |
| 423 | border_mode: pixel extrapolation method. |
| 424 | Default: "replicate". Currently also support "constant", "reflect", |
| 425 | "reflect_101", "wrap". |
| 426 | border_val: value used in case of a constant border. Default: 0 |
| 427 | format: NHWC" is also supported. Default: "NCHW". |
| 428 | interp_mode: interpolation methods. |
| 429 | Default: "linear". Currently only support "linear" mode. |
| 430 | |
| 431 | Returns: |
| 432 | output tensor. |
| 433 | |
| 434 | Note: |
| 435 | The transformation matrix is the inverse of that used by ``cv2.warpPerspective``. |
| 436 | |
| 437 | Examples: |
| 438 | >>> import numpy as np |
| 439 | >>> inp_shape = (1, 1, 4, 4) |
| 440 | >>> x = Tensor(np.arange(16, dtype=np.float32).reshape(inp_shape)) |
| 441 | >>> M_shape = (1, 3, 3) |
| 442 | >>> # M defines a translation: dst(1, 1, h, w) = rst(1, 1, h+1, w+1) |
| 443 | >>> M = Tensor(np.array([[1., 0., 1.], |
| 444 | ... [0., 1., 1.], |
| 445 | ... [0., 0., 1.]], dtype=np.float32).reshape(M_shape)) |
| 446 | >>> out = F.vision.warp_perspective(x, M, (2, 2)) |
| 447 | >>> out.numpy() |
| 448 | array([[[[ 5., 6.], |
| 449 | [ 9., 10.]]]], dtype=float32) |
| 450 | """ |
| 451 | if inp.dtype == np.float32: |
| 452 | mat = mat.astype("float32") |
no test coverage detected