用opencv python代码建立三维坐标轴并附带详细讲解
时间: 2023-12-10 20:38:12 浏览: 492
python opencv教程,以及配套代码
首先,需要导入OpenCV库和NumPy库。
``` python
import cv2
import numpy as np
```
接着,我们需要定义一个函数来绘制三维坐标轴。该函数需要接受四个参数:图像、旋转向量、平移向量和相机矩阵。
``` python
def draw_axis(img, rvec, tvec, camera_matrix):
# 3D 坐标轴的起点
origin = np.zeros((3, 1))
# 3D 坐标轴的终点
axis_points = np.float32([[0.5,0,0], [0,0.5,0], [0,0,0.5]]).reshape(-1,3)
# 投影坐标轴点到图像平面
imgpts, jac = cv2.projectPoints(axis_points, rvec, tvec, camera_matrix, None)
# 绘制坐标轴
img = cv2.line(img, tuple(imgpts[0].ravel()), tuple(imgpts[1].ravel()), (255,0,0), 3) # X 轴 蓝色
img = cv2.line(img, tuple(imgpts[0].ravel()), tuple(imgpts[2].ravel()), (0,255,0), 3) # Y 轴 绿色
img = cv2.line(img, tuple(imgpts[0].ravel()), tuple(imgpts[3].ravel()), (0,0,255), 3) # Z 轴 红色
return img
```
其中,`projectPoints` 函数用于将三维坐标点投影到图像平面上,返回的是二维坐标点。`cv2.line` 函数用于绘制直线,参数包括起点、终点、颜色和线宽。
接下来,我们可以读取一张图片,并且使用OpenCV的标定函数来获取相机矩阵、畸变系数和旋转向量、平移向量。
``` python
# 读取图片
img = cv2.imread("image.jpg")
# 标定图片中的相机参数
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objpoints = []
imgpoints = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (9,6), None)
if ret == True:
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)
img = cv2.drawChessboardCorners(img, (9,6), corners2,ret)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
```
最后,我们可以调用 `draw_axis` 函数来绘制三维坐标轴。
``` python
# 绘制坐标轴
img = draw_axis(img, rvecs[0], tvecs[0], mtx)
# 显示图片
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
完整代码如下:
``` python
import cv2
import numpy as np
def draw_axis(img, rvec, tvec, camera_matrix):
origin = np.zeros((3, 1))
axis_points = np.float32([[0.5,0,0], [0,0.5,0], [0,0,0.5]]).reshape(-1,3)
imgpts, jac = cv2.projectPoints(axis_points, rvec, tvec, camera_matrix, None)
img = cv2.line(img, tuple(imgpts[0].ravel()), tuple(imgpts[1].ravel()), (255,0,0), 3) # X 轴 蓝色
img = cv2.line(img, tuple(imgpts[0].ravel()), tuple(imgpts[2].ravel()), (0,255,0), 3) # Y 轴 绿色
img = cv2.line(img, tuple(imgpts[0].ravel()), tuple(imgpts[3].ravel()), (0,0,255), 3) # Z 轴 红色
return img
img = cv2.imread("image.jpg")
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objpoints = []
imgpoints = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (9,6), None)
if ret == True:
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)
img = cv2.drawChessboardCorners(img, (9,6), corners2,ret)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
img = draw_axis(img, rvecs[0], tvecs[0], mtx)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这样就可以在图片上绘制出三维坐标轴了。
阅读全文