@@ -17,6 +17,11 @@ def createPointCloud(points, colors, filename):
17
17
cloud .to_file (filename )
18
18
19
19
20
+ def normalizePoints (points , focal , pp ):
21
+ points = [ [(p [0 ] - pp [0 ]) / focal , (p [1 ] - pp [1 ]) / focal ] for p in points ]
22
+ return points
23
+
24
+
20
25
def updateTrajectoryDrawing (trackedPoints , groundtruthPoints ):
21
26
plt .cla ()
22
27
plt .plot (trackedPoints [:,0 ], trackedPoints [:,1 ], c = 'blue' , label = "Tracking" )
@@ -32,12 +37,12 @@ def updateTrajectoryDrawing(trackedPoints, groundtruthPoints):
32
37
detector = cv2 .FastFeatureDetector_create (threshold = 20 , nonmaxSuppression = True )
33
38
datasetReader = DatasetReaderKITTI ("videos/KITTI/data_odometry_gray/dataset/sequences/00/" )
34
39
35
- K = datasetReader .readCameraMatrix ()
40
+ K , focal , pp = datasetReader .readCameraMatrix ()
36
41
prevFrameBGR = datasetReader .readFrame (0 )
37
42
38
43
prevPts = np .empty (0 )
39
44
voTruthPoints , voTrackPoints = [], []
40
- currR , currT = np .eye (3 ), np .zeros ((3 ,1 ))
45
+ rotation , position = np .eye (3 ), np .zeros ((3 ,1 ))
41
46
42
47
plt .show ()
43
48
@@ -62,42 +67,41 @@ def updateTrajectoryDrawing(trackedPoints, groundtruthPoints):
62
67
_ , R , T , mask = cv2 .recoverPose (E , currPts , prevPts , K )
63
68
64
69
# Read groundtruth translation T and absolute scale for computing trajectory
65
- truthT , truthScale = datasetReader .readGroundtuthPosition (frameIdx )
70
+ truthPos , truthScale = datasetReader .readGroundtuthPosition (frameIdx )
66
71
if truthScale <= 0.1 :
67
72
continue
68
73
69
74
# Update the pose
70
- currT = currT + truthScale * currR .dot (T )
71
- currR = R .dot (currR )
75
+ position = position + truthScale * rotation .dot (T )
76
+ rotation = R .dot (rotation )
77
+
78
+ # Reconstruct 3D points
79
+ if frameIdx == 1 :
80
+ P = np .hstack ((R , T ))
81
+ triangPoints = cv2 .triangulatePoints (np .eye (3 , 4 ), P ,
82
+ np .transpose (normalizePoints (prevPts , focal = focal , pp = pp )),
83
+ np .transpose (normalizePoints (currPts , focal = focal , pp = pp ))
84
+ )
85
+
86
+ triangPoints = np .transpose (triangPoints )
87
+ triangPoints = np .array ([[x / w , y / w , z / w ] for [x , y , z , w ] in triangPoints ])
88
+
89
+ colors = np .array ([currFrameBGR [int (pt [1 ]),int (pt [0 ])] for pt in prevPts ])
90
+ print (colors )
91
+ createPointCloud (triangPoints , colors , "slam_cloud.ply" )
72
92
73
93
# Update vectors of tracked and ground truth positions, draw trajectory
74
- voTrackPoints .append ([currT [0 ], currT [2 ]])
75
- voTruthPoints .append ([truthT [0 ], truthT [2 ]])
94
+ voTrackPoints .append ([position [0 ], position [2 ]])
95
+ voTruthPoints .append ([truthPos [0 ], truthPos [2 ]])
76
96
updateTrajectoryDrawing (np .array (voTrackPoints ), np .array (voTruthPoints ))
77
97
drawFrameFeatures (currFrame , prevPts , currPts , frameIdx )
78
98
79
99
if cv2 .waitKey (1 ) == ord ('q' ):
80
100
break
81
-
82
- winSize , minDisp , maxDisp = 5 , - 1 , 63
83
- stereo = cv2 .StereoSGBM_create (minDisparity = minDisp , numDisparities = (maxDisp - minDisp ), blockSize = 5 ,
84
- uniquenessRatio = 5 , speckleWindowSize = 5 , speckleRange = 5 , disp12MaxDiff = 1 , P1 = 8 * 3 * winSize ** 2 , P2 = 32 * 3 * winSize ** 2 )
85
- disparityMap = stereo .compute (prevFrame , currFrame )
86
-
87
- focal_length = K [0 , 0 ]
88
- Q2 = np .float32 ([[1 , 0 , 0 , 0 ], [0 , - 1 , 0 , 0 ], [0 , 0 , focal_length * 0.05 , 0 ], [0 , 0 , 0 , 1 ]])
89
-
90
- points_3D = cv2 .reprojectImageTo3D (disparityMap , Q2 )
91
- colors = cv2 .cvtColor (currFrameBGR , cv2 .COLOR_BGR2RGB )
92
- mask_map = disparityMap > disparityMap .min ()
93
- output_points = points_3D [mask_map ]
94
- output_colors = colors [mask_map ]
95
- createPointCloud (output_points , output_colors , "slam.ply" )
96
-
101
+
97
102
# Consider current frame as previous for the next step
98
- prevFrameBGR = currFrameBGR
99
- prevPts = currPts
100
-
103
+ prevPts , prevFrameBGR = currPts , currFrameBGR
104
+
101
105
# plt.savefig('trajectory.png')
102
106
cv2 .waitKey (0 )
103
107
cv2 .destroyAllWindows ()
0 commit comments