opencv中的calibhon3d 在python opencv上如何表现
时间: 2023-11-07 12:00:34 浏览: 41
在Python的OpenCV中,calib3d模块中的calibrateCamera函数用于相机标定。以下是使用calibrateCamera函数进行相机标定的示例代码:
```python
import cv2
import numpy as np
# 准备物体坐标
objp = np.zeros((7*5, 3), np.float32)
objp[:,:2] = np.mgrid[0:7, 0:5].T.reshape(-1, 2)
# 存储所有图像的物体点和图像点
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
# 加载图像
images = glob.glob('*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 寻找棋盘格角点
ret, corners = cv2.findChessboardCorners(gray, (7,5), None)
# 如果找到棋盘格角点
if ret == True:
objpoints.append(objp)
# 亚像素级别角点检测
corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners2)
# 在棋盘格上绘制角点
img = cv2.drawChessboardCorners(img, (7,5), corners2, ret)
# 进行相机标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# 打印相机内参和畸变系数
print("Camera matrix:", mtx)
print("Distortion coefficients:", dist)
```
在以上代码中,objp表示棋盘格上的物体坐标,objpoints和imgpoints分别存储所有图像的物体点和图像点。调用calibrateCamera函数进行相机标定,并输出相机内参和畸变系数。