meshgrid 生成二、三维网格点

例一: 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., 4.],
         [5., 5., 5., 5., 5.],
         [6., 6., 6., 6., 6.],
         [7., 7., 7., 7., 7.]],

        [[4., 4., 4., 4., 4.],
         [5., 5., 5., 5., 5.],
         [6., 6., 6., 6., 6.],
         [7., 7., 7., 7., 7.]],

        [[4., 4., 4., 4., 4.],
         [5., 5., 5., 5., 5.],
         [6., 6., 6., 6., 6.],
         [7., 7., 7., 7., 7.]]])
Z's shape: 
 torch.Size([3, 4, 5])
Z:  tensor([[[ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.]],

        [[ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.]],

        [[ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.],
         [ 8.,  9., 10., 11., 12.]]])


points 's shape: 
 torch.Size([3, 4, 5, 3])
points :  tensor([[[[ 1.,  4.,  8.],
          [ 1.,  4.,  9.],
          [ 1.,  4., 10.],
          [ 1.,  4., 11.],
          [ 1.,  4., 12.]],

         [[ 1.,  5.,  8.],
          [ 1.,  5.,  9.],
          [ 1.,  5., 10.],
          [ 1.,  5., 11.],
          [ 1.,  5., 12.]],

         [[ 1.,  6.,  8.],
          [ 1.,  6.,  9.],
          [ 1.,  6., 10.],
          [ 1.,  6., 11.],
          [ 1.,  6., 12.]],

         [[ 1.,  7.,  8.],
          [ 1.,  7.,  9.],
          [ 1.,  7., 10.],
          [ 1.,  7., 11.],
          [ 1.,  7., 12.]]],


        [[[ 2.,  4.,  8.],
          [ 2.,  4.,  9.],
          [ 2.,  4., 10.],
          [ 2.,  4., 11.],
          [ 2.,  4., 12.]],

         [[ 2.,  5.,  8.],
          [ 2.,  5.,  9.],
          [ 2.,  5., 10.],
          [ 2.,  5., 11.],
          [ 2.,  5., 12.]],

         [[ 2.,  6.,  8.],
          [ 2.,  6.,  9.],
          [ 2.,  6., 10.],
          [ 2.,  6., 11.],
          [ 2.,  6., 12.]],

         [[ 2.,  7.,  8.],
          [ 2.,  7.,  9.],
          [ 2.,  7., 10.],
          [ 2.,  7., 11.],
          [ 2.,  7., 12.]]],


        [[[ 3.,  4.,  8.],
          [ 3.,  4.,  9.],
          [ 3.,  4., 10.],
          [ 3.,  4., 11.],
          [ 3.,  4., 12.]],

         [[ 3.,  5.,  8.],
          [ 3.,  5.,  9.],
          [ 3.,  5., 10.],
          [ 3.,  5., 11.],
          [ 3.,  5., 12.]],

         [[ 3.,  6.,  8.],
          [ 3.,  6.,  9.],
          [ 3.,  6., 10.],
          [ 3.,  6., 11.],
          [ 3.,  6., 12.]],

         [[ 3.,  7.,  8.],
          [ 3.,  7.,  9.],
          [ 3.,  7., 10.],
          [ 3.,  7., 11.],
          [ 3.,  7., 12.]]]])
newPoints 's shape: 
 torch.Size([60, 3])
newPoints :  tensor([[ 1.,  4.,  8.],
        [ 1.,  4.,  9.],
        [ 1.,  4., 10.],
        [ 1.,  4., 11.],
        [ 1.,  4., 12.],
        [ 1.,  5.,  8.],
        [ 1.,  5.,  9.],
        [ 1.,  5., 10.],
        [ 1.,  5., 11.],
        [ 1.,  5., 12.],
        [ 1.,  6.,  8.],
        [ 1.,  6.,  9.],
        [ 1.,  6., 10.],
        [ 1.,  6., 11.],
        [ 1.,  6., 12.],
        [ 1.,  7.,  8.],
        [ 1.,  7.,  9.],
        [ 1.,  7., 10.],
        [ 1.,  7., 11.],
        [ 1.,  7., 12.],
        [ 2.,  4.,  8.],
        [ 2.,  4.,  9.],
        [ 2.,  4., 10.],
        [ 2.,  4., 11.],
        [ 2.,  4., 12.],
        [ 2.,  5.,  8.],
        [ 2.,  5.,  9.],
        [ 2.,  5., 10.],
        [ 2.,  5., 11.],
        [ 2.,  5., 12.],
        [ 2.,  6.,  8.],
        [ 2.,  6.,  9.],
        [ 2.,  6., 10.],
        [ 2.,  6., 11.],
        [ 2.,  6., 12.],
        [ 2.,  7.,  8.],
        [ 2.,  7.,  9.],
        [ 2.,  7., 10.],
        [ 2.,  7., 11.],
        [ 2.,  7., 12.],
        [ 3.,  4.,  8.],
        [ 3.,  4.,  9.],
        [ 3.,  4., 10.],
        [ 3.,  4., 11.],
        [ 3.,  4., 12.],
        [ 3.,  5.,  8.],
        [ 3.,  5.,  9.],
        [ 3.,  5., 10.],
        [ 3.,  5., 11.],
        [ 3.,  5., 12.],
        [ 3.,  6.,  8.],
        [ 3.,  6.,  9.],
        [ 3.,  6., 10.],
        [ 3.,  6., 11.],
        [ 3.,  6., 12.],
        [ 3.,  7.,  8.],
        [ 3.,  7.,  9.],
        [ 3.,  7., 10.],
        [ 3.,  7., 11.],
        [ 3.,  7., 12.]])

