megengine.functional.vision.warp_perspective

warp_perspective(inp, mat, out_shape, mat_idx=None, border_mode='replicate', border_val=0.0, format='NCHW', interp_mode='linear')[源代码]

对按批组织的二维图像进行透视变换。

输入图像通过变换矩阵变换为输出图像:

\[\text{output}(n, c, h, w) = \text{input} \left( n, c, \frac{M_{00}h + M_{01}w + M_{02}}{M_{20}h + M_{21}w + M_{22}}, \frac{M_{10}h + M_{11}w + M_{12}}{M_{20}h + M_{21}w + M_{22}} \right)\]

Optionally, we can set mat_idx to assign different transformations to the same image, otherwise the input images and transformations should be one-to-one correnspondence.

参数
  • inp (Tensor) – 输入图像。

  • mat (Tensor) – (batch, 3, 3) 变换矩阵。

  • out_shape (Union[Tuple[int, int], int, Tensor]) – (h, w) 输出图像的大小。

  • mat_idx (Union[Iterable[int], Tensor, None]) – (batch, ) image batch idx assigned to each matrix. Default: None

  • border_mode (str) – pixel extrapolation method. Default: “replicate”. Currently also support “constant”, “reflect”, “reflect_101”, “wrap”.

  • border_val (float) – 边界填充值。 默认:0

  • format (str) – “NHWC” is also supported. Default: “NCHW”.

  • interp_mode (str) – interpolation methods. Default: “linear”. Currently only support “linear” mode.

返回类型

Tensor

返回

输出张量。

注解

转换矩阵是 cv2.warpPerspective 使用的矩阵的逆矩阵。

例如:

import numpy as np
from megengine import tensor
import megengine.functional as F

inp_shape = (1, 1, 4, 4)
x = tensor(np.arange(16, dtype=np.float32).reshape(inp_shape))
M_shape = (1, 3, 3)
# M defines a translation: dst(1, 1, h, w) = rst(1, 1, h+1, w+1)
M = tensor(np.array([[1., 0., 1.],
                     [0., 1., 1.],
                     [0., 0., 1.]], dtype=np.float32).reshape(M_shape))
out = F.vision.warp_perspective(x, M, (2, 2))
print(out.numpy())

输出:

[[[[ 5.  6.]
   [ 9. 10.]]]]