-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposeEstimation.py
72 lines (46 loc) · 2.18 KB
/
poseEstimation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import cv2 as cv
import glob
chessboardsize = (6,8)
# Load previously saved data
with np.load('matrix\params.npz') as file:
mtx, dist, rvecs, tvecs = [file[i] for i in ('mtx','dist','rvecs','tvecs')]
def draw(img, corners, imgpts):
corner = tuple(corners[0].ravel().astype(int))
img = cv.line(img, corner, tuple(imgpts[0].ravel().astype(int)), (255,0,0), 5) # x
img = cv.line(img, corner, tuple(imgpts[1].ravel().astype(int)), (0,255,0), 5) # y
img = cv.line(img, corner, tuple(imgpts[2].ravel().astype(int)), (0,0,255), 5) # z
return img
def drawBoxes(img, corners, imgpts):
imgpts = np.int32(imgpts).reshape(-1,2)
# draw ground floor in green
img = cv.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 = cv.line(img, tuple(imgpts[i]), tuple(imgpts[j]),(255),3)
# draw top layer in red color
img = cv.drawContours(img, [imgpts[4:]],-1,(0,0,255),3)
return img
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((chessboardsize[0]*chessboardsize[1],3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardsize[0],0:chessboardsize[1]].T.reshape(-1,2)
axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)
axisBoxes = 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('input\DJI_0112.jpg'):
print(fname)
img = cv.imread(fname)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret, corners = cv.findChessboardCorners(gray, chessboardsize,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)
cv.imshow('img',img)
k = cv.waitKey(0) & 0xFF
if k == ord('s'):
cv.imwrite('pose'+fname, img)
cv.destroyAllWindows()