File size: 4,299 Bytes
e4c5b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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