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')[源代码]¶
对按批组织的二维图像进行透视变换。
输入图像通过变换矩阵变换为输出图像:
\[\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 来对同一图像不同batch指定不同的变换矩阵, 不然输入图像和变换矩阵之间就会是一一对应的关系
- 参数
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, )` 每个矩阵对应的图像批次。默认是:None。border_mode (
str
) – 边界处理方式。默认是:“replicate”。现在也支持“constant”,“reflect”,“reflect_101”,“wrap”。border_val (
float
) – 边界填充值。 默认:0format (
str
) – NHWC“ 也支持。默认:”NCHW“。interp_mode (
str
) – 插值方式。默认:”linear“。现在只支持 “linear” 的模式。
- 返回类型
- 返回
输出张量。
注解
转换矩阵是 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.]]]]