forked from AntoniaBie/LoD3ForLocalization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager3DCoords.py
More file actions
285 lines (217 loc) · 13.3 KB
/
manager3DCoords.py
File metadata and controls
285 lines (217 loc) · 13.3 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 4 16:15:05 2023
@author: anton
"""
import numpy as np
import featureMatching
import extractCoords
import os
import matchCoords
import spatialResection
import matplotlib.image
import matplotlib.pyplot as plt
import cv2
def main(cam,GNSS,mesh,image_folder_real,image_folder_LoD,folder_mask,raycast_results,traj_points,method,i):
traj_points_x = []
traj_points_y = []
traj_points_z = []
if method == 'feature images':
print('Method: feature images')
# extract LoD-model image coordinates
print("__________________________________________")
print("Now finding features in the single images (compared with themselves)")
coord1_LoD_2D,_ = featureMatching.get_coordinates(image_folder_LoD + str(i) + ".jpeg",image_folder_LoD + str(i) + ".jpeg",1000)
# extract real world image coordinates
list_images = os.listdir(image_folder_real)
coord1_real_2D,_ = featureMatching.get_coordinates(image_folder_real+"/"+list_images[i],image_folder_real+"/"+list_images[i],1001)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using images created on features")
coord1_LoD_2D_selection,coord1_real_2D_selection = matchCoords.main(cam,coord1_LoD_2D,coord1_real_2D)
# extract the 3D coordinates for the features which are matched between real world and LoD-model
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D_selection, coord1_real_2D_selection)
print('.')
elif method == 'canny':
print('Method: canny')
list_images = os.listdir(image_folder_real)
img_real = cv2.imread(image_folder_real+"/"+list_images[i])
img_LoD = cv2.imread(image_folder_LoD + str(i) + ".jpeg")
# Convert to graycsale
img_real_gray = cv2.cvtColor(img_real, cv2.COLOR_BGR2GRAY)
img_LoD_gray = cv2.cvtColor(img_LoD, cv2.COLOR_BGR2GRAY)
# Blur the image for better edge detection
img_real_blur = cv2.GaussianBlur(img_real_gray, (3,3), 0)
img_LoD_blur = cv2.GaussianBlur(img_LoD_gray, (3,3), 0)
edges_real = cv2.Canny(image=img_real_blur, threshold1=100, threshold2=100)
edges_LoD = cv2.Canny(image=img_LoD_blur, threshold1=100, threshold2=100)
path_real = './images_sobel/image_real.jpeg'
path_LoD = './images_sobel/image_LoD.jpeg'
cv2.imwrite(path_real, edges_real)
cv2.imwrite(path_LoD, edges_LoD)
coord1_LoD_2D,coord1_real_2D = featureMatching.get_coordinates(path_real,path_LoD,1003)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using images created by the canny edge detection")
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D, coord1_real_2D)
elif method == 'real images':
print('Method: real images')
list_images = os.listdir(image_folder_real)
path_real = image_folder_real+"/"+list_images[i]
path_LoD = image_folder_LoD + str(i) + ".jpeg"
coord1_LoD_2D,coord1_real_2D = featureMatching.get_coordinates(path_real,path_LoD,1003)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using real images")
# extract the 3D coordinates for the features which are matched between real world and LoD-model
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D, coord1_real_2D)
elif method == 'sobel':
print('Method: sobel')
list_images = os.listdir(image_folder_real)
img_real = cv2.imread(image_folder_real+"/"+list_images[i])
img_LoD = cv2.imread(image_folder_LoD + str(i) + ".jpeg")
# Convert to graycsale
img_real_gray = cv2.cvtColor(img_real, cv2.COLOR_BGR2GRAY)
img_LoD_gray = cv2.cvtColor(img_LoD, cv2.COLOR_BGR2GRAY)
# Blur the image for better edge detection
img_real_blur = cv2.GaussianBlur(img_real_gray, (3,3), 0)
img_LoD_blur = cv2.GaussianBlur(img_LoD_gray, (3,3), 0)
sobelx_real = cv2.Sobel(src=img_real_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
sobely_real = cv2.Sobel(src=img_real_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
sobelx_LoD = cv2.Sobel(src=img_LoD_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
sobely_LoD = cv2.Sobel(src=img_LoD_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
sobel_real = sobelx_real + sobely_real
sobel_LoD = sobelx_LoD + sobely_LoD
path_real = './images_sobel/image_real.jpeg'
path_LoD = './images_sobel/image_LoD.jpeg'
cv2.imwrite(path_real, sobel_real)
cv2.imwrite(path_LoD, sobely_LoD)
coord1_LoD_2D,coord1_real_2D = featureMatching.get_coordinates(path_real,path_LoD,1003)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using images created by the sobel filter")
# extract the 3D coordinates for the features which are matched between real world and LoD-model
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D, coord1_real_2D)
elif method == 'mask':
print('Method: mask')
list_images = os.listdir(image_folder_real)
list_mask = os.listdir(folder_mask)
#image_folder_real
img1 = plt.imread(image_folder_real+"/"+list_images[i])[:,:,0] * plt.imread(folder_mask+"/"+list_mask[i])
path_real = './images_mask/image_mask.jpeg'
matplotlib.image.imsave(path_real, img1)
coord1_LoD_2D,coord1_real_2D = featureMatching.get_coordinates(path_real,image_folder_LoD + str(i) + ".jpeg",1003)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using masked images")
# extract the 3D coordinates for the features which are matched between real world and LoD-model
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D, coord1_real_2D)
elif method == 'mask and sobel':
print('Method: mask and sobel')
list_images = os.listdir(image_folder_real)
list_mask = os.listdir(folder_mask)
#image_folder_real
img1 = plt.imread(image_folder_real+"/"+list_images[i])[:,:,0] * plt.imread(folder_mask+"/"+list_mask[i])
path_real = './images_mask/image_mask.jpeg'
matplotlib.image.imsave(path_real, img1)
img_real = cv2.imread(path_real)
img_LoD = cv2.imread(image_folder_LoD + str(i) + ".jpeg")
# Convert to graycsale
img_real_gray = cv2.cvtColor(img_real, cv2.COLOR_BGR2GRAY)
img_LoD_gray = cv2.cvtColor(img_LoD, cv2.COLOR_BGR2GRAY)
# Blur the image for better edge detection
img_real_blur = cv2.GaussianBlur(img_real_gray, (3,3), 0)
img_LoD_blur = cv2.GaussianBlur(img_LoD_gray, (3,3), 0)
sobelx_real = cv2.Sobel(src=img_real_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
sobely_real = cv2.Sobel(src=img_real_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
sobelx_LoD = cv2.Sobel(src=img_LoD_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
sobely_LoD = cv2.Sobel(src=img_LoD_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
sobel_real = sobelx_real + sobely_real
sobel_LoD = sobelx_LoD + sobely_LoD
path_real = './images_sobel/image_real.jpeg'
path_LoD = './images_sobel/image_LoD.jpeg'
cv2.imwrite(path_real, sobel_real)
cv2.imwrite(path_LoD, sobel_LoD)
coord1_LoD_2D,coord1_real_2D = featureMatching.get_coordinates(path_real,path_LoD,1003)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using masked images and sobel")
# extract the 3D coordinates for the features which are matched between real world and LoD-model
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D, coord1_real_2D)
elif method == 'mask and canny':
print('Method: mask and canny')
list_images = os.listdir(image_folder_real)
list_mask = os.listdir(folder_mask)
#image_folder_real
img1 = plt.imread(image_folder_real+"/"+list_images[i])[:,:,0] * plt.imread(folder_mask+"/"+list_mask[i])
path_real = './images_mask/image_mask.jpeg'
matplotlib.image.imsave(path_real, img1)
img_real = cv2.imread(path_real)
img_LoD = cv2.imread(image_folder_LoD + str(i) + ".jpeg")
# Convert to graycsale
img_real_gray = cv2.cvtColor(img_real, cv2.COLOR_BGR2GRAY)
img_LoD_gray = cv2.cvtColor(img_LoD, cv2.COLOR_BGR2GRAY)
# Blur the image for better edge detection
img_real_blur = cv2.GaussianBlur(img_real_gray, (3,3), 0)
img_LoD_blur = cv2.GaussianBlur(img_LoD_gray, (3,3), 0)
canny_real = cv2.Canny(image=img_real_blur, threshold1=100, threshold2=200)
canny_LoD = cv2.Canny(image=img_LoD_blur, threshold1=100, threshold2=200)
path_real = './images_sobel/image_real.jpeg'
path_LoD = './images_sobel/image_LoD.jpeg'
cv2.imwrite(path_real, canny_real)
cv2.imwrite(path_LoD, canny_LoD)
coord1_LoD_2D,coord1_real_2D = featureMatching.get_coordinates(path_real,path_LoD,1003)
if coord1_LoD_2D.size == 1 or coord1_real_2D.size == 1:
print("No good matches were found, skipping this image-pair")
traj = traj_points
else:
# match real world and LoD-model 2D images coordinates
# only for the one image-pair: these camera coords are calculated!
print("__________________________________________")
print("Now using masked images and canny")
# extract the 3D coordinates for the features which are matched between real world and LoD-model
coord1_LoD_3D, coord1_real_2D_selection = extractCoords.main(raycast_results, mesh, coord1_LoD_2D, coord1_real_2D)
else:
print('Please select a method to find features in the images.')
camera_pos, std = spatialResection.main(coord1_real_2D_selection,coord1_LoD_3D,cam,GNSS[i,:])
traj_points_x.append(camera_pos[0])
traj_points_y.append(camera_pos[1])
traj_points_z.append(camera_pos[2])
traj_points_x = np.asarray([traj_points_x]).T
traj_points_y = np.asarray([traj_points_y]).T
traj_points_z = np.asarray([traj_points_z]).T
traj = np.concatenate((traj_points_x,traj_points_y,traj_points_z),axis=0)
traj = np.squeeze(traj)
traj = traj[:,np.newaxis]
traj = np.concatenate((traj_points,traj),axis = 1)
return traj, std