meshgrid 生成二、三维网格点

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

例一: 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.</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

培之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值