forked from yasumat/RobustPhotometricStereo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psutil.py
163 lines (140 loc) · 4.9 KB
/
psutil.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import cv2
import glob
import numpy as np
def load_lighttxt(filename=None):
"""
Load light file specified by filename.
The format of lights.txt should be
light1_x light1_y light1_z
light2_x light2_y light2_z
...
lightf_x lightf_y lightf_z
:param filename: filename of lights.txt
:return: light matrix (3 \times f)
"""
if filename is None:
raise ValueError("filename is None")
Lt = np.loadtxt(filename)
return Lt.T
def load_lightnpy(filename=None):
"""
Load light numpy array file specified by filename.
The format of lights.npy should be
light1_x light1_y light1_z
light2_x light2_y light2_z
...
lightf_x lightf_y lightf_z
:param filename: filename of lights.npy
:return: light matrix (3 \times f)
"""
if filename is None:
raise ValueError("filename is None")
Lt = np.load(filename)
return Lt.T
def load_image(filename=None):
"""
Load image specified by filename (read as a gray-scale)
:param filename: filename of the image to be loaded
:return img: loaded image
"""
if filename is None:
raise ValueError("filename is None")
return cv2.imread(filename, 0)
def load_images(foldername=None, ext=None):
"""
Load images in the folder specified by the "foldername" that have extension "ext"
:param foldername: foldername
:param ext: file extension
:return: measurement matrix (numpy array) whose column vector corresponds to an image (p \times f)
"""
if foldername is None or ext is None:
raise ValueError("filename/ext is None")
M = None
height = 0
width = 0
for fname in sorted(glob.glob(foldername + "*." + ext)):
im = cv2.imread(fname).astype(np.float64)
if im.ndim == 3:
# Assuming that RGBA will not be an input
im = np.mean(im, axis=2) # RGB -> Gray
if M is None:
height, width = im.shape
M = im.reshape((-1, 1))
else:
M = np.append(M, im.reshape((-1, 1)), axis=1)
return M, height, width
def load_npyimages(foldername=None):
"""
Load images in the folder specified by the "foldername" in the numpy format
:param foldername: foldername
:return: measurement matrix (numpy array) whose column vector corresponds to an image (p \times f)
"""
if foldername is None:
raise ValueError("filename is None")
M = None
height = 0
width = 0
for fname in sorted(glob.glob(foldername + "*.npy")):
im = np.load(fname)
if im.ndim == 3:
im = np.mean(im, axis=2)
if M is None:
height, width = im.shape
M = im.reshape((-1, 1))
else:
M = np.append(M, im.reshape((-1, 1)), axis=1)
return M, height, width
def disp_normalmap(normal=None, height=None, width=None, delay=0, name=None):
"""
Visualize normal as a normal map
:param normal: array of surface normal (p \times 3)
:param height: height of the image (scalar)
:param width: width of the image (scalar)
:param delay: duration (ms) for visualizing normal map. 0 for displaying infinitely until a key is pressed.
:param name: display name
:return: None
"""
if normal is None:
raise ValueError("Surface normal `normal` is None")
N = np.reshape(normal, (height, width, 3)) # Reshape to image coordinates
N[:, :, 0], N[:, :, 2] = N[:, :, 2], N[:, :, 0].copy() # Swap RGB <-> BGR
N = (N + 1.0) / 2.0 # Rescale
if name is None:
name = 'normal map'
cv2.imshow(name, N)
cv2.waitKey(delay)
cv2.destroyWindow(name)
cv2.waitKey(1) # to deal with frozen window...
def save_normalmap_as_npy(filename=None, normal=None, height=None, width=None):
"""
Save surface normal array as a numpy array
:param filename: filename of the normal array
:param normal: surface normal array (height \times width \times 3)
:return: None
"""
if filename is None:
raise ValueError("filename is None")
N = np.reshape(normal, (height, width, 3))
np.save(filename, N)
def load_normalmap_from_npy(filename=None):
"""
Load surface normal array (which is a numpy array)
:param filename: filename of the normal array
:return: surface normal (numpy array) in formatted in (height, width, 3).
"""
if filename is None:
raise ValueError("filename is None")
return np.load(filename)
def evaluate_angular_error(gtnormal=None, normal=None, background=None):
if gtnormal is None or normal is None:
raise ValueError("surface normal is not given")
ae = np.multiply(gtnormal, normal)
aesum = np.sum(ae, axis=1)
coord = np.where(aesum > 1.0)
aesum[coord] = 1.0
coord = np.where(aesum < -1.0)
aesum[coord] = -1.0
ae = np.arccos(aesum) * 180.0 / np.pi
if background is not None:
ae[background] = 0
return ae