File size: 1,018 Bytes
095b09d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | """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
|