1+ from pickletools import read_uint1
12from scipy .spatial .transform import Rotation
23from copy import deepcopy
34import cv2
@@ -9,25 +10,43 @@ def get_camera_mat(cam_vec):
910 return np .array ([[f , 0 , cx ], [0 , f , cy ], [0 , 0 , 1 ]], dtype = np .float32 )
1011
1112def update_camera_pose (cam_vec , R , t ):
13+ #
1214 t = t .squeeze ()
1315 rvec = Rotation .from_matrix (R ).as_rotvec ()
14- cam = cam_vec .copy () # 내부 파라미터가 안바뀌는 에러가 있음. 이유 찾아내야함.
15- cam [3 :6 ] = rvec
16- cam [6 :9 ] = t
17- return cam
16+ result = np .array ([cam_vec [0 ], cam_vec [1 ], cam_vec [2 ], rvec [0 ], rvec [1 ], rvec [2 ], t [0 ], t [1 ], t [2 ]], dtype = np .float32 )
17+ return result
1818
1919def get_projection_mat (cam_vec ):
2020 K = get_camera_mat (cam_vec = cam_vec )
2121 R = Rotation .from_rotvec (cam_vec [3 :6 ]).as_matrix ()
2222 t = cam_vec [6 :9 , np .newaxis ]
2323 Rt = np .hstack ((R , t ))
24- return K @ Rt
24+ result = K @ Rt
25+ return result
26+
27+ def isBadPoint (point_3d , camera1 , camera2 , Z_limit , max_cos_parallax ):
28+ # 무슨 내용인지 모름
29+ if point_3d [2 ] < - Z_limit or point_3d [2 ] > Z_limit :
30+ return True
31+ rvec1 , rvec2 = np .array ([camera1 [3 ], camera1 [4 ], camera1 [5 ]], dtype = np .float32 ), np .array ([camera2 [3 ], camera2 [4 ], camera2 [5 ]], dtype = np .float32 )
32+ R1 , R2 = Rotation .from_rotvec (rvec1 ).as_matrix (), Rotation .from_rotvec (rvec2 ).as_matrix ()
33+ # rot_vec to rot_mat
34+ t1 , t2 = np .array ([[camera1 [6 ], camera1 [7 ], camera1 [8 ]]], dtype = np .float32 ), np .array ([[camera2 [6 ], camera2 [7 ], camera2 [8 ]]], dtype = np .float32 )
35+ p1 = R1 @ point_3d [:, np .newaxis ] + t1 .T
36+ p2 = R2 @ point_3d [:, np .newaxis ] + t2 .T
37+ if p1 [2 ,0 ] <= 0 or p2 [2 ,0 ] <= 0 : return True
38+ v2 = R1 @ R2 .T @ p2
39+ cos_parallax = p1 .T @ v2 / (np .linalg .norm (p1 ) * np .linalg .norm (v2 ))
40+ if cos_parallax > max_cos_parallax : return True
41+ return False
42+
2543
2644def main ():
2745 img_path = "./bin/data/relief/%02d.jpg"
2846 img_resize = 0.25
2947 f_init , cx_init , cy_init , Z_init , Z_limit = 500 , - 1 , - 1 , 2 , 100
3048 ba_loss_width = 9
49+ max_cos_parallax = np .cos (10 * np .pi / 180 )
3150 min_inlier_num , ba_num_iter = 200 , 200
3251 SHOW_MATCH = False
3352
@@ -73,7 +92,7 @@ def main():
7392 F , inlier_mask = cv2 .findFundamentalMat (src , dst , cv2 .RANSAC ) # inlier_mask = 3x3
7493 for k in range (len (inlier_mask )):
7594 if inlier_mask [k ]:
76- inlier .append (match [k ]) # 매칭된 index를 넣음.
95+ inlier .append (match [k ]) #
7796 inlier = np .array (inlier )
7897 print (f"3DV Tutorial: Image { i } - { j } are matched ({ inlier .size } / { match .size } ).\n " )
7998
@@ -90,7 +109,7 @@ def main():
90109 if len (match_pair ) < 1 : return
91110
92111 # Start Initialize cameras(cam_params, rotation, translation)
93- cameras = np .full ((img_set .shape [0 ], 9 ), np .array ([f_init , cx_init , cy_init , 0 , 0 , 0 , 0 , 0 , 0 ]))
112+ cameras = np .full ((img_set .shape [0 ], 9 ), np .array ([f_init , cx_init , cy_init , 0 , 0 , 0 , 0 , 0 , 0 ]), dtype = np . float32 )
94113 best_pair = 0
95114 best_score = [i for i in range (len (match_inlier ))]
96115 best_points_3d = None
@@ -131,10 +150,14 @@ def main():
131150 best_points_3d [2 ] /= best_points_3d [3 ]
132151
133152 best_score [best_pair ] = 0
153+ for best_point_3d in best_points_3d :
154+ if isBadPoint (best_point_3d [:3 ], cameras [best_cam0 ], cameras [best_cam1 ], Z_limit , max_cos_parallax ): continue
155+ best_score [best_pair ] += 1
156+ print (f"3DV Tutorial: Image { best_cam0 } - { best_cam1 } were checked as the best match (# of inliers = { len (match_inlier [best_pair ])} , # of good points = { best_score [best_pair ]} )" )
157+ if best_score [best_pair ] > 100 : break
134158
135- #
136- break
137159 # End Initialize cameras
160+ best_cam0 = match_pair [best_pair ]
138161
139162 # Prepare the initial 3D points
140163
0 commit comments