33from scipy .spatial .transform import Rotation as R
44import matplotlib .pyplot as plt
55import numpy as np
6+ from visualizer import visualizer
67
7- rgb_path = '/mount/wp/GEMstack/data/color21.png'
8- depth_path = '/mount/wp/GEMstack/data/depth21.tif'
9- lidar_path = '/mount/wp/GEMstack/data/lidar21.npz'
8+ N = 8 #how many point pairs you want to select
9+
10+ # Update Depending on Where Data Stored
11+ rgb_path = './data/color32.png'
12+ depth_path = './data/depth32.tif'
13+ lidar_path = './data/lidar32.npz'
1014
1115img = cv2 .imread (rgb_path , cv2 .IMREAD_UNCHANGED )
1216
1317lidar_points = np .load (lidar_path )['arr_0' ]
1418lidar_points = lidar_points [~ np .all (lidar_points == 0 , axis = 1 )] # remove (0,0,0)'s
1519
1620rx ,ry ,rz = 0.006898647163954201 , 0.023800082245145304 , - 0.025318355743942974
17- tx ,ty ,tz = - 1.1 , 0.037735827433173136 , 1.953202227766785
21+ tx ,ty ,tz = 1.1 , 0.037735827433173136 , 1.953202227766785
1822rot = R .from_euler ('xyz' ,[rx ,ry ,rz ]).as_matrix ()
1923lidar_ex = np .hstack ([rot ,[[tx ],[ty ],[tz ]]])
2024lidar_ex = np .vstack ([lidar_ex ,[0 ,0 ,0 ,1 ]])
2529 [ 0. , 0. , 1. ]
2630], dtype = np .float32 )
2731
32+
2833#%%
2934# blurred = cv2.GaussianBlur(img, (5, 5), 0)
3035# edges = cv2.Canny(blurred, threshold1=50, threshold2=150)
3338
3439#%%
3540import pyvista as pv
36- def vis (title = '' , ratio = 1 ):
41+ def vis (title = '' , ratio = 1 , notebook = False ):
3742 print (title )
3843 pv .set_jupyter_backend ('client' )
39- plotter = pv .Plotter (notebook = True )
40- plotter .show_axes ()
41- class foo :
42- def set_cam (self ,pos = (- 20 * ratio ,0 ,20 * ratio ),foc = (0 ,0 ,0 )):
43- plotter .camera .position = pos
44- plotter .camera .focal_point = foc
45- return self
46- def add_pc (self ,pc ,ratio = ratio ,** kargs ):
47- plotter .add_mesh (
48- pv .PolyData (pc * ratio ),
49- render_points_as_spheres = True ,
50- point_size = 2 ,
51- ** kargs )
52- return self
53- def add_line (self ,p1 ,p2 ,ratio = ratio ,** kargs ):
54- plotter .add_mesh (
55- pv .Line (p1 * ratio ,p2 * ratio ),
56- ** kargs ,
57- line_width = 1 )
58- return self
59- def add_box (self ,bound ,trans ,ratio = ratio ):
60- l ,w ,h = map (lambda x :x * ratio ,bound )
61- box = pv .Box (bounds = (- l / 2 ,l / 2 ,- w / 2 ,w / 2 ,- h / 2 ,h / 2 ))
62- box = box .translate (list (map (lambda x :x * ratio ,trans )))
63- plotter .add_mesh (box , color = 'yellow' )
64- return self
65- def show (self ):
66- plotter .show ()
67- return self
68- def close (self ):
69- plotter .close ()
70- return None
7144
72- return foo ().set_cam ()
45+ return visualizer ().set_cam ()
7346def crop (pc ,ix = None ,iy = None ,iz = None ):
7447 # crop a subrectangle in a pointcloud
7548 # usage: crop(pc, ix = (x_min,x_max), ...)
@@ -85,11 +58,11 @@ def crop(pc,ix=None,iy=None,iz=None):
8558
8659
8760lidar_post = np .pad (lidar_points ,((0 ,0 ),(0 ,1 )),constant_values = 1 ) @ lidar_ex .T [:,:3 ]
88- lidar_post = crop (lidar_post ,ix = (0 ,8 ),iy = (- 5 ,5 ))
89- vis ().add_pc (lidar_post ).show ()
61+ lidar_post = crop (lidar_post ,ix = (0 ,10 ),iy = (- 5 ,5 ))
62+ # vis(notebook=False ).add_pc(lidar_post).show()
9063
9164#%%
92- def pick_4_img (img ):
65+ def pick_n_img (img , n = 4 ):
9366 corners = [] # Reset the corners list
9467 def click_event (event , x , y , flags , param ):
9568 if event == cv2 .EVENT_LBUTTONDOWN :
@@ -101,23 +74,23 @@ def click_event(event, x, y, flags, param):
10174 cv2 .setMouseCallback ('Image' , click_event , img )
10275
10376 while True :
104- if len (corners ) == 4 :
77+ if len (corners ) == n :
10578 break
10679 if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
10780 return None
10881
10982 cv2 .destroyAllWindows ()
11083
11184 return corners
112- cpoints = np .array (pick_4_img (img )).astype (float )
85+ cpoints = np .array (pick_n_img (img , N )).astype (float )
11386print (cpoints )
11487
11588#%%
116- def pick_4_pc (point_cloud ):
89+ def pick_n_pc (point_cloud , n = 4 ):
11790 points = []
11891 def cb (pt ,* args ):
11992 points .append (pt )
120- while len (points )!= 4 :
93+ while len (points )!= n :
12194 points = []
12295 cloud = pv .PolyData (point_cloud )
12396 plotter = pv .Plotter (notebook = False )
@@ -128,7 +101,7 @@ def cb(pt,*args):
128101 plotter .show ()
129102 return points
130103
131- lpoints = np .array (pick_4_pc (lidar_post ))
104+ lpoints = np .array (pick_n_pc (lidar_post , N ))
132105print (lpoints )
133106# %%
134107success , rvec , tvec = cv2 .solvePnP (lpoints , cpoints , camera_in , None )
@@ -182,10 +155,9 @@ def depth_to_points(depth_img: np.ndarray, intrinsics: np.ndarray):
182155depth_img = cv2 .imread (depth_path , cv2 .IMREAD_UNCHANGED )
183156camera_points = depth_to_points (depth_img , camera_in )
184157
185- v = vis ()
186- v .add_pc (np .pad (lidar_points ,((0 ,0 ),(0 ,1 )),constant_values = 1 )@lidar_ex .T @T .T [:,:3 ],color = 'blue' )
187- v .add_pc (camera_points ,color = 'red' )
188- v .show ()
189-
190158#%%
191- print (np .vstack ([(lidar_ex .T @T .T [:,:3 ]).T ,[0 ,0 ,0 ,1 ]]))
159+ v2c = T
160+ print ('vehicle->camera:' ,v2c )
161+ c2v = np .linalg .inv (v2c )
162+ print ('camera->vehicle:' ,c2v )
163+
0 commit comments