Forum

Author Topic: extracting information about Reprojection error from Metashape 1.6  (Read 2321 times)

Neb0skreb

  • Newbie
  • *
  • Posts: 4
    • View Profile
Good afternoon. For more detailed scan processing analytics, it is necessary to extract information about Reprojection error.
Need help writing Python script for metashape version 1.6

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14837
    • View Profile
Re: extracting information about Reprojection error from Metashape 1.6
« Reply #1 on: May 04, 2023, 12:09:08 PM »
Hello Neb0skreb,

Can you please elaborate, what information you need to extract from Metashape project using the script?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Neb0skreb

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: extracting information about Reprojection error from Metashape 1.6
« Reply #2 on: May 04, 2023, 03:12:41 PM »
model>gradual selection>reprojection error

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14837
    • View Profile
Re: extracting information about Reprojection error from Metashape 1.6
« Reply #3 on: May 04, 2023, 03:19:25 PM »
Hello Neb0skreb,

Gradual selection filtering option via Python can be checked in the following script (has 1.6 version):
https://github.com/agisoft-llc/metashape-scripts/blob/1.6/src/contrib/gradual_selection_tie_points.py

Reprojection error calculation per camera is shown in another thread:
https://www.agisoft.com/forum/index.php?topic=14757.msg64682#msg64682

If this information is not sufficient for your needs, let me know, what problems you are facing with mentioned scripts.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Neb0skreb

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: extracting information about Reprojection error from Metashape 1.6
« Reply #4 on: May 08, 2023, 01:00:28 PM »
With the help of the suggested scripts, I was not able to get the desired value. my goal is to repeat the value that is visible in UI metashape or in the pdf report
« Last Edit: May 13, 2023, 09:49:55 AM by Neb0skreb »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14837
    • View Profile
Re: extracting information about Reprojection error from Metashape 1.6
« Reply #5 on: May 17, 2023, 03:32:07 PM »
Hello Neb0skreb,

Please check, if the following script for Metashape 1.6 gives you the desired reprojection (pix) values (RMS and max), similar to information in Chunk Info.
The script should also work in Metashape 1.7 and 1.8. For 2.0 compatibility it should be sufficient to modify chunk.point_cloud to chunk.tie_points in two occurrences.


Code: [Select]
import Metashape
import time, math

def calc_reprojection(chunk):
point_cloud = chunk.point_cloud
points = point_cloud.points
npoints = len(points)
projections = chunk.point_cloud.projections
err_sum = 0
num = 0
maxe = 0

point_ids = [-1] * len(point_cloud.tracks)
point_errors = dict()
for point_id in range(0, npoints):
point_ids[points[point_id].track_id] = point_id

for camera in chunk.cameras:
if not camera.transform:
continue
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
error = camera.error(point.coord, proj.coord).norm() ** 2
err_sum += error
num += 1
if point_id not in point_errors.keys():
point_errors[point_id] = [error]
else:
point_errors[point_id].append(error)
if error > maxe: maxe = error

sigma = math.sqrt(err_sum / num)
return (sigma, point_errors, maxe)

chunk = Metashape.app.document.chunk

t0 = time.time()

result = calc_reprojection(chunk)

print('RMS reprojection error (pix): ' + str(result[0]))

point_errors = dict()
for point in result[1].keys():
point_errors[point] = sum(result[1][point]) / len(result[1][point])

print("Max. reprojection error (pix): " + str(math.sqrt(result[2])))#str(math.sqrt(max(point_errors.values()))))
t0 = time.time() - t0

print("Script finished in " + "{:.2f}".format(float(t0)) + " seconds")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Neb0skreb

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: extracting information about Reprojection error from Metashape 1.6
« Reply #6 on: May 18, 2023, 08:54:25 PM »
import math

doc = Metashape.app.document
chunk = doc.chunk
cameras = chunk.cameras
point_cloud = chunk.point_cloud
points = point_cloud.points
projections_per_camera = point_cloud.projections
tracks = point_cloud.tracks
point_squared_errors = [[] for i in range(len(points))]
point_key_point_size = [[] for i in range(len(points))]
track_cameras = [[] for i in range(len(tracks))]
track_projections = [[] for i in range(len(tracks))]
for camera_id, camera in enumerate(cameras):
    if camera not in projections_per_camera:
        continue

    projections = projections_per_camera[camera]

    for projection_id, projection in enumerate(projections):

        track_id = projection.track_id
        track_cameras[track_id].append(camera_id)
        track_projections[track_id].append(projection_id)

for i, point in enumerate(points):
    if point.valid is False:
        continue
    track_id = point.track_id
    for index in range(len(track_cameras[track_id])):
        camera_id = track_cameras[track_id][index]
        projection_id = track_projections[track_id][index]
        camera = cameras[camera_id]
        projections = projections_per_camera[camera]
        projection = projections[projection_id]
        key_point_size = projection.size
        error = camera.error(point.coord, projection.coord) / key_point_size
        point_squared_errors.append(error.norm() ** 2)
max_squared_error = max([max(el+[0]) for i, el in enumerate(point_squared_errors)])
max_reprojection_error = math.sqrt(max_squared_error)
print(round(max_reprojection_error,4))