Hi again,
I tried a more brute-force approach which seems to do the trick for me. This code measures distances for each camera to every point in the sparse cloud, and gets relative heights from the ten horizontally closest points. It takes forever to run, but it worked well when I tested it.
import PhotoScan
chunk = PhotoScan.app.document.chunk
# Function to transform coordinates to used crs
def transformed(vector):
return chunk.crs.project(chunk.transform.matrix.mulp(vector))
camera_heights = {}
for camera in chunk.cameras:
cam_pos = transformed(camera.center)
# Iterate through every point in the sparse cloud, and measure their distances to the camera.
heights = {}
for point in chunk.point_cloud.points:
point_pos = transformed(point.coord[:3])
distance = cam_pos - point_pos
xy_distance = distance[:2].norm() # To be used later for sorting
heights[xy_distance] = distance[2]
# Sort a list of keys (xy distances)
keys = list(heights.keys())
keys.sort()
# Mean of ten points with the lowest xy distances.
h_mean = sum([heights[k] for k in keys[:10]]) / 10
camera_heights[camera.label] = h_mean
# 'break' could be added here to test the loop.
for k, v in camera_heights.items():
print(k, v)
A suggested addition is to actually save the results in a csv or something, since this is a quite lengthy process. Hope it helps!