def inv2depth(inv_depth): """ Invert an inverse depth map to produce a depth map Parameters ---------- inv_depth : torch.Tensor or list of torch.Tensor [B,1,H,W] Inverse depth map Returns ------- depth : torch.Tensor or list of torch.Tensor [B,1,H,W] Depth map """ if is_seq(inv_depth): return [inv2depth(item) for item in inv_depth] else: return 1. / inv_depth.clamp(min=1e-6)
def inv2depth(self, inv_depth): """ Invert an inverse depth map to produce a depth map Parameters ---------- inv_depth : torch.Tensor or list of torch.Tensor [B,1,H,W] Inverse depth map Returns ------- depth : torch.Tensor or list of torch.Tensor [B,1,H,W] Depth map """ if is_seq(inv_depth): return [self.inv2depth(item) for item in inv_depth] else: return 1. * self.stereo_scale_factor / inv_depth
def resize_depth_preserve(depth, shape): """ Resizes depth map preserving all valid depth pixels Multiple downsampled points can be assigned to the same pixel. Parameters ---------- depth : np.array [h,w] Depth map shape : tuple (H,W) Output shape Returns ------- depth : np.array [H,W,1] Resized depth map """ # Return if depth value is None if depth is None: return depth # If a single number is provided, use resize ratio if not is_seq(shape): shape = tuple(int(s * shape) for s in depth.shape) # Store dimensions and reshapes to single column depth = np.squeeze(depth) h, w = depth.shape x = depth.reshape(-1) # Create coordinate grid uv = np.mgrid[:h, :w].transpose(1, 2, 0).reshape(-1, 2) # Filters valid points idx = x > 0 crd, val = uv[idx], x[idx] # Downsamples coordinates crd[:, 0] = (crd[:, 0] * (shape[0] / h)).astype(np.int32) crd[:, 1] = (crd[:, 1] * (shape[1] / w)).astype(np.int32) # Filters points inside image idx = (crd[:, 0] < shape[0]) & (crd[:, 1] < shape[1]) crd, val = crd[idx], val[idx] # Creates downsampled depth image and assigns points depth = np.zeros(shape) depth[crd[:, 0], crd[:, 1]] = val # Return resized depth map return np.expand_dims(depth, axis=2)
def depth2inv(depth): """ Invert a depth map to produce an inverse depth map Parameters ---------- depth : torch.Tensor or list of torch.Tensor [B,1,H,W] Depth map Returns ------- inv_depth : torch.Tensor or list of torch.Tensor [B,1,H,W] Inverse depth map """ if is_seq(depth): return [depth2inv(item) for item in depth] else: inv_depth = 1. / depth.clamp(min=1e-6) inv_depth[depth <= 0.] = 0. return inv_depth