Forum

Author Topic: Access camera locations and error estimates through python?  (Read 3447 times)

sven_a

  • Newbie
  • *
  • Posts: 19
    • View Profile
Access camera locations and error estimates through python?
« on: January 08, 2020, 02:19:27 PM »
In the processing report there is a nice table with X-Error, Y-Error, Z-Error, XY error and total error (m).
Is it possible to access this information with the python API?
We want to automatically check if the accuracy falls within the expected range (especially important for RTK flights).

Thanks in advance,

Sven

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Access camera locations and error estimates through python?
« Reply #1 on: January 08, 2020, 03:09:53 PM »
Hello Sven,

Please check the following code:

Code: [Select]
import Metashape

chunk = Metashape.app.document.chunk #active chunk
for camera in chunk.cameras:
    error = chunk.transform.matrix.mulp(camera.center) - chunk.crs.unproject(camera.reference.location)
    m = chunk.crs.localframe(chunk.transform.matrix.mulp(camera.center))
    error = m.mulv(error)
    print(camera.label, error.norm(), error.x, error.y, error.z)

It should print to Console total error for each camera, and XYZ error components.
Best regards,
Alexey Pasumansky,
Agisoft LLC

sven_a

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Access camera locations and error estimates through python?
« Reply #2 on: January 09, 2020, 05:28:23 PM »
I am able to access the x, y, z and total error for each camera now.
However when I calculate the average for each component for all cameras I get a different value then visible in the report.
Which is weird because I think it is just a simple average?

I am now using the following code

 total_error = []
    x_error = []
    y_error = []
    z_error = []
    for camera in chunk.cameras:
        error = chunk.transform.matrix.mulp(camera.center) - chunk.crs.unproject(camera.reference.location)
        m = chunk.crs.localframe(chunk.transform.matrix.mulp(camera.center))
        error = m.mulv(error)
        print(camera.label, error.x, error.y, error.z, error.norm())
        x_error.append(error.x)
        y_error.append(error.y)
        z_error.append(error.z)
        total_error.append(error.norm())
    mean_total_error = statistics.mean(total_error)
    mean_x_error = statistics.mean(x_error)
    mean_y_error = statistics.mean(y_error)
    mean_z_error = statistics.mean(z_error)

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Access camera locations and error estimates through python?
« Reply #3 on: January 09, 2020, 07:04:07 PM »
Hello Sven,

Total error represent the RMSE for the components, please see the updated code for total error calculation below:

Code: [Select]
import math, Metashape

chunk = Metashape.app.document.chunk #active chunk
total_error = Metashape.Vector([0,0,0])
n = 0
for camera in chunk.cameras:
error = chunk.transform.matrix.mulp(camera.center) - chunk.crs.unproject(camera.reference.location)
m = chunk.crs.localframe(chunk.transform.matrix.mulp(camera.center))
error = m.mulv(error)
print(camera.label, error.norm(), error.x, error.y, error.z)
total_error += Metashape.Vector([error.x ** 2, error.y **2, error.z **2])
n += 1

for i in range(len(total_error)): #printing total X, Y, Z errors
print(math.sqrt(total_error[i] / n))
Best regards,
Alexey Pasumansky,
Agisoft LLC

sven_a

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Access camera locations and error estimates through python?
« Reply #4 on: January 13, 2020, 05:56:20 PM »
Thank you. X error , Y error and Z error are good now. :)

One more question. How do you calculate XY (m) and Total error (m)?

sven_a

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Access camera locations and error estimates through python?
« Reply #5 on: January 20, 2020, 01:09:56 PM »
I think the issue is solved. I combined some old code from post with a similar question.

Code: [Select]
    errors = Metashape.Vector([0,0,0,0])
    n = 0
    for camera in chunk.cameras:
        if not camera.transform:
            continue
        if not camera.reference.location:
            continue

        estimated_geoc = chunk.transform.matrix.mulp(camera.center)
        error = estimated_geoc - chunk.crs.unproject(camera.reference.location)
        m = chunk.crs.localframe(chunk.transform.matrix.mulp(camera.center))
        error = m.mulv(error)
        errors += Metashape.Vector([error.x **2, error.y **2, error.z **2, error.norm() **2])
        n += 1

    total_error = math.sqrt(errors[3] / n)
    print("total_error", total_error)

Thank you :)