opencv相机标定图片路径代码
时间: 2023-07-07 12:46:17 浏览: 112
以下是使用Python和OpenCV进行相机标定的示例代码,其中`images`是包含标定图像的文件夹路径,`board_size`是标定板的大小(行和列),`square_size`是标定板上每个方格的尺寸(单位为毫米):
```python
import glob
import cv2
import numpy as np
# 标定板大小
board_size = (9, 6)
# 标定板上每个方格的尺寸(单位为毫米)
square_size = 25
# 准备标定点的对象点坐标(0, 0, 0), (1, 0, 0), (2, 0, 0) ... (8, 5, 0)
object_points = np.zeros((np.prod(board_size), 3), dtype=np.float32)
object_points[:, :2] = np.indices(board_size).T.reshape(-1, 2)
object_points *= square_size
# 存储标定图像的路径
images = glob.glob('path/to/images/*.jpg')
# 存储标定点的图像点坐标
image_points = []
for image in images:
# 读取图像
img = cv2.imread(image)
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 查找标定板角点
ret, corners = cv2.findChessboardCorners(gray, board_size, None)
if ret:
# 如果找到了所有角点,则绘制它们
cv2.drawChessboardCorners(img, board_size, corners, ret)
# 优化角点坐标
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
# 存储图像点坐标
image_points.append(corners2)
# 显示图像并等待按键
cv2.imshow('image', img)
cv2.waitKey(500)
# 进行相机标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera([object_points] * len(image_points), image_points, gray.shape[::-1], None, None)
# 打印相机内参和畸变参数
print('相机内参:\n', mtx)
print('畸变参数:\n', dist)
```
其中,`cv2.findChessboardCorners`函数用于查找标定板的角点,`cv2.cornerSubPix`函数用于优化角点坐标,`cv2.calibrateCamera`函数用于进行相机标定。
阅读全文