相机参数
# 相机内参
K = [[2637.5783226764374, 0.0, 960.0],
[0.0, 2637.5783226764374, 540.0],
[0.0, 0.0, 1.0]]
K = np.array(K).astype(np.float64)
# 相机位姿以及旋转顺序
camera_loc = [7, -6, 5]
camera_rot = [60, 15, 40]
mode = 'xyz'
旋转矩阵
import numpy as np
from scipy.spatial.transform import Rotation as R
# e 是为了和世界坐标系对齐方向
e = np.eye(3)
e[[1,2],[1,2]] = -1
RT_camera2world = R.from_euler(mode, camera_rot, degrees=True).as_matrix()
RT_camera2world = RT_camera2world @ e
RT_camera2world = np.concatenate((RT_camera2world, np.reshape(camera_loc,[3,1])), axis=1)
RT_camera2world = np.concatenate([RT_camera2world, [[0, 0, 0, 1]]], axis=0)
RT_world2camera = np.linalg.inv(RT_camera2world)
投影
rvec = cv2.Rodrigues(RT_world2camera[:3, :3])[0]
tvec = RT_world2camera[:3, 3]
corner_raw = [
[1, 1, 1],
[1, -1, 1],
[1, 1, -1],
[-1, 1, 1],
]
corner = np.array(corner_raw).astype(np.float64)
corner_xy = cv2.projectPoints(corner, rvec, tvec, K, np.zeros((1, 5)))[0]
corner_xy = corner_xy.reshape((-1, 2)).astype(np.int16)
可视化点