利用svg棋盘图像计算相机标定参数
利用棋盘网格,生成相机下各个位姿的图像
相机标定的基本步骤
1.打开标定图像 -> 2.定位棋盘角点 ->3.保存角点三维坐标和对应的图像二维坐标 ->4.计算相机标定参数
import numpy as np
import cv2
import glob
# 亚像素角点定位参数
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# 准备三维点坐标, 类似(0,0,0), (1,0,0), (2,0,0) ....,(6,5,0), Z坐标保持为0
#temp_obj_pts = np.zeros((6 * 5, 3), np.float32)
#temp_obj_pts[:, :2] = np.mgrid[0:5, 0:6].T.reshape(-1, 2)
temp_obj_pts = np.array([[x, y, 0] for y in range(6) for x in range(5)], dtype=np.float32)
obj_points = [] # 三维空间点数组
img_points = [] # 二维图像坐标数组.
image_file_names = glob.glob('snap_images/*.jpg')
for file_name in image_file_names:
img = cv2.imread(file_name)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 寻找棋盘格角点, 注意是内角点, 角点个数输入错误将返回错误
ret, corners = cv2.findChessboardCorners(gray, (5, 6), None)
# 如果成功找到角点, 进行亚像素级修正
if ret:
obj_points.append(temp_obj_pts)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1,-1), criteria)
img_points.append(corners2)
# 绘制和显示角点
cv2.drawChessboardCorners(img, (6, 5), corners2, ret)
cv2.imshow('calibrate', img)
cv2.waitKey(500)
cv2.destroyAllWindows()
# 计算相机标定参数
ret, mtx, dist, rvecs, tvecs = \
cv2.calibrateCamera(obj_points, img_points, gray.shape[::-1], None, None)
# 输出标定参数
if ret:
print("------mtx------")
print(mtx)
np.savetxt('camera_mtx.txt', mtx)
print("------dist------")
print(dist)
np.savetxt('camera_dist.txt', dist)