Process finished with exit code 0

例3

下面代码是来自 MVSNet-PyTorch Github开源代码中 homo_wrap 的一部分
主要是想通过下面部分代码输出的 tensor 来理解一下 homo_wrap 的流程:
首先生成一张 image 的所有坐标:
假设 image 的height,width 分别是2,3则
生成
(0,0) (1,0) (2,0)
(0,1) (1,1) (2,1)
的坐标,然后将像素坐标加一个维度,变成 齐次坐标,即
(0,0,1) (1,0,1) (2,0,1)
(0,1,1) (1,1,1) (2,1,1)
然后再经过一个R,t 注意这里的 R是从 reference view 到 source view 的 R,t也是。
其实我认为这里应该不是 R,t 而是与 K作用下的 R,t,而且K包括了 reference 和 souce 两个K。
因为像素坐标不能直接跟外参 R,t相作用,要先经过 K − 1 K^{-1} K1
然后再归一化坐标,得到像素坐标?因为 上面说了 R,t里面隐含了 K其实。

import torch
height,width = 2,3
batch = 1
num_depth = 3

y, x = torch.meshgrid([torch.arange(0, height, dtype=torch.float32),
                               torch.arange(0, width, dtype=torch.float32)])
print("\n1:\n",y,"\n",x)
y, x = y.contiguous(), x.contiguous()
print("\n2:\n",y,"\n",x)
y, x = y.view(height * width), x.view(height * width)
print("\n3:\n",y,"\n",x)

xyz = torch.stack((x, y, torch.ones_like(x)))  # [3, H*W]
print("\n1:\n",xyz)
xyz = torch.unsqueeze(xyz, 0).repeat(batch, 1, 1)  # [B, 3, H*W]
print("\n2:\n",xyz)

depth_values = torch.tensor([0.5,1.5,2.5])
rot_depth_xyz = xyz.unsqueeze(2).repeat(1, 1, num_depth, 1) * depth_values.view(batch, 1, num_depth,                                                                                        1)  # [B, 3, Ndepth, H*W]
print(rot_depth_xyz)
proj_x_normalized = rot_depth_xyz[:, 0, :, :]
proj_y_normalized =  rot_depth_xyz[:, 1, :, :]
proj_xy = torch.stack((proj_x_normalized, proj_y_normalized), dim=3)

print("\nproj_xy:\n",proj_xy)

输出:

1:
 tensor([[0., 0., 0.],
        [1., 1., 1.]]) 
 tensor([[0., 1., 2.],
        [0., 1., 2.]])

2:
 tensor([[0., 0., 0.],
        [1., 1., 1.]]) 
 tensor([[0., 1., 2.],
        [0., 1., 2.]])

3:
 tensor([0., 0., 0., 1., 1., 1.]) 
 tensor([0., 1., 2., 0., 1., 2.])

1:
 tensor([[0., 1., 2., 0., 1., 2.],
        [0., 0., 0., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.]])

2:
 tensor([[[0., 1., 2., 0., 1., 2.],
         [0., 0., 0., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.]]])
tensor([[[[0.0000, 0.5000, 1.0000, 0.0000, 0.5000, 1.0000],
          [0.0000, 1.5000, 3.0000, 0.0000, 1.5000, 3.0000],
          [0.0000, 2.5000, 5.0000, 0.0000, 2.5000, 5.0000]],

         [[0.0000, 0.0000, 0.0000, 0.5000, 0.5000, 0.5000],
          [0.0000, 0.0000, 0.0000, 1.5000, 1.5000, 1.5000],
          [0.0000, 0.0000, 0.0000, 2.5000, 2.5000, 2.5000]],

         [[0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000],
          [1.5000, 1.5000, 1.5000, 1.5000, 1.5000, 1.5000],
          [2.5000, 2.5000, 2.5000, 2.5000, 2.5000, 2.5000]]]])

proj_xy:
 tensor([[[[0.0000, 0.0000],
          [0.5000, 0.0000],
          [1.0000, 0.0000],
          [0.0000, 0.5000],
          [0.5000, 0.5000],
          [1.0000, 0.5000]],

         [[0.0000, 0.0000],
          [1.5000, 0.0000],
          [3.0000, 0.0000],
          [0.0000, 1.5000],
          [1.5000, 1.5000],
          [3.0000, 1.5000]],

         [[0.0000, 0.0000],
          [2.5000, 0.0000],
          [5.0000, 0.0000],
          [0.0000, 2.5000],
          [2.5000, 2.5000],
          [5.0000, 2.5000]]]])

Process finished with exit code 0
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

培之

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

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

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

打赏作者

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

抵扣说明:

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

余额充值