3
« on: March 21, 2024, 09:49:42 PM »
I am trying to automate a Reconstruction Uncertainty process via script but failing. Basically I would like to run Reconstruction Uncertainty, set a starting value (eg 70), remove those tie points, then run Camera Alignment Optimisation, re-run Reconstruction Uncertainty at a value of 60, remove those tie points, re-run Camera Alignment Optimisation, and so forth until Reconstruction Uncertainty gives back a set value (eg 20). Such script should also check that the tie point removal step would not remove more than 50% of the total available tie points.
First challenge I find is that I do not know how to refer to tie points for this action, it seems the Python library only refers to points in the point cloud. Even then the script I tried is not working. Have to admit I tried to load the Python documentation 2.1.1 in GPT and use some old forum conversation as starting point since I am not proficient with such scripting.
This below is the script I have been trying. It gives an error that 'Metashape.PointCloud' object has no attribute 'points'.
import Metashape
# Function to optimize camera alignment and clean up point cloud based on reconstruction uncertainty
def optimize_and_clean(chunk, start_uncertainty=70, end_uncertainty=20, step=-1):
# Ensure a point cloud is available
if len(chunk.point_cloud.points) == 0:
print("The chunk does not contain a point cloud.")
return
# Iterate over the specified range of reconstruction uncertainty values
for threshold_RU in range(start_uncertainty, end_uncertainty + step, step):
pointCount = len(chunk.point_cloud.points)
# Initialize filter
f = Metashape.PointCloud.Filter()
f.init(chunk, criterion=Metashape.PointCloud.Filter.ReconstructionUncertainty)
f.selectPoints(threshold_RU)
pointSelected = len([p for p in chunk.point_cloud.points if p.selected])
# Check if selected points are less than half of total points
if pointSelected < (pointCount / 2):
f.removePoints(threshold_RU)
print("Points removed with RU Threshold:", threshold_RU)
else:
print("Not enough points selected for threshold:", threshold_RU, "- adjusting threshold.")
# Optimize camera alignment with estimation of tie point covariance after adjustment
chunk.optimizeCameras()
print("Camera alignment optimized with RU Threshold:", threshold_RU)
# Assuming 'chunk' is the currently active chunk
chunk = Metashape.app.document.chunk
optimize_and_clean(chunk)