相机单目内参标定,python代码
时间: 2023-07-22 07:57:00 浏览: 104
以下是使用Python进行相机单目内参标定的示例代码:
```python
import numpy as np
import cv2
# 读取棋盘格图像
image = cv2.imread('chessboard.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 设置棋盘格尺寸
pattern_size = (9, 6) # 棋盘格每行、每列的角点数
# 查找棋盘格角点
found, corners = cv2.findChessboardCorners(gray, pattern_size, None)
if found:
# 提取亚像素角点
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
corners = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
# 绘制角点
cv2.drawChessboardCorners(image, pattern_size, corners, found)
# 执行相机标定
object_points = np.zeros((np.prod(pattern_size), 3), dtype=np.float32)
object_points[:, :2] = np.mgrid[0:pattern_size[0], 0:pattern_size[1]].T.reshape(-1, 2)
image_points = corners.reshape(-1, 2)
_, camera_matrix, dist_coeffs, _, _ = cv2.calibrateCamera([object_points], [image_points], gray.shape[::-1], None, None)
# 打印标定结果
print("相机矩阵:")
print(camera_matrix)
print("\n畸变系数:")
print(dist_coeffs)
# 矫正图像
undistorted_image = cv2.undistort(image, camera_matrix, dist_coeffs)
# 显示原始图像和矫正后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Undistorted Image', undistorted_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("未找到棋盘格角点")
```
请确保已经安装了OpenCV库,并将棋盘格图像命名为'chessboard.jpg'并放置在与代码文件相同的目录中。运行代码后,将显示原始图像和矫正后的图像,并打印相机矩阵和畸变系数。
请注意,此代码仅适用于具有棋盘格模式的相机标定。如果您使用其他标定模式,可能需要调整代码以适应不同的情况。
阅读全文