重投影 误差可视化python opencv
时间: 2023-07-03 15:21:13 浏览: 314
Python使用OpenCV进行标定.pdf
要可视化重投影误差,可以按照以下步骤进行操作:
1. 读取标定板图像和相机参数
``` python
import cv2
import numpy as np
# 读取标定板图像
img = cv2.imread('calibration.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 读取相机参数
ret = np.load('ret.npy')
mtx = np.load('mtx.npy')
dist = np.load('dist.npy')
rvecs = np.load('rvecs.npy')
tvecs = np.load('tvecs.npy')
```
2. 定义标定板的世界坐标和图像坐标
``` python
# 定义标定板的世界坐标和图像坐标
objp = np.zeros((6*9, 3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
# 存储图像中的标定板角点
imgpoints = []
# 存储标定板的世界坐标系中的三维点
objpoints = [objp]
# 寻找标定板角点
ret, corners = cv2.findChessboardCorners(gray, (9,6), None)
if ret == True:
imgpoints.append(corners)
# 计算旋转和平移向量
retval, rvecs, tvecs = cv2.solvePnP(objpoints, imgpoints, mtx, dist)
```
3. 计算重投影误差
``` python
# 计算重投影误差
mean_error = 0
for i in range(len(objpoints)):
imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2)
mean_error += error
print("total error: ", mean_error/len(objpoints))
```
4. 可视化重投影误差
``` python
# 可视化重投影误差
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))
# 根据新的相机矩阵将图像去畸变
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# 计算重投影点
imgpoints2, _ = cv2.projectPoints(objpoints[0], rvecs[0], tvecs[0], mtx, dist)
# 绘制标定板角点和重投影点
for i in range(len(imgpoints[0])):
img = cv2.circle(img, tuple(imgpoints[0][i]), 5, (0,0,255), -1)
img = cv2.circle(img, tuple(imgpoints2[i][0]), 5, (0,255,0), -1)
# 显示结果
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
运行以上代码,就可以得到可视化的重投影误差结果。注意,这里只绘制了第一个标定板的角点和重投影点,如果需要可视化多个标定板的重投影误差,需要进行相应的修改。
阅读全文