2019年7月16日15:55:11
感觉虚拟视点也是视觉slam里头一个重要的需求和应该实现的功能,但是好像
没看到什么资料。
百度的全景地图,或者有些公司网站上的3d装修效果图,可以用鼠标拖动查看不同视角,但是
图片看起来很奇怪,那不是虚拟视点,只是对图片做了变换。
虚拟视点的一些资料:
https://www.cnblogs.com/riddick/p/8511960.html
https://www.zhihu.com/question/40793359/answer/130155294
其他有点关联的方向:
https://zhuanlan.zhihu.com/p/73599241
谷歌也有篇文章把短基线变成长基线的文章,也有点虚拟视点的意思。
这里利用sfmlearner的设施做了个简单的demo:
原始图:
相机沿着Z轴往前移动0.5m,注意红框处和上图的对比,的确是往前移动了,这里没做插值,所以
不是太好看 pose = np.array([0, 0, -0.5, 0, 0, 0]) # tx, ty, tz, rx, ry, rz -- [B, 6] 弧度制!!!
下面是pose = np.array([0, -0.5, 0, 0, 0, 0],相机往下走了,y=-0.5
注意depth图红框处,原本从上往下拍时被桌面或者凳子挡住的部分现在看的到了,但是对应的深度图
在之前的角度是测不到的,相机往下移动之后这部分的深度图就显示为空缺了,对应的color1部分也
是黑色的。
下面是代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 11:57:48 2019 @author: scj project test https://pytorch.org/docs/stable/nn.html?highlight=f%20grid_sample#torch.nn.functional.grid_sample https://blog.csdn.net/chamber_of_secrets/article/details/83512540 https://blog.csdn.net/houdong1992/article/details/88122682 """ import torch import numpy as np
import cv2
import matplotlib.pyplot as plt #depth = cv2.imread('/media/scj/work/depth/joinMap/depth/1.pgm', -1)
depth = cv2.imread('D:\\depth\\joinMap\\depth\\1.pgm', -1)
depth = depth/1000 depth_copy = depth.copy() #fig = plt.subplots(1)
#plt.imshow( depth_copy )
#plt.title("depth") #color = cv2.imread('/media/scj/work/depth/joinMap/color/1.png')
color = cv2.imread('D:\\depth\\joinMap\\color\\1.png') #fig = plt.subplots(1)
#plt.imshow( cv2.cvtColor(color, cv2.COLOR_BGR2RGB) )
#plt.title("color") #####################
fig=plt.figure() plt.subplot(121)
plt.imshow(cv2.cvtColor(color, cv2.COLOR_BGR2RGB))
plt.title("color") plt.subplot(122)
plt.imshow(depth_copy)
plt.title("depth") plt.show() ##################### #color = np.transpose(color, (2, 0, 1)) # print(depth.shape, color.shape) # (480, 640) (3, 480, 640) depth = depth[np.newaxis, :].astype(np.float64)
depth = torch.from_numpy(depth) #print(depth.size() ) # torch.Size([1, 480, 640]) cx = 325.5
cy = 253.5
fx = 518.0
fy = 519.0 intrinsics = np.array([
[fx, 0, cx],
[0, fy, cy],
[0, 0, 1],
]).astype(np.float64) intrinsics = intrinsics[np.newaxis, :] intrinsics = torch.from_numpy(intrinsics)
#print( intrinsics.size() ) # (1, 3, 3) ##########
from inverse_warp import pixel2cam # uv2xyz cam_coords = pixel2cam(depth, intrinsics.inverse() ) #print(cam_coords.size() ) xyz1 = cam_coords.detach().numpy().squeeze()
#print(xyz1.shape)
#
#fig = plt.subplots(1)
#plt.imshow( xyz1[0, :, :] )
#plt.title("x")
#
#fig = plt.subplots(1)
#plt.imshow( xyz1[1, :, :] )
#plt.title("y")
#
#fig = plt.subplots(1)
#plt.imshow( xyz1[2, :, :] )
#plt.title("z") # tx, ty, tz, rx, ry, rz -- [B, 6] 弧度制!!!
pose = np.array([0, -0.5, 0, 0, 0, 0]).astype(np.float64)
pose = pose[np.newaxis, :]
pose = torch.from_numpy(pose) from inverse_warp import pose_vec2mat
pose_mat = pose_vec2mat(pose, rotation_mode='euler') # [B,3,4] print(pose_mat) proj_cam_to_src_pixel = intrinsics @ pose_mat # [B, 3, 4] K*T_21
#print(proj_cam_to_src_pixel) from inverse_warp import cam2pixel
# cam2pixel 多传了一个Z出来
src_pixel_coords, Z2 = cam2pixel(cam_coords, # XYZ
proj_cam_to_src_pixel[:,:,:3], # R
proj_cam_to_src_pixel[:,:,-1:], # t
padding_mode='zeros') # [B,H,W,2] print(src_pixel_coords.size() ) uv2 = src_pixel_coords.detach().numpy().squeeze() #fig = plt.subplots(1)
#plt.imshow( uv2[:, :, 0] )
#plt.title("u2")
#
#fig = plt.subplots(1)
#plt.imshow( uv2[:, :, 1] )
#plt.title("v2") #################
b = color[:, :, 0]
g = color[:, :, 1]
r = color[:, :, 2] b = b.reshape(307200, 1)
g = g.reshape(307200, 1)
r = r.reshape(307200, 1) u2 = uv2[:, :, 0].reshape(307200, 1)
v2 = uv2[:, :, 1].reshape(307200, 1) color1 = np.zeros_like(color) zz = Z2.detach().numpy().squeeze() # (307200, ) #zz[133, 182] - depth_copy[133, 182] # 深度的确有变化 相差0.5 depth1 = np.zeros((480, 640)) for i in range(307200):
uu = u2[i]
vv = v2[i] if uu>-1 and uu < 1 and vv>-1 and vv<1:
xx = int(0.5*(uu+1)*639)
yy = int(0.5*(vv+1)*479) color1[yy, xx, 0] = b[i]
color1[yy, xx, 1] = g[i]
color1[yy, xx, 2] = r[i] depth1[yy, xx] = zz[i] #fig = plt.subplots(1)
#plt.imshow( cv2.cvtColor(color1, cv2.COLOR_BGR2RGB) )
#plt.title("color1")
#
#
#fig = plt.subplots(1)
#plt.imshow( depth1 )
#plt.title("depth1") fig=plt.figure() plt.subplot(121)
plt.imshow(cv2.cvtColor(color1, cv2.COLOR_BGR2RGB))
plt.title("color1") plt.subplot(122)
plt.imshow(depth1)
plt.title("depth1") plt.show()
当然,上图的效果不行,还要做插值才能好看点。