import numpy as np
h = 1.5 # 相机离地面1.5m高
pitch = -0.023797440420123328 # 弧度
pixe_x, pixe_y = 888, 700 # 图像像素点,接地点
CameraMat = np.array([[1008, 0, 945],
[0, 1009, 537],
[0, 0, 1]]) # 相机内参
R = np.array([[-0.0330564609, 0.0238237337, 0.999169505],
[0.999452124, -0.000862625046, 0.0330863791, ],
[0.00165014972, 0.999715802, -0.0237821659]]) # 旋转矩阵
T = np.array([0, 0, -1.5])
sigma = np.arctan((pixe_y - CameraMat[1][2]) / CameraMat[1][1])
z = h * np.cos(sigma) / np.sin(sigma + pitch) # 深度
x_pixe, y_pixe = 2 * CameraMat[0][2] - pixe_x, 2 * CameraMat[1][2] - pixe_y # 根据自定坐标系选择是否中心对称转换
camera_x = z * (x_pixe / CameraMat[0][0] - CameraMat[0][2] / CameraMat[0][0])
camera_y = z * (y_pixe / CameraMat[1][1] - CameraMat[1][2] / CameraMat[1][1])
camera_z = z
distance_machine_direction = R[0][0] * camera_x + R[0][1] * camera_y + R[0][2] * camera_z + T[0] # 纵向距离
distance_transverse_direction = R[1][0] * camera_x + R[1][1] * camera_y + R[1][2] * camera_z + T[1] # 横向距离
print(distance_machine_direction, distance_transverse_direction)
python 从旋转矩阵转化到角度、从角度到转化矩阵,主要用到 scipy 库中的 Rotation。
import numpy as np
from scipy.spatial.transform import Rotation
r = np.array([-0.0517, -0.0611, 0.9968, 0.9987, 0.0011, 0.0519, -0.0042, 0.9981, 0.0609]).reshape(3, 3)
euler_r = Rotation.from_matrix(r).as_euler('zxy', degrees=False) # zxy 是 外旋顺序。degrees False 显示弧度,True 显示角度
print(euler_r)
# [ 1.56967277 -0.0518037 1.50976086]
from scipy.spatial.transform import Rotation
euler_r = [1.56967277, -0.0518037, 1.50976086]
new_r = Rotation.from_euler("zxy", [euler_r[0], euler_r[1], euler_r[2]], degrees=False).as_matrix()
import numpy as np
from scipy.spatial.transform import Rotation
def get_r_matrix(str, alpha):
sin = -np.sin(alpha)
cos = np.cos(alpha)
res = np.eye(3)
if str == "z":
res = np.array([[cos, sin, 0],
[-sin, cos, 0],
[0, 0, 1]])
elif str == "y":
res = np.array([[cos, 0, -sin],
[0, 1, 0],
[sin, 0, cos]])
elif str == "x":
res = np.array([[1, 0, 0],
[0, cos, sin],
[0, -sin, cos]])
return res
euler_r = [1.56967277, -0.0518037, 1.50976086]
a, b, c = euler_r[0], euler_r[1], euler_r[2]
z = get_r_matrix("z", a)
x = get_r_matrix("x", b)
y = get_r_matrix("y", c)
mtx = y @ x @ z
mtx_1 = Rotation.from_euler("zxy", [a, b, c], degrees=False).as_matrix()
print(mtx, mtx_1) # 结果完全一致
综上所述,可得
import numpy as np
from scipy.spatial.transform import Rotation
diff_pitch = -0.01 # 假设当前地面坡度为 -0.01 弧度
h = 1.5 # 相机离地面1.5m高
pitch = -0.023797440420123328 # 弧度
pitch = pitch + diff_pitch
pixe_x, pixe_y = 888, 700 # 图像像素点,接地点
CameraMat = np.array([[1008, 0, 945],
[0, 1009, 537],
[0, 0, 1]]) # 相机内参
original_r = np.array([[-0.0330564609, 0.0238237337, 0.999169505],
[0.999452124, -0.000862625046, 0.0330863791],
[0.00165014972, 0.999715802, -0.0237821659]]) # 旋转矩阵
euler_r = Rotation.from_matrix(original_r).as_euler('zxy', degrees=False)
R = Rotation.from_euler("zxy", [euler_r[0], euler_r[1], euler_r[2] + diff_pitch], degrees=False).as_matrix()
T = np.array([0, 0, -1.5]) # 平移矩阵
sigma = np.arctan((pixe_y - CameraMat[1][2]) / CameraMat[1][1])
z = h * np.cos(sigma) / np.sin(sigma + pitch) # 深度
x_pixe, y_pixe = 2 * CameraMat[0][2] - pixe_x, 2 * CameraMat[1][2] - pixe_y # 根据自定坐标系选择是否中心对称转换
camera_x = z * (x_pixe / CameraMat[0][0] - CameraMat[0][2] / CameraMat[0][0])
camera_y = z * (y_pixe / CameraMat[1][1] - CameraMat[1][2] / CameraMat[1][1])
camera_z = z
distance_machine_direction = R[0][0] * camera_x + R[0][1] * camera_y + R[0][2] * camera_z + T[0] # 纵向距离
distance_transverse_direction = R[1][0] * camera_x + R[1][1] * camera_y + R[1][2] * camera_z + T[1] # 横向距离
print(distance_machine_direction, distance_transverse_direction)
知道了 2.2.3 中的三维旋转原理,那我们利用矩阵乘法就可以轻松获得新外参啦
double pitchDiff = -0.01;
cv::Mat initR = (cv::Mat_<double>(3,3) << -0.0330564609, 0.0238237337, 0.999169505,
0.999452124, -0.000862625046, 0.0330863791,
0.00165014972, 0.999715802, -0.0237821659); // 相机初始外参
cv::Mat pitchR = (cv::Mat_<double>(3, 3) << cos(pitchDiff), 0, sin(pitchDiff), 0, 1, 0, -sin(pitchDiff), 0, cos(pitchDiff));
cv::Mat curR = pitchR * initR;