test / tracking /utils /my_utils.py
SCreatorX
init
e4c5b8d
Raw
History Blame Contribute Delete
4.3 kB
import numpy as np
import open3d as o3d
# from ROS camera convention to USD camera convention
U_R_TRANSFORM = np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
# from USD camera convention to ROS camera convention
R_U_TRANSFORM = np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
# from USD camera convention to World camera convention
W_U_TRANSFORM = np.array([[0, 0, -1, 0], [-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
# from World camera convention to USD camera convention
U_W_TRANSFORM = np.array([[0, -1, 0, 0], [0, 0, 1, 0], [-1, 0, 0, 0], [0, 0, 0, 1]])
def depth2fgpcd(depth, mask, cam_params):
# depth: (h, w)
# fgpcd: (n, 3)
# mask: (h, w)
h, w = depth.shape
mask = np.logical_and(mask, depth > 0)
fgpcd = np.zeros((mask.sum(), 3))
fx, fy, cx, cy = cam_params
pos_x, pos_y = np.meshgrid(np.arange(w), np.arange(h))
pos_x = pos_x[mask]
pos_y = pos_y[mask]
fgpcd[:, 0] = (pos_x - cx) * depth[mask] / fx
fgpcd[:, 1] = (pos_y - cy) * depth[mask] / fy
fgpcd[:, 2] = depth[mask]
return fgpcd
def depth2pcd(depth,camera_proj_mat,camera_view_mat):
height, width=depth.shape
vinv = np.linalg.inv(camera_view_mat)
proj=camera_proj_mat
fu =2 /proj[0,0]
fv =2 /proj[1,1]
centerU=width/2
centerV = height/2
u = np.linspace(0, width - 1, width)
v = np.linspace(0, height - 1, height)
u,v = np.meshgrid(u,v,indexing="xy")
Z=depth
x_para=-Z*fu/width
y_para=Z*fv/height
X=(u - centerU)*x_para
Y=(v - centerV)*y_para
position = np.stack([X,Y,Z,np.ones_like(X)],axis=-1)
position = position.view(-1,4)
position = position @ vinv
points=position[:,:3]
return points
def depth2fgpcd_w(depth,mask,K):
# depth: (h, w)
# fgpcd: (n, 3)
# mask: (h, w)
# get_pointcloud
im_height, im_width = depth.shape[0], depth.shape[1]
valid_mask=np.logical_and(mask,depth>0)
if not valid_mask.any():
return np.zeros((0, 3))
ww = np.linspace(0.5, im_width - 0.5, im_width, dtype=np.float32)
hh = np.linspace(0.5, im_height - 0.5, im_height, dtype=np.float32)
xmap, ymap = np.meshgrid(ww, hh, indexing="xy")
# points_2d = np.column_stack((xmap.ravel(), ymap.ravel()))
points_2d = np.column_stack((xmap[mask], ymap[mask])) # (n, 2)
# get_world_points_from_image_coords
# depth =depth.flatten()
depth =depth[mask] # (n,)
homogenous=np.pad(points_2d,((0,0),(0,1)),mode="constant",constant_values=1.0)
points_in_camera_axes = np.matmul(
np.linalg.inv(K),
np.transpose(homogenous)*np.expand_dims(depth,0),
)
points_in_camera_frame=np.transpose(points_in_camera_axes)
return points_in_camera_frame
def np2o3d(pcd, color=None, seg=None):
# pcd: (n, 3)
# color: (n, 3)
pcd_dicts = {}
pcd_o3d = o3d.geometry.PointCloud()
pcd_o3d.points = o3d.utility.Vector3dVector(pcd)
if color is not None:
assert pcd.shape[0] == color.shape[0]
assert color.max() <= 1
assert color.min() >= 0
pcd_o3d.colors = o3d.utility.Vector3dVector(color)
for i, pos in enumerate(pcd_o3d.points):
pcd_dicts[tuple(pos)] = {
'color': pcd_o3d.colors[i],
'seg': seg[i]
}
return pcd_o3d, pcd_dicts
def depth2normal(d_im, K):
# :param d_im: (H, W) depth image in meters
# :param K: (3, 3) camera intrinsics
# :return (H, W, 3) normal image
H, W = d_im.shape
cx, cy, fx, fy = K[0, 2], K[1, 2], K[0, 0], K[1, 1]
pcd = np.zeros((H * W, 3))
xy_grid = np.mgrid[0:W, 0:H].T.reshape(-1, 2)
pcd[:, 0] = (xy_grid[:, 0] - cx) * d_im.reshape(-1) / fx
pcd[:, 1] = (xy_grid[:, 1] - cy) * d_im.reshape(-1) / fy
pcd[:, 2] = d_im.reshape(-1)
pcd = pcd.reshape(H, W, 3)
window = 10
pcd = np.pad(pcd, ((0, window), (0, window), (0, 0)), mode='edge') # shape (H+1, W+1, 3)
pcd_h_diff = pcd[window:, :W, :] - pcd[:-window, :W, :]
pcd_v_diff = pcd[:H, window:, :] - pcd[:H, :-window, :]
pcd_normals = np.cross(pcd_h_diff, pcd_v_diff) # shape (H, W, 3)
pcd_normals = pcd_normals / (np.linalg.norm(pcd_normals, axis=2, keepdims=True) + 1e-6) # shape (H, W, 3)
return pcd_normals