|
1 | | - |
2 | | -import cv2 |
| 1 | +import cv2 as cv |
3 | 2 | import numpy as np |
4 | 3 |
|
5 | | -def main(): |
6 | | - input0, input1 = "../bin/data/image_formation0.xyz", "../bin/data/image_formation1.xyz" |
7 | | - points0, points1 = None, None |
8 | | - file0, file1 = open(input0, 'rt'), open(input1, 'rt') |
9 | | - |
10 | | - with open(input0,'rt') as file0: |
11 | | - points0 = [ list(xyz.split(' '))[:2] for xyz in file0.read().splitlines() if xyz != None ] |
12 | | - points0 = np.array(points0, dtype=np.float32) |
13 | | - with open(input1,'rt') as file0: |
14 | | - points1 = [ list(xyz.split(' '))[:2] for xyz in file1.read().splitlines() if xyz != None ] |
15 | | - points1 = np.array(points1, dtype=np.float32) |
16 | | - |
17 | | - f, cx, cy = 1000, 320, 240 |
18 | | - # print(points0.shape) |
19 | | - if len(points0) != len(points1): raise Exception("Not matching!") |
| 4 | +if __name__ == '__main__': |
| 5 | + f, cx, cy = 1000., 320., 240. |
| 6 | + pts0 = np.loadtxt('../bin/data/image_formation0.xyz')[:,:2] |
| 7 | + pts1 = np.loadtxt('../bin/data/image_formation1.xyz')[:,:2] |
| 8 | + output_file = '../bin/triangulation.xyz' |
20 | 9 |
|
21 | | - # # Estimate relative pose of two view |
22 | | - F,_ = cv2.findFundamentalMat(points0, points1, cv2.FM_8POINT) |
23 | | - K = np.array([[f,0,cx],[0,f,cy],[0,0,1]]) |
| 10 | + # Estimate relative pose of two view |
| 11 | + F, _ = cv.findFundamentalMat(pts0, pts1, cv.FM_8POINT) |
| 12 | + K = np.array([[f, 0, cx], [0, f, cy], [0, 0, 1]]) |
24 | 13 | E = K.T @ F @ K |
25 | | - _, R, t, _ = cv2.recoverPose(E, points0, points1) |
| 14 | + _, R, t, _ = cv.recoverPose(E, pts0, pts1) |
26 | 15 |
|
27 | 16 | # Reconstruct 3D points (triangulation) |
28 | | - P0 = K @ np.eye(3,4, dtype=np.float32) |
| 17 | + P0 = K @ np.eye(3, 4, dtype=np.float32) |
29 | 18 | Rt = np.hstack((R, t)) |
30 | 19 | P1 = K @ Rt |
31 | | - X = cv2.triangulatePoints(P0, P1, points0.T, points1.T) |
| 20 | + X = cv.triangulatePoints(P0, P1, pts0.T, pts1.T) |
32 | 21 | X /= X[3] |
33 | 22 | X = X.T |
34 | | - |
35 | | - triangular_file = "../bin/data/triangulation.xyz" |
36 | | - with open(triangular_file, 'wt') as f: |
37 | | - f.write(str(X[:,:3])) |
38 | 23 |
|
39 | | -if __name__=="__main__": |
40 | | - main() |
| 24 | + # Write the reconstructed 3D points |
| 25 | + np.savetxt(output_file, X) |
0 commit comments