-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotogramm_from_mesh_Vietpara.py
295 lines (228 loc) · 9.76 KB
/
photogramm_from_mesh_Vietpara.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
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
286
287
288
289
290
291
292
293
294
295
# Viet Nguyen
# University of Greifswald
# based on the Geo-SfM course from The University Centre in Svalbard
# and the work from Derek Young and Alex Mandel (https://github.com/open-forest-observatory/automate-metashape/tree/main).
# project crs
EPSG = "EPSG::5650"
import Metashape
from pathlib import Path
import time
doc = Metashape.app.document # accesses the current project and document
chunk = doc.chunk # access the active chunk
def diff_time(t2, t1):
'''
Give a end and start time, subtract, and round
'''
total = str(round(t2-t1, 1))
return total
def reset_region(doc):
'''
Reset the region and make it much larger than the points;
necessary because if points go outside the region,
they get clipped when saving
'''
doc.chunk.resetRegion()
region_dims = doc.chunk.region.size
region_dims[2] *= 3
doc.chunk.region.size = region_dims
return True
def progress_print(p):
print('Current task progress: {:.2f}%'.format(p))
######################
#### rename photo ####
######################
for c in chunk.cameras: # loops over all cameras in the active chunk
cp = Path(c.photo.path) # gets the path for each photo
c.label = str(cp.parent.name) + '/' + cp.name # renames the camera label in the metashape project to include the parent directory of the photo
#####################
#### align photo ####
#####################
# Match photos, align cameras, optimize cameras
# get a beginning time stamp
timer2a = time.time()
# Align cameras
doc.chunk.matchPhotos(downscale=2, # USGS (1) # medium(2) for vegetation as OFO
generic_preselection=True,
reference_preselection=True,
reference_preselection_mode = Metashape.ReferencePreselectionSource,
filter_mask = True,
mask_tiepoints=False,
filter_stationary_points=True,
keypoint_limit=40000, # 60000 for high quality photos
keypoint_limit_per_mpx = 5000,
tiepoint_limit=10000,
keep_keypoints=True,
guided_matching=True,
reset_matches=False)
doc.chunk.alignCameras(adaptive_fitting = True, # True as OFO
reset_alignment = False)
# reset the region
reset_region(doc)
print('Reset region finish.')
# get an ending time stamp
timer2b = time.time()
# calculate difference between end and start time to 1 decimal place
time2 = diff_time(timer2b, timer2a)
# print time record
print('Alignment finish after',time2,'second.')
###############################################
#### optimize camera and filter tie points ####
###############################################
class metashape_tiepoint_filter:
def __init__(self,ms_path: Path):
if not ms_path == None:
self.doc = Metashape.Document()
self.doc.open(ms_path.as_posix())
else:
self.doc = Metashape.app.document
self.chunk = self.doc.chunk
self.total_tie_points = len(self.chunk.point_cloud.points)
def standard_run(self):
if len(self.chunk.dense_clouds) == 0:
self.optimize_cameras()
self.filter_reconstruction_uncertainty()
self.optimize_cameras()
# self.doc.save()
self.filter_projection_accuracy()
self.optimize_cameras()
# self.doc.save()
self.filter_reprojection_error()
self.optimize_cameras()
self.reset_region()
self.set_label_naming_template()
# self.doc.save()
else:
print("Dense cloud exists... Ignoring..")
def reset_region(self):
# Reset the region and make it much larger than the points; necessary because if points go outside the region, they get clipped when saving
self.chunk.resetRegion()
region_dims = self.chunk.region.size
region_dims[2] *= 3
self.chunk.region.size = region_dims
print('Reset region finish.')
def optimize_cameras(self, parameters = None):
print("optimize_cameras")
self.chunk.optimizeCameras(
fit_corrections=True,
tiepoint_covariance=True,
adaptive_fitting = True,
progress=progress_print
)
def filter_reconstruction_uncertainty(self, x = 15): # 10 according to USGS # 15 according to OFO
print("filter_reconstruction_uncertainty")
self.chunk = self.chunk.copy()
f = Metashape.PointCloud.Filter()
f.init(self.chunk, criterion = Metashape.PointCloud.Filter.ReconstructionUncertainty)
while (len([i for i in f.values if i >= x])/self.total_tie_points) >= 0.2: # 0.5 according to usgs # 0.2 according to OFO
x += 0.1
x = round(x,1)
self.chunk.label = f"RecUnc={x}"
f.removePoints(x)
def filter_projection_accuracy(self, x = 2): # 3 according to usgs # 2 according to OFO
print("filter_projection_accuracy")
# self.chunk = self.chunk.copy()
f = Metashape.PointCloud.Filter()
f.init(self.chunk, criterion = Metashape.PointCloud.Filter.ProjectionAccuracy)
while (len([i for i in f.values if i >= x])/len(self.chunk.point_cloud.points)) >= 0.3: # 0.5 according to usgs # 0.3 according to OFO
x += 0.1
x = round(x,1)
# old version with self.chunk.coppy(): self.chunk.label = f"{self.chunk.label.split('Copy of ')[1]}_ProjAcc={x}"
self.chunk.label = f"{self.chunk.label}_ProjAcc={x}"
f.removePoints(x)
def filter_reprojection_error(self, x = 0.3): # 0.3 according to OFO
print("filter_reprojection_error")
# self.chunk = self.chunk.copy()
f = Metashape.PointCloud.Filter()
f.init(self.chunk, criterion = Metashape.PointCloud.Filter.ReprojectionError)
while (len([i for i in f.values if i >= x])/len(self.chunk.point_cloud.points)) >= 0.05:# 0.1 as usgs # 0.05 according to OFO
x += 0.005
print('Reprojection error level:',x)
x = round(x,2)
# old version with self.chunk.coppy(): self.chunk.label = f"{self.chunk.label.split('Copy of ')[1]}_RepErr={x}"
self.chunk.label = f"{self.chunk.label}_RepErr={x}"
f.removePoints(x)
def set_label_naming_template(self):
self.chunk.label = f"{self.chunk.label}"
a = metashape_tiepoint_filter(None)
a.standard_run()
###########################
#### build dense cloud ####
###########################
### Build depth maps
# get a beginning time stamp for the next step
timer4a = time.time()
# build depth maps only instead of also building the dense cloud ##?? what does
doc.chunk.buildDepthMaps(downscale = 2, # medium (4) according to OFO
filter_mode = Metashape.MildFiltering) # Moderate according to OFO
### Build dense cloud
# get a beginning time stamp for the next step
timer3a = time.time()
# build dense cloud
doc.chunk.buildDenseCloud(point_colors = True,
point_confidence = True,
max_neighbors=100)
# get an ending time stamp for the previous step
timer4b = time.time()
# calculate difference between end and start time to 1 decimal place
time4 = diff_time(timer4b, timer4a)
print('Build Dense cloud finished after',time4,'seconds.')
####################
#### build mesh ####
####################
# get a beginning time stamp for the next step
timer5a = time.time()
# build mesh
doc.chunk.buildModel(surface_type=Metashape.HeightField,
interpolation=Metashape.EnabledInterpolation,
face_count=Metashape.MediumFaceCount, # medium as OFO
source_data=Metashape.DenseCloudData,
vertex_colors=True,
vertex_confidence=True)
# decimate mesh to half of face count
doc.chunk.decimateModel(face_count = len(doc.chunk.model.faces) / 2)
# smooth mesh
doc.chunk.smoothModel(50)
# get an ending time stamp for the previous step
timer5b = time.time()
# calculate difference between end and start time to 1 decimal place
time5 = diff_time(timer5b, timer5a)
print('Build Mesh finished after',time5,'seconds.')
###########################
#### build orthomosaic ####
###########################
# get a beginning time stamp for the next step
timer6a = time.time()
# prepping projection
projection = Metashape.OrthoProjection()
projection.crs = Metashape.CoordinateSystem(EPSG)
# build orthomosaic
doc.chunk.buildOrthomosaic(surface_data=Metashape.ModelData,
blending_mode=Metashape.MosaicBlending,
ghosting_filter=True,
fill_holes=True,
cull_faces=True,
refine_seamlines=True, # True as OFO
projection=projection)
# get an ending time stamp for the previous step
timer6b = time.time()
# calculate difference between end and start time to 1 decimal place
time6 = diff_time(timer6b, timer6a)
print('Build Orthomosaic finished after',time6,'seconds.')
###################
#### build DEM ####
###################
# get a beginning time stamp for the next step
timer7a = time.time()
# prepping params for buildDem
projection = Metashape.OrthoProjection()
projection.crs = Metashape.CoordinateSystem(EPSG)
doc.chunk.buildDem(source_data = Metashape.DenseCloudData,
interpolation = Metashape.EnabledInterpolation,
projection = projection)
# get an ending time stamp for the previous step
timer7b = time.time()
# calculate difference between end and start time to 1 decimal place
time7 = diff_time(timer7b, timer7a)
print('Build DEM finished after',time7,'seconds.')
# save project
doc.save()