Forum

Author Topic: Disabling camera with large error  (Read 4124 times)

maddin

  • Full Member
  • ***
  • Posts: 161
    • View Profile
Disabling camera with large error
« on: February 01, 2017, 11:24:00 PM »
Using Python with PhotoScan Pro 1.2.6, how would I find all cameras in an existing project that have an error larger than a certain threshold and disable them before building textures?

Thanks!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Disabling camera with large error
« Reply #1 on: February 01, 2017, 11:43:05 PM »
Hello maddin,

Which error do you mean? Positionning error or reprojection error?
Best regards,
Alexey Pasumansky,
Agisoft LLC

maddin

  • Full Member
  • ***
  • Posts: 161
    • View Profile
Re: Disabling camera with large error
« Reply #2 on: February 01, 2017, 11:55:47 PM »
Hello maddin,

Which error do you mean? Positionning error or reprojection error?

Reprojection error is what I need. Thanks!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Disabling camera with large error
« Reply #3 on: February 02, 2017, 03:07:49 PM »
Hello maddin,

I think you can use the following script:

Code: [Select]
import PhotoScan, 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
photo_avg = {}

for camera in chunk.cameras:
if not camera.transform:
continue

T = camera.transform.inv()
calib = camera.sensor.calibration
point_index = 0
photo_num = 0
photo_err = 0
for proj in projections[camera]:
track_id = proj.track_id
while point_index < npoints and points[point_index].track_id < track_id:
point_index += 1
if point_index < npoints and points[point_index].track_id == track_id:
if not points[point_index].valid:
continue

#dist = calib.error(T.mulp(points[point_index].coord), proj.coord).norm() ** 2
dist = camera.error(points[point_index].coord, proj.coord).norm() ** 2

err_sum += dist
num += 1
photo_num += 1
photo_err += dist

photo_avg[camera.label] = (math.sqrt(photo_err / photo_num), photo_num)

sigma = math.sqrt(err_sum / num)
rep_avg = sigma

return (rep_avg, photo_avg)


chunk = PhotoScan.app.document.chunk

threshold = PhotoScan.app.getFloat("Specify the reprojection error threshold:", 1.0)

if threshold > 0:
average, avg_photos = calc_reprojection(chunk)
print("Average reprojection error: " + str(average))

for camera in chunk.cameras:
if avg_photos[camera.label][0] > threshold:
camera.enabled = False
print("Disabled camera " + camera.label)
else:
print("Incorrect input. Threshold should be positive value.")
print("Script finished.")

It asks for the threshold value at script start and then disables the cameras with the average reprojection error above the input threshold.
Best regards,
Alexey Pasumansky,
Agisoft LLC

maddin

  • Full Member
  • ***
  • Posts: 161
    • View Profile
Re: Disabling camera with large error
« Reply #4 on: February 02, 2017, 03:26:39 PM »
Hello maddin,

I think you can use the following script:

Wow, thank you very much. I would not have expected this to require calculating all the errors in code, I was rather looking at the values already reported in the Reference tab.

But that code will be fine, too. Thanks again!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Disabling camera with large error
« Reply #5 on: February 02, 2017, 03:55:07 PM »
Hello maddin,

Those values in the Reference pane are not stored in the project data and cannot be accessed so easily.
Best regards,
Alexey Pasumansky,
Agisoft LLC

maddin

  • Full Member
  • ***
  • Posts: 161
    • View Profile
Re: Disabling camera with large error
« Reply #6 on: February 02, 2017, 04:48:17 PM »
I get an KeyError in line 55 when processing the second camera in the list:
Quote
if avg_photos[camera.label][0] > threshold:

Looks like the key is not in the dictionary 'avg_photos' for some reason?
Maybe it's because there are non-aligned cameras in the chunk?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Disabling camera with large error
« Reply #7 on: February 02, 2017, 04:56:50 PM »
Yes, it seems, so, as avg_photos dictionary contains only aligned cameras. So you can add a check "if camera.label in avg_photos.keys()".
Best regards,
Alexey Pasumansky,
Agisoft LLC