-
Notifications
You must be signed in to change notification settings - Fork 0
/
stereo_reconstruction.py
218 lines (171 loc) · 6.71 KB
/
stereo_reconstruction.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import cv2
import numpy as np
import glob
import json
from tqdm import tqdm
import PIL.ExifTags
import PIL.Image
from matplotlib import pyplot as plt
from ast import literal_eval
import open3d as o3d
#Function to create point cloud file
def create_output(vertices, colors, filename):
colors = colors.reshape(-1,3)
vertices = np.hstack([vertices.reshape(-1,3),colors])
ply_header = '''ply
format ascii 1.0
element vertex %(vert_num)d
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
'''
with open(filename, 'w') as f:
f.write(ply_header %dict(vert_num=len(vertices)))
np.savetxt(f,vertices,'%f %f %f %d %d %d')
def Load_Images(Img1_path,Img2_path ,GreyScale):
#Load 2 Images
if(GreyScale):
image1=cv2.imread(Img1_path,0)
image2=cv2.imread(Img2_path,0)
else:
image1=cv2.imread(Img1_path)
image2=cv2.imread(Img2_path)
return image1,image2
def Load_intrinsics(Path):
with open(Path) as f:
lines = f.readlines()
cam_matrix = lines[0].split("=")[1][:-1]
cam_matrix = "[" + cam_matrix +"]"
cam_matrix = cam_matrix.replace(";","],[")
cam_matrix =np.matrix(cam_matrix).reshape(3,3)
return cam_matrix
def calculate_matches(img1,img2,cam_matrix):
#Feature detection and Matching as in Task1
sift = cv2.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
good = []
pts1 = []
pts2 = []
# ratio test as per Lowe's paper
#getting the matched points in appropriate format
MatchesList=[]
for i,(m,n) in enumerate(matches):
if m.distance < 0.8*n.distance:
good.append([m])
pts2.append(kp2[m.trainIdx].pt)
pts1.append(kp1[m.queryIdx].pt)
MatchesList.append(m)
pts1 = np.int32(pts1)
pts2 = np.int32(pts2)
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
plt.imshow(img3),plt.show()
return pts1, pts2
def Calculate_RT(pts1,pts2,cam_matrix):
E , mask = cv2.findEssentialMat(pts1,pts2,cam_matrix) #Finding E matrix
pts1 = pts1[mask.ravel()==1] #eliminate outliers
pts2 = pts2[mask.ravel()==1]
_, R, t, mask = cv2.recoverPose(E,pts1,pts2,cam_matrix) # Decompose E into R AND t
pts1 = pts1[mask.ravel()==255] #eliminate outliers
pts2 = pts2[mask.ravel()==255]
return R,t ,pts1,pts2
def disparityMap(img1,img2):
stereo = cv2.StereoBM_create()
####### Disparity Hyperparameters #######################
minDisparity = 15
numDisparities= 128
stereo.setNumDisparities(numDisparities)
stereo.setBlockSize(37)
stereo.setPreFilterType(1)
stereo.setPreFilterSize(11)
stereo.setPreFilterCap(19)
stereo.setTextureThreshold(10)
stereo.setUniquenessRatio(7)
stereo.setSpeckleRange(25)
stereo.setSpeckleWindowSize(12)
stereo.setDisp12MaxDiff(5)
stereo.setMinDisparity(minDisparity)
###################################################################################
############################## Compute and visualize Disparity between the two Images #####################
disparity_map = stereo.compute(img1,img2)
disparity_map = disparity_map.astype(np.float32)
disparity_map = (disparity_map/16.0 - minDisparity)/numDisparities #### Normalize disparity values #############
plt.imshow(disparity_map,'gray')
plt.show()
return disparity_map
def triangulation(pts1,pts2,cam_matrix,R,t , coloredImage):
T = np.concatenate((R,t),axis =1)
#T_h = np.concatenate((T,np.array([[0,0,0,1]])),axis =0)
PM2 = np.matmul(cam_matrix,T)
PM1 = np.matmul(cam_matrix,np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0]]))
#T_inv = np.linalg.inv(T_h)
#T=T_inv
points_3d=[]
colors = []
A = np.zeros((4,4))
for i in range(len(pts1)):
# point1 = kp1[m.queryIdx].pt
# point2 = kp2[m.trainIdx].pt
point1 = pts1[i]
point2 = pts2[i]
A[0]=(point1[1]*PM1[2])- PM1[1]
A[1]= PM1[0] -(point1[0]*PM1[2])
A[2]=(point2[1]*PM2[2])- PM2[1]
A[3] = PM2[0] -(point2[0]*PM2[2])
W,V =np.linalg.eig(np.matmul(A.T,A))
point_world=V[:,np.argmin(W)]
point_world= (1/point_world[3])*point_world
points_3d.append(point_world[:3])
#breakpoint()
colors.append(coloredImage[point1[1],point1[0]])
points_3d = np.array(points_3d)
colors = np.array(colors)
#breakpoint()
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points_3d)
#pcd.colors = o3d.utility.Vector3dVector(colors)
o3d.visualization.draw_geometries([pcd])
return points_3d , colors
def stereo_reconstruct(img1,img2,colored_image1,colored_image2,cam_matrix):
pts1 , pts2 = calculate_matches(img1,img2,cam_matrix)
R , t ,pts1,pts2= Calculate_RT(pts1,pts2,cam_matrix)
h,w = img1.shape[:2]
_,_,_,_,QQ,_,_=cv2.stereoRectify(cam_matrix, 0, cam_matrix, 0,(h, w), R, t)
disparity_map = disparityMap(img1,img2)
points_3D = cv2.reprojectImageTo3D(disparity_map, QQ)
#Get color points
colors = cv2.cvtColor(colored_image1, cv2.COLOR_BGR2RGB)
#Get rid of points with value 0 (i.e no depth)
mask_map = disparity_map > 0.25
#Mask colors and points.
output_points = points_3D[mask_map]
output_colors = colors[mask_map]
#Define name for output file
output_file = 'reconstructedDisparity.ply'
#Generate point cloud
print ("\n Creating the output file... \n")
create_output(output_points, output_colors, output_file)
points_3d ,colors= triangulation(pts1,pts2,cam_matrix,R,t,colors)
output_file = 'reconstructedTriang.ply'
create_output(points_3d, colors, output_file)
return
if __name__ == '__main__':
img1_path = "im0.png"
img2_path = "im1.png"
calib = "calib.txt"
img1,img2 = Load_Images(img1_path,img2_path, True)
colored_image1,colored_image2 = Load_Images(img1_path,img2_path, False)
cam_matrix = Load_intrinsics(calib)
stereo_reconstruct(img1,img2,colored_image1,colored_image2,cam_matrix)
breakpoint()