forked from Insta360-Research-Team/DAP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth2normal.py
More file actions
139 lines (106 loc) · 4.64 KB
/
depth2normal.py
File metadata and controls
139 lines (106 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import cv2
import torch
import torch.nn.functional as F
from PIL import Image
import utils3d # Your original utility library
import os
# ----------------------------
# Utilities
# ----------------------------
def spherical_uv_to_directions(uv: np.ndarray):
theta, phi = (1 - uv[..., 0]) * (2 * np.pi), uv[..., 1] * np.pi
directions = np.stack([
np.sin(phi) * np.cos(theta),
np.sin(phi) * np.sin(theta),
np.cos(phi)
], axis=-1)
return directions
def spherical_uv_to_directions_torch(uv: torch.Tensor, device: str = 'cuda'):
theta, phi = (1 - uv[..., 0]) * (2 * np.pi), uv[..., 1] * np.pi
directions = torch.stack([
torch.sin(phi) * torch.cos(theta),
torch.sin(phi) * torch.sin(theta),
torch.cos(phi)
], axis=-1).to(device)
return directions
def normal_normalize(normal: np.ndarray):
normal_norm = np.linalg.norm(normal, axis=-1, keepdims=True)
normal_norm[normal_norm < 1e-6] = 1e-6
return normal / normal_norm
def normal_normalize_torch(normal: torch.Tensor):
normal_norm = torch.norm(normal, dim=-1, keepdim=True)
normal_norm = torch.where(
normal_norm < 1e-6,
torch.tensor(1e-6, device=normal_norm.device, dtype=normal_norm.dtype),
normal_norm
)
return normal / normal_norm
def normal_to_rgb(normal: np.ndarray | torch.Tensor, normal_mask: np.ndarray | torch.Tensor = None):
""" normal ([-1,1]) → RGB ([0,255]) """
if torch.is_tensor(normal):
normal = normal.detach().cpu().numpy()
if normal_mask is not None:
normal_mask = normal_mask.detach().cpu().numpy()
normal_rgb = (((normal + 1) * 0.5) * 255).astype(np.uint8)
if normal_mask is not None:
normal_mask_c = np.stack([normal_mask]*3, axis=-1).astype(np.uint8)
normal_rgb = normal_rgb * normal_mask_c
return normal_rgb
# ----------------------------
# Depth to normal (NumPy version)
# ----------------------------
def depth2normal(depth: np.ndarray, mask: np.ndarray = None, to_rgb: bool = False):
h, w = depth.shape[:2]
# depth → 3D points
points = depth[:, :, None] * spherical_uv_to_directions(utils3d.numpy.image_uv(width=w, height=h))
if mask is None:
mask = np.ones_like(depth, dtype=bool)
normal, normal_mask = utils3d.numpy.points_to_normals(points, mask)
# Adjust direction & normalize
normal = normal * np.array([-1, -1, 1])
normal = normal_normalize(normal)
# Reorder channels (same as your original code)
normal = np.stack([normal[..., 0], normal[..., 2], normal[..., 1]], axis=-1)
if to_rgb:
return normal, normal_mask, Image.fromarray(normal_to_rgb(normal, normal_mask))
else:
return normal, normal_mask
# ----------------------------
# Depth to normal (Torch version)
# ----------------------------
def depth2normal_torch(depth: torch.Tensor, mask: torch.Tensor = None, to_rgb: bool = False):
h, w = depth.shape[-2:]
points = depth.unsqueeze(-1) * spherical_uv_to_directions_torch(utils3d.torch.image_uv(width=w, height=h), device=depth.device)
if mask is None:
mask = torch.ones_like(depth, dtype=torch.uint8)
normal, normal_mask = utils3d.torch.points_to_normals(points, mask)
# Adjust direction
normal = normal * torch.tensor([-1, -1, 1], device=normal.device, dtype=normal.dtype)
normal = normal_normalize_torch(normal)
# Reorder channels
normal = torch.stack([normal[..., 0], normal[..., 2], normal[..., 1]], dim=-1)
if to_rgb:
normal_mask_img = normal_mask.squeeze()
normal_imgs = [Image.fromarray(normal_to_rgb(normal[i], normal_mask_img[i])) for i in range(normal.shape[0])]
return normal, normal_mask, normal_imgs
else:
return normal, normal_mask
# ----------------------------
# Main
# ----------------------------
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--img-path', default='', type=str)
args = parser.parse_args()
# args.img_path is a directory containing multiple depth maps
save_out = os.path.dirname(args.img_path) + '/normal'
os.makedirs(save_out, exist_ok=True)
for depth_path in os.listdir(args.img_path):
depth_path = os.path.join(args.img_path, depth_path)
depth = np.load(depth_path).astype(np.float32)
# depth = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255
normal, mask, normal_img = depth2normal(depth, to_rgb=True)
normal_img.save(os.path.join(save_out, depth_path.split('/')[-1].replace('.npy', '.png')))
# normal_img.save(os.path.join(save_out, depth_path.split('/')[-1]))