import cv2
import numpy as np
import glob
# Load previously saved data
with np.load('/tmp/xx_B.npz') as X:
mtx, dist, _, _ = [X[i] for i in ('mtx','dist','rvecs','tvecs')]
现在创建一个名为draw
的函数,接收通过cv2.findChessboardCorners()
获取的棋盘格角点和轴点,用于绘制3D坐标轴。
def draw(img, corners, imgpts):
# corner = tuple(corners[0].ravel())
corner = tuple(np.int32(corners[0].ravel()))
imgpts = np.int32(imgpts).reshape(-1, 2) # 确保形状为 (N,2)
img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5)
img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5)
img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5)
return img
与之前案例类似,创建终止条件、目标点(棋盘格角点的3D坐标)和轴点。 轴点是3D空间中用于绘制坐标轴的参考点。 以棋盘格方格尺寸为单位绘制长度为3的坐标轴(因标定基于该尺寸)。因此:
- X轴:从 (0,0,0) 到 (3,0,0)
- Y轴:从 (0,0,0) 到 (0,3,0)
- Z轴:从 (0,0,0) 到 (0,0,-3)(负号表示朝向相机方向绘制)。
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)
现在,像往常一样加载每张图像。搜索7x6网格。
如果找到,则用亚角像素细化。然后使用 cv2.solvePnPRansac()
函数计算旋转和平移。
获得变换矩阵后,将其用于将轴点投影到图像平面。
简单来说,找到3D空间中 (3,0,0)、(0,3,0)、(0,0,3) 对应的图像平面上的点。
获取后,用 draw()
函数从第一个角点向这些点画线。完成!
%matplotlib inline
import matplotlib.pyplot as plt
import os
os.chdir('/data/cvdata/')
cv = cv2
for fname in glob.glob('left*.jpg'):
img = cv.imread(fname)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret, corners = cv.findChessboardCorners(gray, (7,6),None)
if ret == True:
corners2 = cv.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# Find the rotation and translation vectors.
ret,rvecs, tvecs = cv.solvePnP(objp, corners2, mtx, dist)
# project 3D points to image plane
imgpts, jac = cv.projectPoints(axis, rvecs, tvecs, mtx, dist)
img = draw(img,corners2,imgpts)
plt.imshow(img)
# cv.imshow('img',img)
# k = cv.waitKey(0) & 0xFF
# if k == ord('s'):
# cv.imwrite(fname[:6]+'.png', img)
# cv.destroyAllWindows()
for fname in glob.glob('left*.jpg'):
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
if ret == True:
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# 处理不同OpenCV版本的返回值
result = cv2.solvePnPRansac(objp, corners2, mtx, dist)
# OpenCV 4.x+ 返回格式 (success, rvec, tvec, inliers)
if len(result) == 4:
success, rvecs, tvecs, inliers = result
# OpenCV 3.x 返回格式 (rvec, tvec, inliers)
elif len(result) == 3:
rvecs, tvecs, inliers = result
# 某些版本可能只返回 (rvec, tvec)
else:
rvecs, tvecs = result
inliers = None
# project 3D points to image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
img = draw(img, corners2, imgpts)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 修正plt.show的用法
plt.show()
# cv2.imshow('img',img)
# k = cv2.waitKey(0) & 0xff
# if k == 's':
# cv2.imwrite(fname[:6]+'.png', img)
# cv2.destroyAllWindows()
def draw(img, corners, imgpts):
imgpts = np.int32(imgpts).reshape(-1,2)
# draw ground floor in green
img = cv2.drawContours(img, [imgpts[:4]],-1,(0,255,0),-3)
# draw pillars in blue color
for i,j in zip(range(4),range(4,8)):
img = cv2.line(img, tuple(imgpts[i]), tuple(imgpts[j]),(255),3)
# draw top layer in red color
img = cv2.drawContours(img, [imgpts[4:]],-1,(0,0,255),3)
return img
修改后的轴点。它们是3D空间中立方体的8个角点。
axis = np.float32([[0,0,0], [0,3,0], [3,3,0], [3,0,0],
[0,0,-3],[0,3,-3],[3,3,-3],[3,0,-3] ])
for fname in glob.glob('left*.jpg'):
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
if ret == True:
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# 处理不同OpenCV版本的返回值
result = cv2.solvePnPRansac(objp, corners2, mtx, dist)
# OpenCV 4.x+ 返回格式 (success, rvec, tvec, inliers)
if len(result) == 4:
success, rvecs, tvecs, inliers = result
# OpenCV 3.x 返回格式 (rvec, tvec, inliers)
elif len(result) == 3:
rvecs, tvecs, inliers = result
# 某些版本可能只返回 (rvec, tvec)
else:
rvecs, tvecs = result
inliers = None
# project 3D points to image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
img = draw(img, corners2, imgpts)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 修正plt.show的用法
plt.show()
# cv2.imshow('img',img)
# k = cv2.waitKey(0) & 0xff
# if k == 's':
# cv2.imwrite(fname[:6]+'.png', img)
# cv2.destroyAllWindows()
若对图形学、增强现实等领域感兴趣,可使用 OpenGL 渲染更复杂的图形。