megengine.functional.nn.warp_perspective#

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

将透视变换应用于批处理的2D图像上,透视变换是将图像投影到一个新的界面上。

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

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

可选参数,可以设置 mat_idx 来为统一图像分配不同的转换,否则输入图像和转换应该是一一对应的。

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

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

  • out_shape (Union[Tuple[int, int], int, Tensor]) – (h, w) 的输出图片

  • mat_idx (Union[Iterable[int], Tensor, None]) – 分配给每个矩阵的没批图像的编号,默认值:None

  • border_mode (str) – 边界处理方式。默认是:“replicate”. 现在也支持 “constant”, “reflect”, “reflect_101”, “wrap”.

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

  • format (str) – NHWC“ 也支持。默认:”NCHW“.

  • interp_mode (str) – 插值方式。默认:”linear“。现在只支持 “linear” 的模式。

返回类型:

Tensor

返回:

输出张量。

备注

变换矩阵是 cv2.warpPerspective 中变换矩阵的逆矩阵

实际案例

>>> import numpy as np
>>> 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))
>>> out.numpy()
array([[[[ 5.,  6.],
         [ 9., 10.]]]], dtype=float32)