python圆点标定与棋盘格标定代码
时间: 2024-01-20 14:17:00 浏览: 157
以下是使用OpenCV进行圆点标定和棋盘格标定的Python代码示例:
1. 圆点标定代码示例:
```python
import cv2
import numpy as np
# 读取图像
image = cv2.imread('image.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 使用Hough变换检测圆点
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1, minDist=100, param1=50, param2=30, minRadius=10, maxRadius=50)
# 绘制检测到的圆点
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(image, (x, y), r, (0, 255, 0), 4)
# 显示结果
cv2.imshow("Circle Detection", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. 棋盘格标定代码示例:
```python
import cv2
import numpy as np
# 设置棋盘格的大小
pattern_size = (9, 6)
# 创建棋盘格角点的坐标
obj_points = np.zeros((np.prod(pattern_size), 3), dtype=np.float32)
obj_points[:, :2] = np.mgrid[0:pattern_size[0], 0:pattern_size[1]].T.reshape(-1, 2)
# 存储棋盘格角点的坐标和图像中检测到的角点的坐标
obj_points_list = [] # 世界坐标系中的角点坐标
img_points_list = [] # 图像坐标系中的角点坐标
# 读取图像
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 寻找棋盘格角点
ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)
# 如果找到了棋盘格角点
if ret:
obj_points_list.append(obj_points)
img_points_list.append(corners)
# 绘制棋盘格角点
cv2.drawChessboardCorners(image, pattern_size, corners, ret)
# 进行相机标定
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points_list, img_points_list, gray.shape[::-1], None, None)
# 打印相机内参和畸变系数
print("相机内参:")
print(mtx)
print("畸变系数:")
print(dist)
# 显示结果
cv2.imshow("Chessboard Detection", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文