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

相关内容

热门资讯

电视安卓系统哪个品牌好,哪家品... 你有没有想过,家里的电视是不是该升级换代了呢?现在市面上电视品牌琳琅满目,各种操作系统也是让人眼花缭...
安卓会员管理系统怎么用,提升服... 你有没有想过,手机里那些你爱不释手的APP,背后其实有个强大的会员管理系统在默默支持呢?没错,就是那...
安卓系统软件使用技巧,解锁软件... 你有没有发现,用安卓手机的时候,总有一些小技巧能让你玩得更溜?别小看了这些小细节,它们可是能让你的手...
安卓系统提示音替换 你知道吗?手机里那个时不时响起的提示音,有时候真的能让人心情大好,有时候又让人抓狂不已。今天,就让我...
安卓开机不了系统更新 手机突然开不了机,系统更新还卡在那里,这可真是让人头疼的问题啊!你是不是也遇到了这种情况?别急,今天...
安卓系统中微信视频,安卓系统下... 你有没有发现,现在用手机聊天,视频通话简直成了标配!尤其是咱们安卓系统的小伙伴们,微信视频功能更是用...
安卓系统是服务器,服务器端的智... 你知道吗?在科技的世界里,安卓系统可是个超级明星呢!它不仅仅是个手机操作系统,竟然还能成为服务器的得...
pc电脑安卓系统下载软件,轻松... 你有没有想过,你的PC电脑上安装了安卓系统,是不是瞬间觉得世界都大不一样了呢?没错,就是那种“一机在...
电影院购票系统安卓,便捷观影新... 你有没有想过,在繁忙的生活中,一部好电影就像是一剂强心针,能瞬间让你放松心情?而我今天要和你分享的,...
安卓系统可以写程序? 你有没有想过,安卓系统竟然也能写程序呢?没错,你没听错!这个我们日常使用的智能手机操作系统,竟然有着...
安卓系统架构书籍推荐,权威书籍... 你有没有想过,想要深入了解安卓系统架构,却不知道从何下手?别急,今天我就要给你推荐几本超级实用的书籍...
安卓系统看到的炸弹,技术解析与... 安卓系统看到的炸弹——揭秘手机中的隐形威胁在数字化时代,智能手机已经成为我们生活中不可或缺的一部分。...
鸿蒙系统有安卓文件,畅享多平台... 你知道吗?最近在科技圈里,有个大新闻可是闹得沸沸扬扬的,那就是鸿蒙系统竟然有了安卓文件!是不是觉得有...
宝马安卓车机系统切换,驾驭未来... 你有没有发现,现在的汽车越来越智能了?尤其是那些豪华品牌,比如宝马,它们的内饰里那个大屏幕,简直就像...
p30退回安卓系统 你有没有听说最近P30的用户们都在忙活一件大事?没错,就是他们的手机要退回安卓系统啦!这可不是一个简...
oppoa57安卓原生系统,原... 你有没有发现,最近OPPO A57这款手机在安卓原生系统上的表现真是让人眼前一亮呢?今天,就让我带你...
安卓系统输入法联想,安卓系统输... 你有没有发现,手机上的输入法真的是个神奇的小助手呢?尤其是安卓系统的输入法,简直就是智能生活的点睛之...
怎么进入安卓刷机系统,安卓刷机... 亲爱的手机控们,你是否曾对安卓手机的刷机系统充满好奇?想要解锁手机潜能,体验全新的系统魅力?别急,今...
安卓系统程序有病毒 你知道吗?在这个数字化时代,手机已经成了我们生活中不可或缺的好伙伴。但是,你知道吗?即使是安卓系统,...
奥迪中控安卓系统下载,畅享智能... 你有没有发现,现在汽车的中控系统越来越智能了?尤其是奥迪这种豪华品牌,他们的中控系统简直就是科技与艺...