Download code/geometry.py from Sirenna0796/depth: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/datasets/Sirenna0796/depth/resolve/main/code/geometry.py
- Command line
-
hf download hf://datasets/Sirenna0796/depth/code/geometry.py
-
curl -L -o geometry.py https://huggingface.co/datasets/Sirenna0796/depth/resolve/main/code/geometry.py
1.02 kB
| """Backprojection and local inclination from metric Z-depth.""" | |
| import cv2 | |
| import numpy as np | |
| def slope_features(depth, intrinsic, up_camera): | |
| y, x = np.indices(depth.shape, dtype=np.float32) | |
| valid = np.isfinite(depth) & (depth > 0) | |
| z = np.where(valid, depth, 0) | |
| xyz = np.stack([(x-intrinsic[0, 2])/intrinsic[0, 0]*z, | |
| (y-intrinsic[1, 2])/intrinsic[1, 1]*z, z], axis=-1) | |
| xyz = cv2.GaussianBlur(xyz, (21, 21), 4) | |
| normal = np.cross(np.gradient(xyz, axis=1), np.gradient(xyz, axis=0)) | |
| norm = np.linalg.norm(normal, axis=-1) | |
| normal /= np.maximum(norm[..., None], 1e-8) | |
| up = np.asarray(up_camera, dtype=np.float32).copy() | |
| up /= np.linalg.norm(up) | |
| slope = np.degrees(np.arccos(np.clip(np.abs(normal @ up), 0, 1))) | |
| valid = (cv2.erode(valid.astype(np.uint8), np.ones((21, 21), np.uint8)) > 0) & (norm > 1e-8) | |
| valid[:10] = valid[-10:] = False | |
| valid[:, :10] = valid[:, -10:] = False | |
| return np.where(valid, slope, np.nan).astype(np.float32), valid | |