Read a depth image, return float32 depth array of shape (H, W).
(path: Union[str, os.PathLike, IO])
| 87 | |
| 88 | |
| 89 | def read_depth(path: Union[str, os.PathLike, IO]) -> Tuple[np.ndarray, float]: |
| 90 | """ |
| 91 | Read a depth image, return float32 depth array of shape (H, W). |
| 92 | """ |
| 93 | if isinstance(path, (str, os.PathLike)): |
| 94 | data = Path(path).read_bytes() |
| 95 | else: |
| 96 | data = path.read() |
| 97 | pil_image = Image.open(io.BytesIO(data)) |
| 98 | near = float(pil_image.info.get('near')) |
| 99 | far = float(pil_image.info.get('far')) |
| 100 | unit = float(pil_image.info.get('unit')) if 'unit' in pil_image.info else None |
| 101 | depth = np.array(pil_image) |
| 102 | mask_nan, mask_inf = depth == 0, depth == 65535 |
| 103 | depth = (depth.astype(np.float32) - 1) / 65533 |
| 104 | depth = near ** (1 - depth) * far ** depth |
| 105 | depth[mask_nan] = np.nan |
| 106 | depth[mask_inf] = np.inf |
| 107 | return depth, unit |
| 108 | |
| 109 | |
| 110 | def write_depth( |