-
Notifications
You must be signed in to change notification settings - Fork 23
/
show3d_balls.py
267 lines (229 loc) · 8.26 KB
/
show3d_balls.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
import numpy as np
import ctypes as ct
import cv2
import sys
showsz=800
mousex,mousey=0.5,0.5
zoom=1.0
changed=True
def onmouse(*args):
global mousex,mousey,changed
y=args[1]
x=args[2]
mousex=x/float(showsz)
mousey=y/float(showsz)
changed=True
cv2.namedWindow('show3d')
cv2.moveWindow('show3d',0,0)
cv2.setMouseCallback('show3d',onmouse)
dll=np.ctypeslib.load_library('render_balls_so','.')
def showpoints(xyz,c_gt=None, c_pred = None ,waittime=0,showrot=False,magnifyBlue=0,freezerot=False,background=(0,0,0),normalizecolor=True,ballradius=10):
global showsz,mousex,mousey,zoom,changed, idx
idx = 0
if len(xyz.shape) == 2:
xyz = np.expand_dims(xyz, 0)
num_samples = xyz.shape[0]
for i in range(num_samples):
xyz[i]=xyz[i]-xyz[i].mean(axis=0)
radius=((xyz[i]**2).sum(axis=-1)**0.5).max()
xyz[i]/=(radius*2.2)/showsz
if c_gt is None:
c0=np.zeros((len(xyz[idx]),),dtype='float32')+255
c1=np.zeros((len(xyz[idx]),),dtype='float32')+255
c2=np.zeros((len(xyz[idx]),),dtype='float32')+255
else:
c0=c_gt[:,0]
c1=c_gt[:,1]
c2=c_gt[:,2]
if normalizecolor:
c0/=(c0.max()+1e-14)/255.0
c1/=(c1.max()+1e-14)/255.0
c2/=(c2.max()+1e-14)/255.0
c0=np.require(c0,'float32','C')
c1=np.require(c1,'float32','C')
c2=np.require(c2,'float32','C')
show=np.zeros((showsz,showsz,3),dtype='uint8')
def render():
rotmat=np.eye(3)
if not freezerot:
xangle=(mousey-0.5)*np.pi*1.2
else:
xangle=0
rotmat=rotmat.dot(np.array([
[1.0,0.0,0.0],
[0.0,np.cos(xangle),-np.sin(xangle)],
[0.0,np.sin(xangle),np.cos(xangle)],
]))
if not freezerot:
yangle=(mousex-0.5)*np.pi*1.2
else:
yangle=0
rotmat=rotmat.dot(np.array([
[np.cos(yangle),0.0,-np.sin(yangle)],
[0.0,1.0,0.0],
[np.sin(yangle),0.0,np.cos(yangle)],
]))
rotmat*=zoom
nxyz=xyz[idx].dot(rotmat)+[showsz/2,showsz/2,0]
ixyz=nxyz.astype('int32')
show[:]=background
dll.render_ball(
ct.c_int(show.shape[0]),
ct.c_int(show.shape[1]),
show.ctypes.data_as(ct.c_void_p),
ct.c_int(ixyz.shape[0]),
ixyz.ctypes.data_as(ct.c_void_p),
c0.ctypes.data_as(ct.c_void_p),
c1.ctypes.data_as(ct.c_void_p),
c2.ctypes.data_as(ct.c_void_p),
ct.c_int(ballradius)
)
if magnifyBlue>0:
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],1,axis=0))
if magnifyBlue>=2:
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],-1,axis=0))
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],1,axis=1))
if magnifyBlue>=2:
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],-1,axis=1))
if showrot:
cv2.putText(show,'xangle %d'%(int(xangle/np.pi*180)),(30,showsz-30),0,0.5,cv2.cv.CV_RGB(255,0,0))
cv2.putText(show,'yangle %d'%(int(yangle/np.pi*180)),(30,showsz-50),0,0.5,cv2.cv.CV_RGB(255,0,0))
cv2.putText(show,'zoom %d%%'%(int(zoom*100)),(30,showsz-70),0,0.5,cv2.cv.CV_RGB(255,0,0))
changed=True
while True:
if changed:
render()
changed=False
cv2.imshow('show3d',show)
if waittime==0:
cmd=cv2.waitKey(10)%256
else:
cmd=cv2.waitKey(waittime)%256
if cmd==ord('q'):
break
elif cmd==ord('Q'):
sys.exit(0)
if cmd==ord('t') or cmd == ord('p'):
if cmd == ord('t'):
if c_gt is None:
c0=np.zeros((len(xyz[idx]),),dtype='float32')+255
c1=np.zeros((len(xyz[idx]),),dtype='float32')+255
c2=np.zeros((len(xyz[idx]),),dtype='float32')+255
else:
c0=c_gt[:,0]
c1=c_gt[:,1]
c2=c_gt[:,2]
else:
if c_pred is None:
c0=np.zeros((len(xyz[idx]),),dtype='float32')+255
c1=np.zeros((len(xyz[idx]),),dtype='float32')+255
c2=np.zeros((len(xyz[idx]),),dtype='float32')+255
else:
c0=c_pred[:,0]
c1=c_pred[:,1]
c2=c_pred[:,2]
if normalizecolor:
c0/=(c0.max()+1e-14)/255.0
c1/=(c1.max()+1e-14)/255.0
c2/=(c2.max()+1e-14)/255.0
c0=np.require(c0,'float32','C')
c1=np.require(c1,'float32','C')
c2=np.require(c2,'float32','C')
changed = True
if cmd==ord('j'):
idx = (idx + 1) % num_samples
print(idx)
changed=True
if cmd==ord('k'):
idx = (idx - 1) % num_samples
print(idx)
changed=True
if cmd==ord('n'):
zoom*=1.1
changed=True
elif cmd==ord('m'):
zoom/=1.1
changed=True
elif cmd==ord('r'):
zoom=1.0
changed=True
elif cmd==ord('s'):
cv2.imwrite('show3d.png',show)
if waittime!=0:
break
return cmd
def showpoints_frame(xyz,c_gt=None, c_pred = None ,waittime=0,showrot=False,magnifyBlue=0,freezerot=False,background=(0,0,0),normalizecolor=True,ballradius=10):
#global showsz,mousex,mousey,zoom,changed, idx
showsz=800
mousex,mousey=0.5,0.5
zoom=1.0
changed=True
idx = 0
if len(xyz.shape) == 2:
xyz = np.expand_dims(xyz, 0)
num_samples = xyz.shape[0]
for i in range(num_samples):
xyz[i]=xyz[i]-xyz[i].mean(axis=0)
radius=((xyz[i]**2).sum(axis=-1)**0.5).max()
xyz[i]/=(radius*2.2)/showsz
if c_gt is None:
c0=np.zeros((len(xyz[idx]),),dtype='float32')+255
c1=np.zeros((len(xyz[idx]),),dtype='float32')+255
c2=np.zeros((len(xyz[idx]),),dtype='float32')+255
else:
c0=c_gt[:,0]
c1=c_gt[:,1]
c2=c_gt[:,2]
if normalizecolor:
c0/=(c0.max()+1e-14)/255.0
c1/=(c1.max()+1e-14)/255.0
c2/=(c2.max()+1e-14)/255.0
c0=np.require(c0,'float32','C')
c1=np.require(c1,'float32','C')
c2=np.require(c2,'float32','C')
show=np.zeros((showsz,showsz,3),dtype='uint8')
def render():
rotmat=np.eye(3)
xangle=-np.pi/4
rotmat=rotmat.dot(np.array([
[1.0,0.0,0.0],
[0.0,np.cos(xangle),-np.sin(xangle)],
[0.0,np.sin(xangle),np.cos(xangle)],
]))
yangle=-np.pi/4
rotmat=rotmat.dot(np.array([
[np.cos(yangle),0.0,-np.sin(yangle)],
[0.0,1.0,0.0],
[np.sin(yangle),0.0,np.cos(yangle)],
]))
rotmat*=zoom
nxyz=xyz[idx].dot(rotmat)+[showsz/2,showsz/2,0]
ixyz=nxyz.astype('int32')
show[:]=background
dll.render_ball(
ct.c_int(show.shape[0]),
ct.c_int(show.shape[1]),
show.ctypes.data_as(ct.c_void_p),
ct.c_int(ixyz.shape[0]),
ixyz.ctypes.data_as(ct.c_void_p),
c0.ctypes.data_as(ct.c_void_p),
c1.ctypes.data_as(ct.c_void_p),
c2.ctypes.data_as(ct.c_void_p),
ct.c_int(ballradius)
)
if magnifyBlue>0:
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],1,axis=0))
if magnifyBlue>=2:
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],-1,axis=0))
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],1,axis=1))
if magnifyBlue>=2:
show[:,:,0]=np.maximum(show[:,:,0],np.roll(show[:,:,0],-1,axis=1))
if showrot:
cv2.putText(show,'xangle %d'%(int(xangle/np.pi*180)),(30,showsz-30),0,0.5,cv2.cv.CV_RGB(255,0,0))
cv2.putText(show,'yangle %d'%(int(yangle/np.pi*180)),(30,showsz-50),0,0.5,cv2.cv.CV_RGB(255,0,0))
cv2.putText(show,'zoom %d%%'%(int(zoom*100)),(30,showsz-70),0,0.5,cv2.cv.CV_RGB(255,0,0))
render()
return show
if __name__=='__main__':
np.random.seed(100)
showpoints(np.random.randn(2500,3))