| 117 | |
| 118 | |
| 119 | class SceneViz: |
| 120 | def __init__(self): |
| 121 | self.scene = trimesh.Scene() |
| 122 | |
| 123 | def add_rgbd(self, image, depth, intrinsics=None, cam2world=None, zfar=np.inf, mask=None): |
| 124 | image = img_to_arr(image) |
| 125 | |
| 126 | # make up some intrinsics |
| 127 | if intrinsics is None: |
| 128 | H, W, THREE = image.shape |
| 129 | focal = max(H, W) |
| 130 | intrinsics = np.float32([[focal, 0, W/2], [0, focal, H/2], [0, 0, 1]]) |
| 131 | |
| 132 | # compute 3d points |
| 133 | pts3d = depthmap_to_pts3d(depth, intrinsics, cam2world=cam2world) |
| 134 | |
| 135 | return self.add_pointcloud(pts3d, image, mask=(depth<zfar) if mask is None else mask) |
| 136 | |
| 137 | def add_pointcloud(self, pts3d, color=(0,0,0), mask=None, denoise=False): |
| 138 | pts3d = to_numpy(pts3d) |
| 139 | mask = to_numpy(mask) |
| 140 | if not isinstance(pts3d, list): |
| 141 | pts3d = [pts3d.reshape(-1,3)] |
| 142 | if mask is not None: |
| 143 | mask = [mask.ravel()] |
| 144 | if not isinstance(color, (tuple,list)): |
| 145 | color = [color.reshape(-1,3)] |
| 146 | if mask is None: |
| 147 | mask = [slice(None)] * len(pts3d) |
| 148 | |
| 149 | pts = np.concatenate([p[m] for p,m in zip(pts3d,mask)]) |
| 150 | pct = trimesh.PointCloud(pts) |
| 151 | |
| 152 | if isinstance(color, (list, np.ndarray, torch.Tensor)): |
| 153 | color = to_numpy(color) |
| 154 | col = np.concatenate([p[m] for p,m in zip(color,mask)]) |
| 155 | assert col.shape == pts.shape, bb() |
| 156 | pct.visual.vertex_colors = uint8(col.reshape(-1,3)) |
| 157 | else: |
| 158 | assert len(color) == 3 |
| 159 | pct.visual.vertex_colors = np.broadcast_to(uint8(color), pts.shape) |
| 160 | |
| 161 | if denoise: |
| 162 | # remove points which are noisy |
| 163 | centroid = np.median(pct.vertices, axis=0) |
| 164 | dist_to_centroid = np.linalg.norm( pct.vertices - centroid, axis=-1) |
| 165 | dist_thr = np.quantile(dist_to_centroid, 0.99) |
| 166 | valid = (dist_to_centroid < dist_thr) |
| 167 | # new cleaned pointcloud |
| 168 | pct = trimesh.PointCloud(pct.vertices[valid], color=pct.visual.vertex_colors[valid]) |
| 169 | |
| 170 | self.scene.add_geometry(pct) |
| 171 | return self |
| 172 | |
| 173 | def add_rgbd(self, image, depth, intrinsics=None, cam2world=None, zfar=np.inf, mask=None): |
| 174 | # make up some intrinsics |
| 175 | if intrinsics is None: |
| 176 | H, W, THREE = image.shape |
no outgoing calls
no test coverage detected