深度视觉中有关图像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) 来缩放坐标系

相关内容

热门资讯

oppo安卓版系统设置,全面解... 亲爱的手机控们,你是不是也和我一样,对OPPO安卓版系统的设置充满了好奇?想要让你的OPPO手机更加...
安卓系统是什么cp,CP架构下... 你有没有想过,你的手机里那个默默无闻的安卓系统,其实就像是一个超级贴心的CP(情侣搭档)呢?没错,就...
系统垃圾清理大师 安卓,安卓手... 手机里的垃圾文件是不是让你头疼不已?别急,今天我要给你介绍一位安卓系统里的“清洁小能手”——系统垃圾...
安卓系统分为几层,安卓系统分层... 你知道吗?安卓系统,这个陪伴我们手机生活的“小助手”,其实它内部结构可是相当复杂的呢!今天,就让我带...
系统最像苹果的安卓,揭秘最像苹... 你有没有发现,现在的安卓手机越来越像苹果了?没错,就是那个以简洁设计和流畅体验著称的苹果。今天,就让...
安卓更新13系统游戏,性能升级... 你知道吗?最近安卓系统又来了一次大变身,那就是安卓13系统!这次更新可是带来了不少惊喜,尤其是对那些...
安卓系统开机出错了,安卓系统开... 手机突然开不了机了,这可怎么办?别急,让我来帮你分析一下安卓系统开机出错的那些事儿。一、安卓系统开机...
vovg是安卓系统吗,安卓系统... 你有没有听说过Vovg这个操作系统?最近,这个名词在数码圈里可是引起了不小的热议呢!很多人都在问,V...
谷歌终止安卓系统更新,影响与未... 你知道吗?最近科技圈可是炸开了锅,因为谷歌突然宣布了一项重大决定——终止对某些安卓系统的更新!这可不...
塞班系统比安卓好,超越安卓的卓... 你知道吗?在手机操作系统的大战中,塞班系统和安卓系统一直是你争我斗的态势。但你知道吗?塞班系统在某些...
安卓系统手机便宜测评,深度测评... 你有没有想过,为什么安卓系统手机总是那么便宜呢?是不是觉得它们质量不好?别急,今天我就要带你深入了解...
安卓怎么扫描门禁系统,安卓设备... 你有没有想过,家里的门禁系统竟然也能用手机轻松搞定?没错,就是那个你每天进出都离不开的安卓手机!今天...
安卓系统账号注册过程,安卓系统... 你终于决定加入安卓系统的大家庭啦! 想必你对这个系统充满了期待,不过别急,注册账号可是第一步哦!今天...
日产天籁的安卓系统,智能驾驶体... 你有没有注意到,最近开车的朋友们都在议论纷纷,说他们的日产天籁换了个新玩意儿——安卓系统!这可不是什...
安卓系统怎么下载闹钟,安卓系统... 你有没有发现,每天早晨闹钟一响,整个人就像被电击了一样,瞬间清醒?没错,闹钟可是我们生活中不可或缺的...
手机系统设置铃声安卓,个性化定... 手机里那首动听的铃声,是不是让你每次听到都忍不住嘴角上扬呢?今天,就让我带你一起探索安卓手机系统设置...
安卓电脑双系统平板,畅享多模态... 你有没有想过,一台平板电脑既能满足你办公的需求,又能让你畅享娱乐时光?现在,有一种神奇的设备——安卓...
创维电视安卓系统2.3,回顾经... 你有没有发现家里的创维电视有点儿“老态龙钟”了?别急,别急,今天就来给你揭秘一下这款电视的“内心世界...
如何破解车载安卓系统,轻松解锁... 如何破解车载安卓系统:揭秘背后的技术与风险在当今这个数字化飞速发展的时代,汽车已经不仅仅是一种交通工...
安卓机上的windows系统,... 你有没有想过,把Windows系统的强大功能搬到安卓机上?想象那可是个让人眼前一亮的操作体验呢!今天...