批量像素操作
Pixel operations in batches
我有一批深度图,shape -> [B, 1, H, W]。对于我需要执行的批处理的每个图像中的每个像素:
X = d * Kinverse @ [u, v, 1] #therefore X is in R^3
其中 d 是 float tensor[0;1] 表示像素 u,v 处的深度; Kinverse 是一个常量 3X3 矩阵,u,v 分别指代像素列和行。
有没有什么方法可以向量化操作以获得批次中所有图像的 X(u+1,v)、X(u,v) 和 X(u,v+1)。
我最终需要采用这个叉积:
{X(u+1,v) - X(u,v)} x {X(u, v+1) - X(u,v)}
感谢您的帮助!
您可以使用 torch.meshgrid
to produce the u
and v
tensors. Once you have them, you can use 与 Kinverse
进行批量矩阵乘法。
最后,您可以使用 torch.cross
来计算叉积:
u, v = torch.meshgrid(*[torch.arange(s_, dtype=d.dtype, device=d.device) for s_ in d.shape[2:]])
# make a single 1x1xHxW for [u v 1] per pixel:
uv = torch.cat((u[None, None, ...], v[None, None, ...], torch.ones_like(u)[None, None, ...]), dim=1)
# compute X
X = d * torch.einsum('ij,bjhw->bihw',Kinverse,uv)
# the cross product
out = torch.cross(X[..., 1:, :-1] - X[..., :-1, :-1], X[..., :-1, 1:] - X[..., :-1, :-1], dim=1)
我有一批深度图,shape -> [B, 1, H, W]。对于我需要执行的批处理的每个图像中的每个像素:
X = d * Kinverse @ [u, v, 1] #therefore X is in R^3
其中 d 是 float tensor[0;1] 表示像素 u,v 处的深度; Kinverse 是一个常量 3X3 矩阵,u,v 分别指代像素列和行。
有没有什么方法可以向量化操作以获得批次中所有图像的 X(u+1,v)、X(u,v) 和 X(u,v+1)。 我最终需要采用这个叉积: {X(u+1,v) - X(u,v)} x {X(u, v+1) - X(u,v)}
感谢您的帮助!
您可以使用 torch.meshgrid
to produce the u
and v
tensors. Once you have them, you can use Kinverse
进行批量矩阵乘法。
最后,您可以使用 torch.cross
来计算叉积:
u, v = torch.meshgrid(*[torch.arange(s_, dtype=d.dtype, device=d.device) for s_ in d.shape[2:]])
# make a single 1x1xHxW for [u v 1] per pixel:
uv = torch.cat((u[None, None, ...], v[None, None, ...], torch.ones_like(u)[None, None, ...]), dim=1)
# compute X
X = d * torch.einsum('ij,bjhw->bihw',Kinverse,uv)
# the cross product
out = torch.cross(X[..., 1:, :-1] - X[..., :-1, :-1], X[..., :-1, 1:] - X[..., :-1, :-1], dim=1)