megengine.functional.vision.warp_perspective

warp_perspective(inp, M, dsize, border_mode='REPLICATE', border_val=0.0, 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)\]
参数
  • inp (Tensor) – 输入图像。

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

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

  • border_mode (str) – 像素外推方法。默认:”REPLICATE”。当前也支持”CONSTANT”, “REFLECT”, “REFLECT_101”, “WRAP”。

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

  • interp_mode (str) – 插值方法。默认:”LINEAR”。当前只支持 “LINEAR” 模式。

返回类型

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.]]]]