Hello ctabain,
You can estimate average GSD for the project using the following code:
import Metashape, statistics, math
def percentile(data, perc: int):
size = len(data)
return sorted(data)[int(math.ceil((size * perc) / 100)) - 1]
def estimate_gsd_height(chunk):
cameras = [camera for camera in chunk.cameras if camera.transform and camera.type == Metashape.Camera.Type.Regular]
if not len(cameras):
print("No aligned cameras, script aborted")
return 0
point_cloud = chunk.point_cloud
points = point_cloud.points
npoints = len(points)
projections = point_cloud.projections
T = chunk.transform.matrix
cameras = [camera for camera in list(cameras) if (camera in projections)]
point_ids = [-1] * len(point_cloud.tracks)
for point_id in range(0, npoints):
point_ids[points[point_id].track_id] = point_id
dist = list()
flight_height = list()
for camera in cameras:
sensor = camera.sensor
f = sensor.calibration.f
dist_camera = list()
altitude = list()
for proj in projections[camera]:
track_id = proj.track_id
point_id = point_ids[track_id]
if point_id < 0:
continue
point = points[point_id]
if not point.valid:
continue
coord = point.coord
coord.size = 3
d = (T.mulp(coord) - T.mulp(camera.center)).norm()
dist_camera.append(d / f)
altitude.append(d)
flight_height.append(percentile(altitude, 20))
#print(camera.label, flight_height[-1])
#flight_height.append(statistics.median(altitude))
dist.append(statistics.median(dist_camera))
X = statistics.median(flight_height)
#X = sum(flight_height) / len(flight_height)
gsd = sum(dist)/len(dist)
print("GSD = " + str(gsd))
print("Flight height = " + str(X))
return gsd, X
chunk = Metashape.app.document.chunk
estimate_gsd_height(chunk)
Even if it doesn't give you the same result as the default value in Build Orthomosaic dialog, the value will be quite close.