深度视觉中有关图像projection的代码改写cv2.remap() → F.grid_sample() | Numpy+cv2格式改为PyTorch格式
创始人
2024-04-20 02:58:50
0

Cv2 remap in pytorch?

Numpy+cv2实现的代码迁移到PyTorch上往往不怎么需要改动,直接把np换成torch即可,但cv2.remap()函数是个特殊例子,该函数通过xy两个数组重新采样图像,可以用来实现投影变换(warp,projection),在torch中与之对应的是torch.nn.functional.grid_sample() 函数,但用法上有着一些不同。

以下以我的一个实际代码片段例子来直观介绍torch版本的代码重写。

我的任务是将ref视点的ref_img and ref_depth 投影到另一个src视点。

Numpycv2 风格的代码:

def reproject_with_depth(img_ref, depth_ref, intrinsics_ref, extrinsics_ref, intrinsics_src, extrinsics_src):width, height = depth_ref.shape[1], depth_ref.shape[0]x_ref, y_ref = np.meshgrid(np.arange(0, width), np.arange(0, height))x_ref, y_ref = x_ref.reshape([-1]), y_ref.reshape([-1])xyz_ref = np.matmul(np.linalg.inv(intrinsics_src),np.vstack((x_ref, y_ref, np.ones_like(x_ref))) * depth_ref.reshape([-1]))xyz_src = np.matmul(np.matmul(extrinsics_ref, np.linalg.inv(extrinsics_src)),np.vstack((xyz_ref, np.ones_like(x_ref))))[:3]K_xyz_src = np.matmul(intrinsics_ref, xyz_src)xy_src = K_xyz_src[:2] / K_xyz_src[2:3]x_src = xy_src[0].reshape([height, width]).astype(np.float32)y_src = xy_src[1].reshape([height, width]).astype(np.float32)sampled_depth_src = cv2.remap(depth_ref, x_src, y_src, interpolation=cv2.INTER_LINEAR)sampled_img_src = cv2.remap(img_ref, x_src, y_src, interpolation=cv2.INTER_LINEAR)return sampled_depth_src, sampled_img_src

翻译成 torch 风格后的代码:

def reproject_with_depth(img_ref, depth_ref, intrinsics_ref, extrinsics_ref, intrinsics_src, extrinsics_src):B, width, height = depth_ref.shape[0], depth_ref.shape[2], depth_ref.shape[1]y_ref, x_ref = torch.meshgrid([torch.arange(0, height, dtype=torch.float32, device=depth_ref.device), torch.arange(0, width, dtype=torch.float32, device=depth_ref.device)])y_ref, x_ref = y_ref.contiguous(), x_ref.contiguous()x_ref, y_ref = x_ref.reshape([-1]), y_ref.reshape([-1])# reference 3D spacexyz_ref = torch.matmul(torch.inverse(intrinsics_src),torch.stack((x_ref, y_ref, torch.ones_like(x_ref))).unsqueeze(0).repeat(B, 1, 1) * depth_ref.reshape([B, 1, -1]))xyz_src = torch.matmul(torch.matmul(extrinsics_ref, torch.inverse(extrinsics_src)),torch.cat([xyz_ref, torch.ones_like(x_ref).unsqueeze(0).repeat(B,1,1)], dim=1))[:,:3]K_xyz_src = torch.matmul(intrinsics_ref, xyz_src)xy_src = K_xyz_src[:, :2] / K_xyz_src[:, 2:3]x_src = xy_src[:, 0].reshape([B, height, width]).float()y_src = xy_src[:, 1].reshape([B, height, width]).float()grid = torch.stack((x_src/((width-1)/2)-1, y_src/((height-1)/2)-1), dim=3)sampled_depth_src = F.grid_sample(depth_ref.unsqueeze(1), grid.view(B, height, width, 2), mode='bilinear', padding_mode='zeros').squeeze(1)sampled_img_src = F.grid_sample(img_ref, grid.view(B, height, width, 2), mode='bilinear', padding_mode='zeros')return sampled_depth_src, sampled_img_src

