python 进行外部校准以检索摄像机的偏航、俯仰和滚动

vbkedwbf  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(172)

我目前正在尝试检索我使用的相机的偏航,俯仰和滚动。我首先做了一个内在的校准,以检索的内在矩阵和失真系数。然后,我在墙上放置一个校准目标,确保它处于0°。然而,结果显示-90°的滚动角,而相机几乎处于0°。
下面是我正在使用的代码:

import numpy as np
import cv2

# Load the image
img = cv2.imread('extrinsic_images/image_0.jpg')

original2Display = cv2.resize(img,(960,540))

# Define the size of the calibration target
target_size = (6, 8)  # inner corners of the checkerboard

# Define the 3D coordinates of the corners of the calibration target
square_size = 0.03  # side length of each square in meters
objp = np.zeros((target_size[0] * target_size[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:target_size[0], 0:target_size[1]].T.reshape(-1, 2)
objp *= square_size

# Define the intrinsic parameters of the camera
K = np.array([[3326.45166, 0, 1968.81763], [0, 3298.43162, 1103.86361], [0, 0, 1]])

# Define the distortion coefficients of the camera
dist_coeffs = np.array([-0.37799524,  0.10135872, -0.00174793, -0.00361135,  0.13015989])

# Undistort the image using the intrinsic parameters
img_undistorted = cv2.undistort(img, K, dist_coeffs)

# Convert the image to grayscale
gray = cv2.cvtColor(img_undistorted, cv2.COLOR_BGR2GRAY)

# Find the corners of the calibration target in the image
ret, corners = cv2.findChessboardCorners(gray, target_size, None)

# Refine the corner positions to subpixel accuracy
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)

# Estimate the rotation and translation vectors using solvePnP
ret, rvec, tvec = cv2.solvePnP(objp, corners, K, None)

# Convert the rotation vector to a rotation matrix
R, _ = cv2.Rodrigues(rvec)

# Calculate the Euler angles from the rotation matrix
theta_x = np.arctan2(R[2, 1], R[2, 2])
theta_y = np.arctan2(-R[2, 0], np.sqrt(R[2, 1]**2 + R[2, 2]**2))
theta_z = np.arctan2(R[1, 0], R[0, 0])

# Convert the angles to degrees and print the results
print('Yaw:   {:.2f} deg'.format(np.rad2deg(theta_y)))
print('Pitch: {:.2f} deg'.format(np.rad2deg(theta_x)))
print('Roll:  {:.2f} deg'.format(np.rad2deg(theta_z)))

以下是原始的、未失真的和灰色(显示角)的图像:

有人能帮忙吗?

bihw5rsg

bihw5rsg1#

结果是我犯了个愚蠢的错误。我颠倒了校准目标的行和列。3D对象和检测到的角点之间的坐标不匹配。square_size必须为(columns,rows)。

相关问题