Skip to content

Commit be1112f

Browse files
committed
Generate point cloud using triangulation
1 parent 7cddcea commit be1112f

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode
22
*.pyc
3-
videos/
3+
videos/
4+
*.ply

DatasetReaderKITTI.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def readCameraMatrix(self):
3737
K[1, 2] = cy
3838
K[2, 2] = 1
3939
print("Constructed camera matrix {}:\n{}".format(K.shape, K))
40-
return K
40+
return K, focal, (cx, cy)
4141

4242
def readGroundtuthPosition(self, frameId):
4343
groundtruthFile = os.path.join(self._datasetPath, "poses.txt")

slam.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def createPointCloud(points, colors, filename):
1717
cloud.to_file(filename)
1818

1919

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+
2025
def updateTrajectoryDrawing(trackedPoints, groundtruthPoints):
2126
plt.cla()
2227
plt.plot(trackedPoints[:,0], trackedPoints[:,1], c='blue', label="Tracking")
@@ -32,12 +37,12 @@ def updateTrajectoryDrawing(trackedPoints, groundtruthPoints):
3237
detector = cv2.FastFeatureDetector_create(threshold=20, nonmaxSuppression=True)
3338
datasetReader = DatasetReaderKITTI("videos/KITTI/data_odometry_gray/dataset/sequences/00/")
3439

35-
K = datasetReader.readCameraMatrix()
40+
K, focal, pp = datasetReader.readCameraMatrix()
3641
prevFrameBGR = datasetReader.readFrame(0)
3742

3843
prevPts = np.empty(0)
3944
voTruthPoints, voTrackPoints = [], []
40-
currR, currT = np.eye(3), np.zeros((3,1))
45+
rotation, position = np.eye(3), np.zeros((3,1))
4146

4247
plt.show()
4348

@@ -62,42 +67,41 @@ def updateTrajectoryDrawing(trackedPoints, groundtruthPoints):
6267
_, R, T, mask = cv2.recoverPose(E, currPts, prevPts, K)
6368

6469
# Read groundtruth translation T and absolute scale for computing trajectory
65-
truthT, truthScale = datasetReader.readGroundtuthPosition(frameIdx)
70+
truthPos, truthScale = datasetReader.readGroundtuthPosition(frameIdx)
6671
if truthScale <= 0.1:
6772
continue
6873

6974
# 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")
7292

7393
# 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]])
7696
updateTrajectoryDrawing(np.array(voTrackPoints), np.array(voTruthPoints))
7797
drawFrameFeatures(currFrame, prevPts, currPts, frameIdx)
7898

7999
if cv2.waitKey(1) == ord('q'):
80100
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+
97102
# Consider current frame as previous for the next step
98-
prevFrameBGR = currFrameBGR
99-
prevPts = currPts
100-
103+
prevPts, prevFrameBGR = currPts, currFrameBGR
104+
101105
# plt.savefig('trajectory.png')
102106
cv2.waitKey(0)
103107
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)