一些核心翻译原则需要遵守的(可能会给你带来困惑的)是:

  • torch版的代码要考虑 batch B这个维度,因此诸如切片等操作记得先把0-dim考虑进去,例如 [:, idx]
  • torch版代码需要考虑device ,一般可以另新创建的tensor的dtype为输入参数的dtype
  • F.grid_sample()的坐标采样范围是[-1, 1],而cv2.remap() 直接使用的是像素坐标尺度,因此需要在x轴/((width-1)/2)-1,在y轴/((height-1)/2)-1) 来缩放坐标系

相关内容

热门资讯

55英寸安卓系统,畅享科技与娱... 你有没有想过,家里的电视该升级换代了?别急,今天就来给你好好聊聊55英寸安卓系统的电视,看看它到底有...
全民k系统安卓版下载,一键下载... 你有没有听说最近超级火的全民K系统安卓版下载?没错,就是那个让无数手机用户为之疯狂的系统!今天,我就...
手机中的安卓系统,手机智能生活... 你有没有发现,现在手机里的安卓系统就像是个万能的小助手,无处不在,无所不能呢?它就像是我们生活中的得...
gm8系统是安卓系统吗,安卓生... 你有没有听说过GM8系统?最近这个话题在数码圈里可是挺火的。很多人都在问,GM8系统是安卓系统吗?今...
安卓系统文件非常大,揭秘庞大文... 你有没有发现,最近你的安卓手机越来越卡了?别急,别急,让我来给你揭秘一下这个谜团。没错,就是那个让我...
安卓11系统怎么看,深度解析与... 你有没有发现,你的安卓手机最近是不是变得有点不一样了?没错,安卓11系统已经悄悄地来到了我们的身边。...
安卓p系统是什么,新一代智能体... 你有没有注意到,你的安卓手机最近是不是变得聪明多了?没错,这就是安卓P系统的魔力!今天,就让我带你一...
摩托罗拉安卓13系统,探索创新... 你有没有听说?摩托罗拉的新款手机终于升级到安卓13系统啦!这可是个大新闻,咱们得好好聊聊这个话题。想...
犯罪大师安卓系统在哪下,轻松追... 你有没有听说最近的一款超级火爆的安卓游戏——《犯罪大师》?这款游戏不仅剧情跌宕起伏,角色设定独特,而...
安卓系统升级txt,功能革新与... 你有没有发现,你的安卓手机最近是不是总在提醒你系统要升级了呢?别急,别急,今天就来给你详细说说这个安...
安卓调用系统打开图片,安卓系统... 你有没有遇到过这种情况:手机里存了那么多美美的照片,想分享给朋友,却发现打开图片的步骤竟然那么繁琐?...
安卓不再提示系统更新,轻松告别... 你有没有发现,最近你的安卓手机好像变得特别安静了呢?没错,就是那个一直默默提醒你系统更新的小家伙,它...
安卓系统的如何测试软件,从入门... 你有没有想过,你的安卓手机里那些神奇的软件是怎么诞生的呢?它们可不是凭空出现的,而是经过一系列严格的...
小米8安卓系统版本,安卓系统版... 你有没有发现,手机更新换代的速度简直就像坐上了火箭呢?这不,小米8这款手机自从上市以来,就凭借着出色...
华为手机安卓系统7以上,创新体... 你有没有发现,最近华为手机越来越受欢迎了呢?尤其是那些搭载了安卓系统7.0及以上版本的机型,简直让人...
儿童英语免费安卓系统,儿童英语... 哇,亲爱的家长朋友们,你是否在为孩子的英语学习发愁呢?别担心,今天我要给你带来一个超级好消息——儿童...
ios系统切换安卓系统还原,还... 你有没有想过,有一天你的手机从iOS系统切换到了安卓系统,然后再从安卓系统回到iOS系统呢?这听起来...
灵焕3装安卓系统,引领智能新体... 你知道吗?最近手机圈里可是掀起了一股热潮,那就是灵焕3这款神器的安卓系统升级。没错,就是那个曾经以独...
安卓系统指南针软件,探索未知世... 手机里的指南针功能是不是让你在户外探险时倍感神奇?但你知道吗,安卓系统中的指南针软件可是大有学问呢!...
华为是不用安卓系统了吗,迈向自... 最近有个大新闻在科技圈里炸开了锅,那就是华为是不是不再使用安卓系统了?这可不是一个简单的问题,它涉及...