-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.py
310 lines (266 loc) · 11.3 KB
/
rps.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Robust Photometric Stereo in Python
"""
__author__ = "Yasuyuki Matsushita <[email protected]>"
__version__ = "0.1.0"
__date__ = "11 May 2018"
import psutil
import rpsnumerics
import numpy as np
from sklearn.preprocessing import normalize
class RPS(object):
"""
Robust Photometric Stereo class
"""
# Choice of solution methods
L2_SOLVER = 0 # Conventional least-squares
L1_SOLVER = 1 # L1 residual minimization
L1_SOLVER_MULTICORE = 2 # L1 residual minimization (multicore)
SBL_SOLVER = 3 # Sparse Bayesian Learning
SBL_SOLVER_MULTICORE = 4 # Sparse Bayesian Learning (multicore)
RPCA_SOLVER = 5 # Robust PCA
def __init__(self):
self.M = None # measurement matrix in numpy array
self.L = None # light matrix in numpy array
self.N = None # surface normal matrix in numpy array
self.height = None # image height
self.width = None # image width
self.foreground_ind = None # mask (indices of active pixel locations (rows of M))
self.background_ind = None # mask (indices of inactive pixel locations (rows of M))
def load_lighttxt(self, 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
"""
self.L = psutil.load_lighttxt(filename)
def load_relight_lp(self, filename=None):
"""
Load light file specified by filename.
The format is that of the relight GUI.
:param filename: filename of lights.txt
"""
self.L = psutil.load_relight_lp(filename)
def load_lightnpy(self, 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
"""
self.L = psutil.load_lightnpy(filename)
def load_images(self, foldername=None, ext=None):
"""
Load images in the folder specified by the "foldername" that have extension "ext"
:param foldername: foldername
:param ext: file extension
"""
self.M, self.height, self.width = psutil.load_images(foldername, ext)
def load_npyimages(self, foldername=None):
"""
Load images in the folder specified by the "foldername" in the numpy format
:param foldername: foldername
"""
self.M, self.height, self.width = psutil.load_npyimages(foldername)
def load_mask(self, filename=None):
"""
Load mask image and set the mask indices
In the mask image, pixels with zero intensity will be ignored.
:param filename: filename of the mask image
:return: None
"""
if filename is None:
raise ValueError("filename is None")
mask = psutil.load_image(filename=filename)
mask = mask.reshape((-1, 1))
self.foreground_ind = np.where(mask != 0)[0]
self.background_ind = np.where(mask == 0)[0]
def disp_normalmap(self, delay=0):
"""
Visualize normal map
:return: None
"""
psutil.disp_normalmap(normal=self.N, height=self.height, width=self.width, delay=delay)
def save_normalmap(self, filename=None):
"""
Saves normal map as numpy array format (npy)
:param filename: filename of a normal map
:return: None
"""
psutil.save_normalmap_as_npy(filename=filename, normal=self.N, height=self.height, width=self.width)
def solve(self, method=L2_SOLVER):
if self.M is None:
raise ValueError("Measurement M is None")
if self.L is None:
raise ValueError("Light L is None")
if self.M.shape[1] != self.L.shape[1]:
raise ValueError("Inconsistent dimensionality between M and L")
if method == RPS.L2_SOLVER:
self._solve_l2()
elif method == RPS.L1_SOLVER:
self._solve_l1()
elif method == RPS.L1_SOLVER_MULTICORE:
self._solve_l1_multicore()
elif method == RPS.SBL_SOLVER:
self._solve_sbl()
elif method == RPS.SBL_SOLVER_MULTICORE:
self._solve_sbl_multicore()
elif method == RPS.RPCA_SOLVER:
self._solve_rpca()
else:
raise ValueError("Undefined solver")
def _solve_l2(self):
"""
Lambertian Photometric stereo based on least-squares
Woodham 1980
:return: None
Compute surface normal : numpy array of surface normal (p \times 3)
"""
self.N = np.linalg.lstsq(self.L.T, self.M.T, rcond=None)[0].T
self.N = normalize(self.N, axis=1) # normalize to account for diffuse reflectance
if self.background_ind is not None:
for i in range(self.N.shape[1]):
self.N[self.background_ind, i] = 0
def _solve_l1(self):
"""
Lambertian Photometric stereo based on sparse regression (L1 residual minimization)
Satoshi Ikehata, David P. Wipf, Yasuyuki Matsushita, Kiyoharu Aizawa:
Robust photometric stereo using sparse regression. CVPR 2012: 318-325
:return: None
Compute surface normal : numpy array of surface normal (p \times 3)
"""
A = self.L.T
self.N = np.zeros((self.M.shape[0], 3))
if self.foreground_ind is None:
indices = range(self.M.shape[0])
else:
indices = self.foreground_ind
for index in indices:
b = np.array([self.M[index, :]]).T
n = rpsnumerics.L1_residual_min(A, b)
self.N[index, :] = n.ravel()
self.N = normalize(self.N, axis=1)
def _solve_l1_multicore(self):
"""
Lambertian Photometric stereo based on sparse regression (L1 residual minimization)
Satoshi Ikehata, David P. Wipf, Yasuyuki Matsushita, Kiyoharu Aizawa:
Robust photometric stereo using sparse regression. CVPR 2012: 318-325
:return: None
Compute surface normal : numpy array of surface normal (p \times 3)
"""
from multiprocessing import Pool
import multiprocessing
if self.foreground_ind is None:
indices = range(self.M.shape[0])
else:
indices = self.foreground_ind
p = Pool(processes=multiprocessing.cpu_count()-1)
normal = p.map(self._solve_l1_multicore_impl, indices)
if self.foreground_ind is None:
self.N = np.asarray(normal)
self.N = normalize(self.N, axis=1)
else:
N = np.asarray(normal)
N = normalize(N, axis=1)
self.N = np.zeros((self.M.shape[0], 3))
for i in range(N.shape[1]):
self.N[self.foreground_ind, i] = N[:, i]
def _solve_l1_multicore_impl(self, index):
"""
Implementation of Lambertian Photometric stereo based on sparse regression (L1 residual minimization)
Satoshi Ikehata, David P. Wipf, Yasuyuki Matsushita, Kiyoharu Aizawa:
Robust photometric stereo using sparse regression. CVPR 2012: 318-325
:param index: an index of a measurement (row of M)
:return: a row vector of surface normal at pixel index specified by "index"
"""
A = self.L.T
b = np.array([self.M[index, :]]).T
n = rpsnumerics.L1_residual_min(A, b) # row vector of a surface normal at pixel "index"
return n.ravel()
def _solve_sbl(self):
"""
Lambertian Photometric stereo based on sparse regression (Sparse Bayesian learning)
Satoshi Ikehata, David P. Wipf, Yasuyuki Matsushita, Kiyoharu Aizawa:
Robust photometric stereo using sparse regression. CVPR 2012: 318-325
:return: None
Compute surface normal : numpy array of surface normal (p \times 3)
"""
A = self.L.T
self.N = np.zeros((self.M.shape[0], 3))
if self.foreground_ind is None:
indices = range(self.M.shape[0])
else:
indices = self.foreground_ind
for index in indices:
b = np.array([self.M[index, :]]).T
n = rpsnumerics.sparse_bayesian_learning(A, b)
self.N[index, :] = n.ravel()
self.N = normalize(self.N, axis=1)
def _solve_sbl_multicore(self):
"""
Lambertian Photometric stereo based on sparse regression (Sparse Bayesian learning)
Satoshi Ikehata, David P. Wipf, Yasuyuki Matsushita, Kiyoharu Aizawa:
Robust photometric stereo using sparse regression. CVPR 2012: 318-325
:return: None
Compute surface normal : numpy array of surface normal (p \times 3)
"""
from multiprocessing import Pool
import multiprocessing
if self.foreground_ind is None:
indices = range(self.M.shape[0])
else:
indices = self.foreground_ind
p = Pool(processes=multiprocessing.cpu_count()-1)
normal = p.map(self._solve_sbl_multicore_impl, indices)
if self.foreground_ind is None:
self.N = np.asarray(normal)
self.N = normalize(self.N, axis=1)
else:
N = np.asarray(normal)
N = normalize(N, axis=1)
self.N = np.zeros((self.M.shape[0], 3))
for i in range(self.N.shape[1]):
self.N[self.foreground_ind, i] = N[:, i]
def _solve_sbl_multicore_impl(self, index):
"""
Implementation of Lambertian Photometric stereo based on sparse regression (Sparse Bayesian learning)
Satoshi Ikehata, David P. Wipf, Yasuyuki Matsushita, Kiyoharu Aizawa:
Robust photometric stereo using sparse regression. CVPR 2012: 318-325
:param index: an index of a measurement (row of M)
:return: a row vector of surface normal at pixel index specified by "index"
"""
A = self.L.T
b = np.array([self.M[index, :]]).T
n = rpsnumerics.sparse_bayesian_learning(A, b) # row vector of a surface normal at pixel "index"
return n.ravel()
def _solve_rpca(self):
"""
Photometric stereo based on robust PCA.
Lun Wu, Arvind Ganesh, Boxin Shi, Yasuyuki Matsushita, Yongtian Wang, Yi Ma:
Robust Photometric Stereo via Low-Rank Matrix Completion and Recovery. ACCV (3) 2010: 703-717
:return: None
Compute surface normal : numpy array of surface normal (p \times 3)
"""
if self.foreground_ind is None:
_M = self.M.T
else:
_M = self.M[self.foreground_ind, :].T
A, E, ite = rpsnumerics.rpca_inexact_alm(_M) # RPCA Photometric stereo
if self.foreground_ind is None:
self.N = np.linalg.lstsq(self.L.T, A, rcond=None)[0].T
self.N = normalize(self.N, axis=1) # normalize to account for diffuse reflectance
else:
N = np.linalg.lstsq(self.L.T, A, rcond=None)[0].T
N = normalize(N, axis=1) # normalize to account for diffuse reflectance
self.N = np.zeros((self.M.shape[0], 3))
for i in range(self.N.shape[1]):
self.N[self.foreground_ind, i] = N[:, i]