例一: 2维
根据下面的代码就能看出来。
实际上是对[0,3]*[0,2] 矩形建 grid
torch.meshgrid 的输出的形式实际上就是一种遍历所有 点的方法。
import torch
H= 3
W = 4
i, j = torch.meshgrid(torch.linspace(0, W-1, W), torch.linspace(0, H-1, H)) # pytorch's meshgrid has indexing='ij'
points = torch.stack([i, j], dim=-1).reshape(-1, 2)
print(i,"\n",j)
print(points)
输出:
tensor([[0., 0., 0.],
[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]])
tensor([[0., 1., 2.],
[0., 1., 2.],
[0., 1., 2.],
[0., 1., 2.]])
tensor([[0., 0.],
[0., 1.],
[0., 2.],
[1., 0.],
[1., 1.],
[1., 2.],
[2., 0.],
[2., 1.],
[2., 2.],
[3., 0.],
[3., 1.],
[3., 2.]])
例二:3维
import torch
X, Y, Z = torch.meshgrid(torch.linspace(1, 3, 3),
torch.linspace(4, 7, 4),
torch.linspace(8, 12, 5))
points = torch.stack([X, Y, Z], dim=-1).reshape(-1, 3)
print("X's shape: \n",X.shape)
print("X: ",X)
print("Y's shape: \n",Y.shape)
print("Y: ",Y)
print("Z's shape: \n",Z.shape)
print("Z: ",Z)
print("points 's shape: \n",points.shape)
print("points : ",points )
print("newPoints 's shape: \n",newPoints.shape)
print("newPoints : ",newPoints )
输出如下:
X's shape:
torch.Size([3, 4, 5])
X: tensor([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]])
Y's shape:
torch.Size([3, 4, 5])
Y: tensor([[[4., 4., 4., 4.</

本文通过实例讲解了如何使用PyTorch的torch.meshgrid创建2维和3维网格,演示了如何将像素坐标转换为齐次坐标,并结合外参数进行操作,以及在MVSNet中的具体应用场景,帮助读者理解网格生成和坐标变换过程。
最低0.47元/天 解锁文章
1552

被折叠的 条评论
为什么被折叠?



