圆形非对称靶标相机标定代码
时间: 2023-12-05 15:05:48 浏览: 264
相机标定代码
以下是圆形非对称靶标相机标定的Python代码,代码中有详细的注释说明:
```python
import numpy as np
import cv2
# 定义圆形非对称靶标的行列数和每个小格子的大小
rows = 6
cols = 9
square_size = 20 # 单位为毫米
# 生成标定板角点的世界坐标系坐标
objp = np.zeros((rows * cols, 3), np.float32)
objp[:, :2] = np.mgrid[0:cols, 0:rows].T.reshape(-1, 2) * square_size
# 存储所有的角点坐标
objpoints = [] # 世界坐标系中的坐标
imgpoints = [] # 图像坐标系中的坐标
# 读取所有标定图片并提取角点坐标
images = glob.glob('calibration_images/*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 查找角点
ret, corners = cv2.findCirclesGrid(gray, (cols, rows), flags=cv2.CALIB_CB_ASYMMETRIC_GRID)
# 如果找到了角点,则添加到objpoints和imgpoints中
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
# 在图像上绘制角点
cv2.drawChessboardCorners(img, (cols, rows), corners, ret)
cv2.imshow('img', img)
cv2.waitKey(500)
cv2.destroyAllWindows()
# 进行相机标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# 打印标定结果
print("相机内参矩阵:\n", mtx)
print("畸变系数:\n", dist)
# 校正图像
img = cv2.imread('calibration_images/test_image.jpg')
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# 显示校正前后的图像
cv2.imshow('original', img)
cv2.imshow('corrected', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文