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')[源代码]

Applies perspective transformation to batched 2D images.

The input images are transformed to the output images by the transformation matrix:

\[\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)\]

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) – input image.

  • mat (Tensor) – batch, 3, 3)` transformation matrix.

  • out_shape (Union[Tuple[int, int], int, Tensor]) – h, w)` size of the output image.

  • 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) – value used in case of a constant border. Default: 0

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

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

返回类型

Tensor

返回

output tensor.

注解

The transformation matrix is the inverse of that used by 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())

Outputs:

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