编写一段手眼标定的python代码
时间: 2023-11-23 13:07:53 浏览: 97
下面是一段手眼标定的Python代码:
```python
import cv2
import numpy as np
#读取标定板图片
img = cv2.imread('calib.jpg')
#标定板角点个数(行方向、列方向)
nx = 9
ny = 6
#生成目标点坐标
objp = np.zeros((nx*ny,3), np.float32)
objp[:,:2] = np.mgrid[0:nx,0:ny].T.reshape(-1,2)
#存储所有的目标点坐标和图像点坐标
objpoints = [] #目标点坐标
imgpoints = [] #图像点坐标
#将标定板角点检测出来
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (nx,ny), None)
#如果找到了角点,就添加到对应的数组中
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
#进行相机校正
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
#显示标定结果
print("相机矩阵:\n", mtx)
print("畸变系数:\n", dist)
print("旋转向量:\n", rvecs)
print("平移向量:\n", tvecs)
```
阅读全文