單目視覺三維重建
1. 單目視覺三維重建簡介
2. 代碼實現
(1)標定攝像機獲得攝像機矩陣K(內參數矩陣)
import cv2
import numpy as np
import glob
################################################################################
print 'criteria and object points set'
# termination criteria
criteria = (3L, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(8,5,0)
objpoint = np.zeros((9 * 6, 3), np.float32)
objpoint[:,:2] = np.mgrid[0:9, 0:6].T.reshape(-1,2)
# arrays to store object points and image points from all the images
# 3d point in real world space
objpoints = []
# 2d points in image plane
imgpoints = []
################################################################################
print 'Load Images'
images = glob.glob('images/Phone Camera/*.bmp')
for frame in images:
img = cv2.imread(frame)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# find chess board corners
ret, corners = cv2.findChessboardCorners(imgGray, (9,6), None)
# print ret to check if pattern size is set correctly
print ret
# if found, add object points, image points (after refining them)
if ret == True:
# add object points
objpoints.append(objpoint)
cv2.cornerSubPix(imgGray, corners, (11,11), (-1,-1), criteria)
# add corners as image points
imgpoints.append(corners)
# draw corners
cv2.drawChessboardCorners(img, (9,6), corners, ret)
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
################################################################################
print 'camera matrix'
ret, camMat, distortCoffs, rotVects, transVects = cv2.calibrateCamera(objpoints, imgpoints, imgGray.shape[::-1],None,None)
################################################################################
print 're-projection error'
meanError = 0
for i in xrange(len(objpoints)):
imgpoints2, _ = cv2.projectPoints(objpoints[i], rotVects[i], transVects[i], camMat, distortCoffs)
error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2)
meanError += error
print "total error: ", meanError / len(objpoints)
################################################################################
def drawAxis(img, corners, imgpoints):
corner = tuple(corners[0].ravel())
cv2.line(img, corner, tuple(imgpoints[0].ravel()), (255,0,0), 5)
cv2.line(img, corner, tuple(imgpoints[1].ravel()), (0,255,0), 5)
cv2.line(img, corner, tuple(imgpoints[2].ravel()), (0,0,255), 5)
return img
################################################################################
def drawCube(img, corners, imgpoints):
imgpoints = np.int32(imgpoints).reshape(-1,2)
# draw ground floor in green color
cv2.drawContours(img, [imgpoints[:4]], -1, (0,255,0), -3)
# draw pillars in blue color
for i,j in zip(range(4), range(4,8)):
cv2.line(img, tuple(imgpoints[i]), tuple(imgpoints[j]), (255,0,0), 3)
# draw top layer in red color
cv2.drawContours(img, [imgpoints[4:]], -1, (0,0,255), 3)
return img
################################################################################
print 'pose calculation'
axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)
axisCube = 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 frame in glob.glob('images/Phone Camera/*.bmp'):
img = cv2.imread(frame)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (9,6), None)
if ret == True:
# find the rotation and translation vectors.
rotVects, transVects, inliers = cv2.solvePnPRansac(objpoint, corners, camMat, distortCoffs)
# project 3D points to image plane
'''
imgpoints, jac = cv2.projectPoints(axis, rotVecs, transVecs, camMat, distortCoffs)
img = drawAxis(img, corners, imgpoints)
'''
imgpoints, jac = cv2.projectPoints(axisCube, rotVects, transVects, camMat, distortCoffs)
img = drawCube(img, corners, imgpoints)
cv2.imshow('Image with Pose', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
標定結果如下。左圖出現鋸齒狀折線說明找到模式,右圖爲多個座標軸的組成的正方體的可視化。每個正方體的邊長爲3,測得黑格和白格的邊長即可獲得物體的實際長度,所以用在視覺測量方面很方便,即測量圖像中物體的實際長度。
(2)SIFT特徵點匹配
<pre name="code" class="python">################################################################################
print 'SIFT Keypoints and Descriptors'
sift = cv2.SIFT()
keypoint1, descriptor1 = sift.detectAndCompute(img1, None)
keypoint2, descriptor2 = sift.detectAndCompute(img2, None)
################################################################################
print 'SIFT Points Match'
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
# flann = cv2.FlannBasedMatcher(index_params, search_params)
bf = cv2.BFMatcher()
matches = bf.knnMatch(descriptor1, descriptor2, k = 2)
################################################################################
good = []
points1 = []
points2 = []
################################################################################
for i, (m, n) in enumerate(matches):
if m.distance < 0.7 * n.distance:
good.append(m)
points1.append(keypoint1[m.queryIdx].pt)
points2.append(keypoint2[m.trainIdx].pt)
points1 = np.float32(points1)
points2 = np.float32(points2)
F, mask = cv2.findFundamentalMat(points1, points2, cv2.RANSAC)
# We select only inlier points
points1 = points1[mask.ravel() == 1]
points2 = points2[mask.ravel() == 1]
(3)已知內參數矩陣,計算基礎矩陣F和本徵矩陣E
基礎矩陣適用於未標定的攝像頭,假設空間點在兩個物理成像平面中的座標分別爲p = (u, v)和p' = (u', v'),則滿足transpose(p) *F*p = 0,*表示矩陣乘法。根據基礎矩陣的定義F = inverse(transpose(K)) * E * inverse(K)計算出本徵矩陣E。
################################################################################
# camera matrix from calibration
K = np.array([[517.67386649, 0.0, 268.65952163], [0.0, 519.75461699, 215.58959128], [0.0, 0.0, 1.0]])
# essential matrix
E = K.T * F * K
(4)根據本徵矩陣的旋轉和平移分量構造投影矩陣對P和P’
W = np.array([[0., -1., 0.], [1., 0., 0.], [0., 0., 1.]])
U, S, V = np.linalg.svd(E)
# rotation matrix
R = U * W * V
# translation vector
t = [U[0][2], U[1][2], U[2][2]]
checkValidRot(R)
P1 = [[R[0][0], R[0][1], R[0][2], t[0]], [R[1][0], R[1][1], R[1][2], t[1]], [R[2][0], R[2][1], R[2][2], t[2]]]
P = [[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.]]
(5)有效特徵點三角化實現重建
################################################################################
print 'points triangulation'
u = []
u1 = []
Kinv = np.linalg.inv(K)
# convert points in gray image plane to homogeneous coordinates
for idx in range(len(points1)):
t = np.dot(Kinv, np.array([points1[idx][0], points1[idx][1], 1.]))
t1 = np.dot(Kinv, np.array([points2[idx][0], points2[idx][1], 1.]))
u.append(t)
u1.append(t1)
################################################################################
# re-projection error
reprojError = 0
# point cloud (X,Y,Z)
pointCloudX = []
pointCloudY = []
pointCloudZ = []
for idx in range(len(points1)):
X = linearLSTriangulation(u[idx], P, u1[idx], P1)
pointCloudX.append(X[0])
pointCloudY.append(X[1])
pointCloudZ.append(X[2])
temp = np.zeros(4, np.float32)
temp[0] = X[0]
temp[1] = X[1]
temp[2] = X[2]
temp[3] = 1.0
print temp
# calculate re-projection error
reprojPoint = np.dot(np.dot(K, P1), temp)
imgPoint = np.array([points1[idx][0], points1[idx][1], 1.])
reprojError += math.sqrt((reprojPoint[0] / reprojPoint[2] - imgPoint[0]) * (reprojPoint[0] / reprojPoint[2] - imgPoint[0]) + (reprojPoint[1] / reprojPoint[2] - imgPoint[1]) * (reprojPoint[1] / reprojPoint[2] - imgPoint[1]))
print 'Re-project Error:', reprojError / len(points1)
繪製空間點的在三維空間中的位置,但沒有根據黑白格邊長作長度單位的轉換。實驗結果如下圖所示。上面兩幅圖爲步驟(2)和(3)的實驗結果,所有極線的交點爲極點,都在圖像之外。圖像中匹配合格的特徵點有相同的編號。根據編號的相對位置關係判斷重建可以是否合理。