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)\]- 参数
- 返回类型
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.]]]]