objectPoints_historic[1, -number_of_points:], '.', color='red', ) pl.plot_plane() ax_object.set_title('Object Points') ax_object.set_xlim(-pl.radius - 0.05, pl.radius + 0.05) ax_object.set_ylim(-pl.radius - 0.05, pl.radius + 0.05) ax_object.set_aspect('equal') plt.show() plt.pause(0.001) if calc_metrics: #CONDITION NUMBER CALCULATION input_list = gd.extract_objectpoints_vars(new_objectPoints) input_list.append(np.array(cam.P)) mat_cond = gd.matrix_condition_number_autograd(*input_list, normalize=False) #CONDITION NUMBER WITH A NORMALIZED CALCULATION input_list = gd.extract_objectpoints_vars(new_objectPoints) input_list.append(np.array(cam.P)) mat_cond_normalized = gd.matrix_condition_number_autograd( *input_list, normalize=True) DataOut['cond_number'].append(mat_cond) DataOut['cond_number_norm'].append(mat_cond_normalized) ##HOMOGRAPHY ERRORS ## TRUE VALUE OF HOMOGRAPHY OBTAINED FROM CAMERA PARAMETERS
DataSinglePose['Angle'] = angle DataSinglePose['Camera'] = cam.clone() DataSinglePose['Iters'] = [] DataSinglePose['ObjectPoints'] = [] DataSinglePose['ImagePoints'] = [] DataSinglePose['CondNumber'] = [] objectPoints_iter = np.copy(objectPoints_start) imagePoints_iter = np.array(cam.project(objectPoints_iter, False)) gradient = gd.create_gradient(metric='condition_number', n=n) for i in range(1000): DataSinglePose['ObjectPoints'].append(objectPoints_iter) DataSinglePose['ImagePoints'].append(imagePoints_iter) input_list = gd.extract_objectpoints_vars(objectPoints_iter) input_list.append(np.array(cam.P)) # TODO Yue: set parameter image_pts_measured as None and append it to input_list input_list.append(None) mat_cond = gd.matrix_condition_number_autograd(*input_list, normalize=False) DataSinglePose['CondNumber'].append(mat_cond) #PLOT IMAGE POINTS plt.sca(ax_image) plt.ion() if i == 0: ax_image.cla() ax_image.plot( imagePoints_iter[0],
def calculate_metrics(self): new_objectPoints = self.ObjectPoints[-1] cam = self.Camera[-1] validation_plane = self.ValidationPlane new_imagePoints = np.array(cam.project(new_objectPoints, False)) self.ImagePoints.append(new_imagePoints) #CONDITION NUMBER CALCULATION input_list = gd.extract_objectpoints_vars(new_objectPoints) input_list.append(np.array(cam.P)) mat_cond = gd.matrix_condition_number_autograd(*input_list, normalize=False) #CONDITION NUMBER WITH A NORMALIZED CALCULATION input_list = gd.extract_objectpoints_vars(new_objectPoints) input_list.append(np.array(cam.P)) mat_cond_normalized = gd.matrix_condition_number_autograd( *input_list, normalize=True) self.CondNumber.append(mat_cond) self.CondNumberNorm.append(mat_cond_normalized) ##HOMOGRAPHY ERRORS ## TRUE VALUE OF HOMOGRAPHY OBTAINED FROM CAMERA PARAMETERS Hcam = cam.homography_from_Rt() ##We add noise to the image points and calculate the noisy homography homo_dlt_error_loop = [] homo_HO_error_loop = [] homo_CV_error_loop = [] ippe_tvec_error_loop = [] ippe_rmat_error_loop = [] epnp_tvec_error_loop = [] epnp_rmat_error_loop = [] pnp_tvec_error_loop = [] pnp_rmat_error_loop = [] # WE CREATE NOISY IMAGE POINTS (BASED ON THE TRUE VALUES) AND CALCULATE # THE ERRORS WE THEN OBTAIN AN AVERAGE FOR EACH ONE for j in range(self.ValidationIters): new_imagePoints_noisy = cam.addnoise_imagePoints( new_imagePoints, mean=0, sd=self.ImageNoise) #Calculate the pose using IPPE (solution with least repro error) normalizedimagePoints = cam.get_normalized_pixel_coordinates( new_imagePoints_noisy) ippe_tvec1, ippe_rmat1, ippe_tvec2, ippe_rmat2 = pose_ippe_both( new_objectPoints, normalizedimagePoints, debug=False) ippeCam1 = cam.clone_withPose(ippe_tvec1, ippe_rmat1) #Calculate the pose using solvepnp EPNP debug = False epnp_tvec, epnp_rmat = pose_pnp(new_objectPoints, new_imagePoints_noisy, cam.K, debug, cv2.SOLVEPNP_EPNP, False) epnpCam = cam.clone_withPose(epnp_tvec, epnp_rmat) #Calculate the pose using solvepnp ITERATIVE pnp_tvec, pnp_rmat = pose_pnp(new_objectPoints, new_imagePoints_noisy, cam.K, debug, cv2.SOLVEPNP_ITERATIVE, False) pnpCam = cam.clone_withPose(pnp_tvec, pnp_rmat) #Calculate errors ippe_tvec_error1, ippe_rmat_error1 = ef.calc_estimated_pose_error( cam.get_tvec(), cam.R, ippeCam1.get_tvec(), ippe_rmat1) ippe_tvec_error_loop.append(ippe_tvec_error1) ippe_rmat_error_loop.append(ippe_rmat_error1) epnp_tvec_error, epnp_rmat_error = ef.calc_estimated_pose_error( cam.get_tvec(), cam.R, epnpCam.get_tvec(), epnp_rmat) epnp_tvec_error_loop.append(epnp_tvec_error) epnp_rmat_error_loop.append(epnp_rmat_error) pnp_tvec_error, pnp_rmat_error = ef.calc_estimated_pose_error( cam.get_tvec(), cam.R, pnpCam.get_tvec(), pnp_rmat) pnp_tvec_error_loop.append(pnp_tvec_error) pnp_rmat_error_loop.append(pnp_rmat_error) #Homography Estimation from noisy image points #DLT TRANSFORM Xo = new_objectPoints[[0, 1, 3], :] Xi = new_imagePoints_noisy Hnoisy_dlt, _, _ = homo2d.homography2d(Xo, Xi) Hnoisy_dlt = Hnoisy_dlt / Hnoisy_dlt[2, 2] #HO METHOD Xo = new_objectPoints[[0, 1, 3], :] Xi = new_imagePoints_noisy Hnoisy_HO = hh(Xo, Xi) #OpenCV METHOD Xo = new_objectPoints[[0, 1, 3], :] Xi = new_imagePoints_noisy Hnoisy_OpenCV, _ = cv2.findHomography(Xo[:2].T.reshape(1, -1, 2), Xi[:2].T.reshape(1, -1, 2)) ## ERRORS FOR THE DLT HOMOGRAPHY ## VALIDATION OBJECT POINTS validation_objectPoints = validation_plane.get_points() validation_imagePoints = np.array( cam.project(validation_objectPoints, False)) Xo = np.copy(validation_objectPoints) Xo = np.delete(Xo, 2, axis=0) Xi = np.copy(validation_imagePoints) homo_dlt_error_loop.append( ef.validation_points_error(Xi, Xo, Hnoisy_dlt)) ## ERRORS FOR THE HO HOMOGRAPHY ## VALIDATION OBJECT POINTS validation_objectPoints = validation_plane.get_points() validation_imagePoints = np.array( cam.project(validation_objectPoints, False)) Xo = np.copy(validation_objectPoints) Xo = np.delete(Xo, 2, axis=0) Xi = np.copy(validation_imagePoints) homo_HO_error_loop.append( ef.validation_points_error(Xi, Xo, Hnoisy_HO)) ## ERRORS FOR THE OpenCV HOMOGRAPHY ## VALIDATION OBJECT POINTS validation_objectPoints = validation_plane.get_points() validation_imagePoints = np.array( cam.project(validation_objectPoints, False)) Xo = np.copy(validation_objectPoints) Xo = np.delete(Xo, 2, axis=0) Xi = np.copy(validation_imagePoints) homo_CV_error_loop.append( ef.validation_points_error(Xi, Xo, Hnoisy_OpenCV)) self.Homo_DLT_mean.append(np.mean(homo_dlt_error_loop)) self.Homo_HO_mean.append(np.mean(homo_HO_error_loop)) self.Homo_CV_mean.append(np.mean(homo_CV_error_loop)) self.ippe_tvec_error_mean.append(np.mean(ippe_tvec_error_loop)) self.ippe_rmat_error_mean.append(np.mean(ippe_rmat_error_loop)) self.epnp_tvec_error_mean.append(np.mean(epnp_tvec_error_loop)) self.epnp_rmat_error_mean.append(np.mean(epnp_rmat_error_loop)) self.pnp_tvec_error_mean.append(np.mean(pnp_tvec_error_loop)) self.pnp_rmat_error_mean.append(np.mean(pnp_rmat_error_loop)) self.Homo_DLT_std.append(np.std(homo_dlt_error_loop)) self.Homo_HO_std.append(np.std(homo_HO_error_loop)) self.Homo_CV_std.append(np.std(homo_CV_error_loop)) self.ippe_tvec_error_std.append(np.std(ippe_tvec_error_loop)) self.ippe_rmat_error_std.append(np.std(ippe_rmat_error_loop)) self.epnp_tvec_error_std.append(np.std(epnp_tvec_error_loop)) self.epnp_rmat_error_std.append(np.std(epnp_rmat_error_loop)) self.pnp_tvec_error_std.append(np.std(pnp_tvec_error_loop)) self.pnp_rmat_error_std.append(np.std(pnp_rmat_error_loop))
#cam.look_at([0,0,0]) #cam.set_R_axisAngle(1.0, 0.0, 0.0, np.deg2rad(110.0)) #cam.set_t(0.0,-0.3,0.1, frame='world') ## Define a Display plane pl = Plane(origin=np.array([0, 0, 0]), normal=np.array([0, 0, 1]), size=(0.3, 0.3), n=(2, 2)) pl = CircularPlane() pl.random(n=number_of_points, r=0.01, min_sep=0.01) objectPoints = pl.get_points() x1, y1, x2, y2, x3, y3, x4, y4 = gd.extract_objectpoints_vars(objectPoints) imagePoints_true = np.array(cam.project(objectPoints, False)) imagePoints_measured = cam.addnoise_imagePoints(imagePoints_true, mean=0, sd=4) repro = gd.repro_error_autograd(x1, y1, x2, y2, x3, y3, x4, y4, cam.P, imagePoints_measured) #%% ## CREATE A SET OF IMAGE POINTS FOR VALIDATION OF THE HOMOGRAPHY ESTIMATION validation_plane = Plane(origin=np.array([0, 0, 0]), normal=np.array([0, 0, 1]), size=(0.3, 0.3), n=(4, 4)) validation_plane.uniform() ## we create the gradient for the point distribution normalize = False