Skip to content

Commit

Permalink
Merge pull request #1505 from s-trinh/fix_Blender_script_cam_pose
Browse files Browse the repository at this point in the history
Fix Blender scripts to print camera frame as in computer vision convention
  • Loading branch information
fspindle authored Nov 19, 2024
2 parents 8206553 + ae330f6 commit 98270b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 12 additions & 8 deletions script/Blender/Keypoints_extraction/render_still_image_Suzanne.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
np.savetxt(output_pose_filepath, c_T_o)

0 comments on commit 98270b9

Please sign in to comment.