22import ctypes as ct
33import cv2
44import sys
5- showsz = 800
6- mousex ,mousey = 0.5 ,0.5
7- zoom = 1.0
8- changed = True
5+ showsz = 800
6+ mousex , mousey = 0.5 , 0.5
7+ zoom = 1.0
8+ changed = True
9+
910def onmouse (* args ):
10- global mousex ,mousey ,changed
11- y = args [1 ]
12- x = args [2 ]
13- mousex = x / float (showsz )
14- mousey = y / float (showsz )
15- changed = True
11+ global mousex , mousey , changed
12+ y = args [1 ]
13+ x = args [2 ]
14+ mousex = x / float (showsz )
15+ mousey = y / float (showsz )
16+ changed = True
17+
1618cv2 .namedWindow ('show3d' )
17- cv2 .moveWindow ('show3d' ,0 , 0 )
18- cv2 .setMouseCallback ('show3d' ,onmouse )
19+ cv2 .moveWindow ('show3d' , 0 , 0 )
20+ cv2 .setMouseCallback ('show3d' , onmouse )
1921
20- dll = np .ctypeslib .load_library ('render_balls_so' ,'.' )
22+ dll = np .ctypeslib .load_library ('render_balls_so' , '.' )
2123
22- 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 ):
23- global showsz ,mousex ,mousey ,zoom ,changed
24+ def showpoints (xyz ,c_gt = None , c_pred = None , waittime = 0 ,
25+ showrot = False , magnifyBlue = 0 , freezerot = False , background = (0 ,0 ,0 ),
26+ normalizecolor = True , ballradius = 10 ):
27+ global showsz , mousex , mousey , zoom , changed
2428 xyz = xyz - xyz .mean (axis = 0 )
2529 radius = ((xyz ** 2 ).sum (axis = - 1 )** 0.5 ).max ()
2630 xyz /= (radius * 2.2 )/ showsz
2731 if c_gt is None :
28- c0 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
29- c1 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
30- c2 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
32+ c0 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
33+ c1 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
34+ c2 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
3135 else :
32- c0 = c_gt [:,0 ]
33- c1 = c_gt [:,1 ]
34- c2 = c_gt [:,2 ]
36+ c0 = c_gt [:, 0 ]
37+ c1 = c_gt [:, 1 ]
38+ c2 = c_gt [:, 2 ]
3539
3640
3741 if normalizecolor :
38- c0 /= (c0 .max ()+ 1e-14 )/ 255.0
39- c1 /= (c1 .max ()+ 1e-14 )/ 255.0
40- c2 /= (c2 .max ()+ 1e-14 )/ 255.0
42+ c0 /= (c0 .max () + 1e-14 ) / 255.0
43+ c1 /= (c1 .max () + 1e-14 ) / 255.0
44+ c2 /= (c2 .max () + 1e-14 ) / 255.0
4145
4246
43- c0 = np .require (c0 ,'float32' ,'C' )
44- c1 = np .require (c1 ,'float32' ,'C' )
45- c2 = np .require (c2 ,'float32' ,'C' )
47+ c0 = np .require (c0 , 'float32' , 'C' )
48+ c1 = np .require (c1 , 'float32' , 'C' )
49+ c2 = np .require (c2 , 'float32' , 'C' )
4650
47- show = np .zeros ((showsz ,showsz ,3 ),dtype = 'uint8' )
51+ show = np .zeros ((showsz , showsz , 3 ), dtype = 'uint8' )
4852 def render ():
4953 rotmat = np .eye (3 )
5054 if not freezerot :
5155 xangle = (mousey - 0.5 )* np .pi * 1.2
5256 else :
5357 xangle = 0
54- rotmat = rotmat .dot (np .array ([
55- [1.0 ,0.0 ,0.0 ],
56- [0.0 ,np .cos (xangle ),- np .sin (xangle )],
57- [0.0 ,np .sin (xangle ),np .cos (xangle )],
58+ rotmat = rotmat .dot (
59+ np .array ([
60+ [1.0 , 0.0 , 0.0 ],
61+ [0.0 , np .cos (xangle ), - np .sin (xangle )],
62+ [0.0 , np .sin (xangle ), np .cos (xangle )],
5863 ]))
5964 if not freezerot :
60- yangle = (mousex - 0.5 )* np .pi * 1.2
65+ yangle = (mousex - 0.5 ) * np .pi * 1.2
6166 else :
62- yangle = 0
63- rotmat = rotmat .dot (np .array ([
64- [np .cos (yangle ),0.0 ,- np .sin (yangle )],
65- [0.0 ,1.0 ,0.0 ],
66- [np .sin (yangle ),0.0 ,np .cos (yangle )],
67+ yangle = 0
68+ rotmat = rotmat .dot (
69+ np .array ([
70+ [np .cos (yangle ), 0.0 , - np .sin (yangle )],
71+ [0.0 , 1.0 , 0.0 ],
72+ [np .sin (yangle ), 0.0 , np .cos (yangle )],
6773 ]))
68- rotmat *= zoom
69- nxyz = xyz .dot (rotmat )+ [showsz / 2 , showsz / 2 , 0 ]
74+ rotmat *= zoom
75+ nxyz = xyz .dot (rotmat ) + [showsz / 2 , showsz / 2 , 0 ]
7076
71- ixyz = nxyz .astype ('int32' )
72- show [:]= background
77+ ixyz = nxyz .astype ('int32' )
78+ show [:] = background
7379 dll .render_ball (
74- ct .c_int (show .shape [0 ]),
75- ct .c_int (show .shape [1 ]),
76- show .ctypes .data_as (ct .c_void_p ),
77- ct .c_int (ixyz .shape [0 ]),
78- ixyz .ctypes .data_as (ct .c_void_p ),
79- c0 .ctypes .data_as (ct .c_void_p ),
80- c1 .ctypes .data_as (ct .c_void_p ),
81- c2 .ctypes .data_as (ct .c_void_p ),
82- ct .c_int (ballradius )
83- )
80+ ct .c_int (show .shape [0 ]), ct .c_int (show .shape [1 ]),
81+ show .ctypes .data_as (ct .c_void_p ), ct .c_int (ixyz .shape [0 ]),
82+ ixyz .ctypes .data_as (ct .c_void_p ), c0 .ctypes .data_as (ct .c_void_p ),
83+ c1 .ctypes .data_as (ct .c_void_p ), c2 .ctypes .data_as (ct .c_void_p ),
84+ ct .c_int (ballradius ))
8485
85- if magnifyBlue > 0 :
86- show [:,:,0 ]= np .maximum (show [:,:,0 ],np .roll (show [:,:,0 ],1 ,axis = 0 ))
87- if magnifyBlue >= 2 :
88- show [:,:,0 ]= np .maximum (show [:,:,0 ],np .roll (show [:,:,0 ],- 1 ,axis = 0 ))
89- show [:,:,0 ]= np .maximum (show [:,:,0 ],np .roll (show [:,:,0 ],1 ,axis = 1 ))
90- if magnifyBlue >= 2 :
91- show [:,:,0 ]= np .maximum (show [:,:,0 ],np .roll (show [:,:,0 ],- 1 ,axis = 1 ))
86+ if magnifyBlue > 0 :
87+ show [:, :, 0 ] = np .maximum (show [:, :, 0 ], np .roll (
88+ show [:, :, 0 ], 1 , axis = 0 ))
89+ if magnifyBlue >= 2 :
90+ show [:, :, 0 ] = np .maximum (show [:, :, 0 ],
91+ np .roll (show [:, :, 0 ], - 1 , axis = 0 ))
92+ show [:, :, 0 ] = np .maximum (show [:, :, 0 ], np .roll (
93+ show [:, :, 0 ], 1 , axis = 1 ))
94+ if magnifyBlue >= 2 :
95+ show [:, :, 0 ] = np .maximum (show [:, :, 0 ],
96+ np .roll (show [:, :, 0 ], - 1 , axis = 1 ))
9297 if showrot :
93- cv2 .putText (show ,'xangle %d' % (int (xangle / np .pi * 180 )),(30 ,showsz - 30 ),0 ,0.5 ,cv2 .cv .CV_RGB (255 ,0 ,0 ))
94- cv2 .putText (show ,'yangle %d' % (int (yangle / np .pi * 180 )),(30 ,showsz - 50 ),0 ,0.5 ,cv2 .cv .CV_RGB (255 ,0 ,0 ))
95- cv2 .putText (show ,'zoom %d%%' % (int (zoom * 100 )),(30 ,showsz - 70 ),0 ,0.5 ,cv2 .cv .CV_RGB (255 ,0 ,0 ))
96- changed = True
98+ cv2 .putText (show , 'xangle %d' % (int (xangle / np .pi * 180 )),
99+ (30 , showsz - 30 ), 0 , 0.5 , cv2 .cv .CV_RGB (255 , 0 , 0 ))
100+ cv2 .putText (show , 'yangle %d' % (int (yangle / np .pi * 180 )),
101+ (30 , showsz - 50 ), 0 , 0.5 , cv2 .cv .CV_RGB (255 , 0 , 0 ))
102+ cv2 .putText (show , 'zoom %d%%' % (int (zoom * 100 )), (30 , showsz - 70 ), 0 ,
103+ 0.5 , cv2 .cv .CV_RGB (255 , 0 , 0 ))
104+ changed = True
97105 while True :
98106 if changed :
99107 render ()
100- changed = False
101- cv2 .imshow ('show3d' ,show )
102- if waittime == 0 :
103- cmd = cv2 .waitKey (10 )% 256
108+ changed = False
109+ cv2 .imshow ('show3d' , show )
110+ if waittime == 0 :
111+ cmd = cv2 .waitKey (10 ) % 256
104112 else :
105- cmd = cv2 .waitKey (waittime )% 256
106- if cmd == ord ('q' ):
113+ cmd = cv2 .waitKey (waittime ) % 256
114+ if cmd == ord ('q' ):
107115 break
108- elif cmd == ord ('Q' ):
116+ elif cmd == ord ('Q' ):
109117 sys .exit (0 )
110118
111- if cmd == ord ('t' ) or cmd == ord ('p' ):
119+ if cmd == ord ('t' ) or cmd == ord ('p' ):
112120 if cmd == ord ('t' ):
113121 if c_gt is None :
114- c0 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
115- c1 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
116- c2 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
122+ c0 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
123+ c1 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
124+ c2 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
117125 else :
118- c0 = c_gt [:,0 ]
119- c1 = c_gt [:,1 ]
120- c2 = c_gt [:,2 ]
126+ c0 = c_gt [:, 0 ]
127+ c1 = c_gt [:, 1 ]
128+ c2 = c_gt [:, 2 ]
121129 else :
122130 if c_pred is None :
123- c0 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
124- c1 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
125- c2 = np .zeros ((len (xyz ),), dtype = 'float32' )+ 255
131+ c0 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
132+ c1 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
133+ c2 = np .zeros ((len (xyz ), ), dtype = 'float32' ) + 255
126134 else :
127- c0 = c_pred [:,0 ]
128- c1 = c_pred [:,1 ]
129- c2 = c_pred [:,2 ]
135+ c0 = c_pred [:, 0 ]
136+ c1 = c_pred [:, 1 ]
137+ c2 = c_pred [:, 2 ]
130138 if normalizecolor :
131- c0 /= (c0 .max ()+ 1e-14 )/ 255.0
132- c1 /= (c1 .max ()+ 1e-14 )/ 255.0
133- c2 /= (c2 .max ()+ 1e-14 )/ 255.0
134- c0 = np .require (c0 ,'float32' ,'C' )
135- c1 = np .require (c1 ,'float32' ,'C' )
136- c2 = np .require (c2 ,'float32' ,'C' )
139+ c0 /= (c0 .max () + 1e-14 ) / 255.0
140+ c1 /= (c1 .max () + 1e-14 ) / 255.0
141+ c2 /= (c2 .max () + 1e-14 ) / 255.0
142+ c0 = np .require (c0 , 'float32' , 'C' )
143+ c1 = np .require (c1 , 'float32' , 'C' )
144+ c2 = np .require (c2 , 'float32' , 'C' )
137145 changed = True
138146
139-
140-
141147 if cmd == ord ('n' ):
142148 zoom *= 1.1
143149 changed = True
@@ -152,7 +158,7 @@ def render():
152158 if waittime != 0 :
153159 break
154160 return cmd
155- if __name__ == '__main__' :
156- np .random .seed (100 )
157- showpoints (np .random .randn (2500 ,3 ))
158161
162+ if __name__ == '__main__' :
163+ np .random .seed (100 )
164+ showpoints (np .random .randn (2500 , 3 ))
0 commit comments