-
Notifications
You must be signed in to change notification settings - Fork 28
/
inspect_epipolar_geometry.py
81 lines (59 loc) · 2.71 KB
/
inspect_epipolar_geometry.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
73
74
75
76
77
78
79
80
import cv2
import numpy as np
import os
import json
def skew(x):
return np.array([[0, -x[2], x[1]],
[x[2], 0, -x[0]],
[-x[1], x[0], 0]])
def two_view_geometry(intrinsics1, extrinsics1, intrinsics2, extrinsics2):
relative_pose = extrinsics2.dot(np.linalg.inv(extrinsics1))
R = relative_pose[:3, :3]
T = relative_pose[:3, 3]
tx = skew(T)
E = np.dot(tx, R)
F = np.linalg.inv(intrinsics2[:3, :3]).T.dot(E).dot(np.linalg.inv(intrinsics1[:3, :3]))
return E, F, relative_pose
def drawpointslines(img1, pts1, img2, lines2, colors):
h, w = img2.shape[:2]
# img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
# img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
print(pts1.shape, lines2.shape, colors.shape)
for p, l, c in zip(pts1, lines2, colors):
c = tuple(c.tolist())
img1 = cv2.circle(img1, tuple(p), 5, c, -1)
x0, y0 = map(int, [0, -l[2]/l[1]])
x1, y1 = map(int, [w, -(l[2]+l[0]*w)/l[1]])
img2 = cv2.line(img2, (x0, y0), (x1, y1), c, 1, lineType=cv2.LINE_AA)
return img1, img2
def inspect(img1, K1, W2C1, img2, K2, W2C2):
E, F, relative_pose = two_view_geometry(K1, W2C1, K2, W2C2)
orb = cv2.ORB_create()
kp1 = orb.detect(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), None)[:20]
pts1 = np.array([[int(kp.pt[0]), int(kp.pt[1])] for kp in kp1])
lines2 = cv2.computeCorrespondEpilines(pts1.reshape(-1,1,2), 1, F)
lines2 = lines2.reshape(-1, 3)
colors = np.random.randint(0, high=255, size=(len(pts1), 3))
img1, img2 = drawpointslines(img1, pts1, img2, lines2, colors)
im_to_show = np.concatenate((img1, img2), axis=1)
# down sample to fit screen
h, w = im_to_show.shape[:2]
im_to_show = cv2.resize(im_to_show, (int(0.5*w), int(0.5*h)), interpolation=cv2.INTER_AREA)
cv2.imwrite('./debug_epipolar.png', im_to_show)
# cv2.imshow('epipolar geometry', im_to_show)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
if __name__ == '__main__':
base_dir = './examples/outputs'
img_dir = os.path.join(base_dir, 'images')
img_name1 = 'JAX_167_003_RGB.png'
img_name2 = 'JAX_167_004_RGB.png'
cam_dict = json.load(open(os.path.join(base_dir, 'cameras_adjusted', img_name1.replace('.png', '.json'))))
img1 = cv2.imread(os.path.join(img_dir, img_name1))
K1 = np.array(cam_dict['K']).reshape((4, 4))
W2C1 = np.array(cam_dict['W2C']).reshape((4, 4))
cam_dict = json.load(open(os.path.join(base_dir, 'cameras_adjusted', img_name2.replace('.png', '.json'))))
img2 = cv2.imread(os.path.join(img_dir, img_name2))
K2 = np.array(cam_dict['K']).reshape((4, 4))
W2C2 = np.array(cam_dict['W2C']).reshape((4, 4))
inspect(img1, K1, W2C1, img2, K2, W2C2)