From ae330f6f13ba5b1925f95a04ff9f5f71c2da01a5 Mon Sep 17 00:00:00 2001 From: Souriya Trinh Date: Tue, 12 Nov 2024 21:55:21 +0100 Subject: [PATCH] Fix scripts to print camera frame as in computer vision convention. --- .../extract_sift_sampling_Suzanne.py | 16 +++++++++------ .../render_still_image_Suzanne.py | 20 +++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/script/Blender/Keypoints_extraction/extract_sift_sampling_Suzanne.py b/script/Blender/Keypoints_extraction/extract_sift_sampling_Suzanne.py index 4f67ab41cc..cf1ca23749 100755 --- a/script/Blender/Keypoints_extraction/extract_sift_sampling_Suzanne.py +++ b/script/Blender/Keypoints_extraction/extract_sift_sampling_Suzanne.py @@ -65,9 +65,13 @@ def inv_transfo(w_T_o): return o_T_w def get_camera_pose(cameraName, objectName): - M = np.eye(4) - M[1][1] = -1 - M[2][2] = -1 + # Camera frame in OpenGL: + # X-axis to the right + # Y-axis up + # Z-axis backward + cv_M_gl = np.eye(4) + cv_M_gl[1][1] = -1 + cv_M_gl[2][2] = -1 cam = bpy.data.objects[cameraName] object_pose = bpy.data.objects[objectName].matrix_world @@ -82,13 +86,13 @@ def get_camera_pose(cameraName, objectName): w_T_o = np.array(object_pose_normalized_blender) print(f"object_pose_normalized:\n{object_pose_normalized_blender}") - w_T_c = np.array(cam.matrix_world) + w_T_c = np.array(cam.matrix_world) @ cv_M_gl print(f"w_T_c:\n{w_T_c}") - c_T_w = np.array(cam.matrix_world.inverted()) + c_T_w = cv_M_gl @ np.array(cam.matrix_world.inverted()) print(f"c_T_w:\n{c_T_w}") - c_T_o = M @ c_T_w @ w_T_o + c_T_o = c_T_w @ w_T_o print(f"c_T_o:\n{c_T_o}") return c_T_o diff --git a/script/Blender/Keypoints_extraction/render_still_image_Suzanne.py b/script/Blender/Keypoints_extraction/render_still_image_Suzanne.py index 14ae6fc3b1..644b67bf03 100755 --- a/script/Blender/Keypoints_extraction/render_still_image_Suzanne.py +++ b/script/Blender/Keypoints_extraction/render_still_image_Suzanne.py @@ -63,9 +63,13 @@ def inv_transfo(w_T_o): return o_T_w def get_camera_pose(cameraName, objectName): - M = np.eye(4) - M[1][1] = -1 - M[2][2] = -1 + # Camera frame in OpenGL: + # X-axis to the right + # Y-axis up + # Z-axis backward + cv_M_gl = np.eye(4) + cv_M_gl[1][1] = -1 + cv_M_gl[2][2] = -1 cam = bpy.data.objects[cameraName] object_pose = bpy.data.objects[objectName].matrix_world @@ -80,16 +84,16 @@ def get_camera_pose(cameraName, objectName): w_T_o = np.array(object_pose_normalized_blender) print(f"object_pose_normalized:\n{object_pose_normalized_blender}") - w_T_c = np.array(cam.matrix_world) + w_T_c = np.array(cam.matrix_world) @ cv_M_gl print(f"w_T_c:\n{w_T_c}") - c_T_w = np.array(cam.matrix_world.inverted()) + c_T_w = cv_M_gl @ np.array(cam.matrix_world.inverted()) print(f"c_T_w:\n{c_T_w}") c_T_w2 = inv_transfo(w_T_c) - print(f"c_T_w2:\n{c_T_w2}") + print(f"c_T_w2:\n{c_T_w2}") # c_T_w (using Blender inverted()) and c_T_w2 should be equal - c_T_o = M @ c_T_w @ w_T_o + c_T_o = c_T_w @ w_T_o print(f"c_T_o:\n{c_T_o}") return c_T_o @@ -111,4 +115,4 @@ def get_camera_pose(cameraName, objectName): output_pose_name = "c_T_o.txt" output_pose_filepath = os.path.join(output_dir, output_pose_name) - np.savetxt(output_pose_filepath, c_T_o) \ No newline at end of file + np.savetxt(output_pose_filepath, c_T_o)