Forum

Author Topic: Python Script to calculate platform speed  (Read 8202 times)

nickgeo

  • Newbie
  • *
  • Posts: 13
    • View Profile
Python Script to calculate platform speed
« on: May 17, 2016, 03:51:11 PM »
Hello everyone,

I am new to python so I need any help provided to achieve the following. I want to create and make use of a script that after the image alignment and markers' placement, will be able to calculate the platform's speed between two consecutive frames by using the estimated camera locations. I would use the UAV's GPS antenna, alas it is custom-made and cannot produce geolocated images. As I think about it, it should be able to calculate the vector distance between two consecutive frames and then divide it with the time between the two captures -by a known, fixed timer setting by the camera, e.g. 5 secs-.

Thank you very much in advance!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15465
    • View Profile
Re: Python Script to calculate platform speed
« Reply #1 on: May 18, 2016, 01:35:13 PM »
Hello nickgeo,

You can access the estimated coordinates of the actual camera using the following code:

Code: [Select]
chunk = PhotoScan.app.document.chunk #active chunk
camera = chunk.cameras[0] #first camera in the Workspace pane list
coords = chunk.crs.project(chunk.transform.matrix.mulp(camera.center))
It should work for the chunk that is referenced in the geographic coordinate system (not local) for aligned camera.
So I think that getting coordinates for the two neighboring cameras and taking the length of the vector between them will solve the task:
Code: [Select]
distance = (coord1 - coord2).norm()
Best regards,
Alexey Pasumansky,
Agisoft LLC

nickgeo

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Python Script to calculate platform speed
« Reply #2 on: May 18, 2016, 04:12:03 PM »
Hello nickgeo,

You can access the estimated coordinates of the actual camera using the following code:

Code: [Select]
chunk = PhotoScan.app.document.chunk #active chunk
camera = chunk.cameras[0] #first camera in the Workspace pane list
coords = chunk.crs.project(chunk.transform.matrix.mulp(camera.center))
It should work for the chunk that is referenced in the geographic coordinate system (not local) for aligned camera.
So I think that getting coordinates for the two neighboring cameras and taking the length of the vector between them will solve the task:
Code: [Select]
distance = (coord1 - coord2).norm()
Thank you very much Alexey! Can you also provide me with a sample code on how to select two consecutive camers (probably with a for loop I suspect)?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15465
    • View Profile
Re: Python Script to calculate platform speed
« Reply #3 on: May 18, 2016, 05:30:50 PM »
Hello nickgeo,

Are you going to use only two cameras for the speed estimation, or you'll be averaging the values for every neighboring pair?
Best regards,
Alexey Pasumansky,
Agisoft LLC

nickgeo

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Python Script to calculate platform speed
« Reply #4 on: May 23, 2016, 12:20:22 PM »
Hello nickgeo,

Are you going to use only two cameras for the speed estimation, or you'll be averaging the values for every neighboring pair?
Hello Alexey,

I will be using every camera in the chunk and every pair of cameras for the speed estimation. To be specific I will give you an example. If there are 5 cameras in the chunk, I will use the 1st and 2nd to find the speed of the 1st, then the 2nd and the 3rd to find the speed of the 2nd and so on. But for the last camera I will use the speed of the one before that (the speed of the 4th camera in this example).

Thank you very much in advance.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15465
    • View Profile
Re: Python Script to calculate platform speed
« Reply #5 on: May 23, 2016, 07:18:58 PM »
Hello nickgeo,

Please try the following script:
Code: [Select]

import PhotoScan

chunk = PhotoScan.app.document.chunk #active chunk
T = chunk.transform.matrix
crs = chunk.crs
dist_list = list()

for i in range(0, len(chunk.cameras) - 1):
camera1 = chunk.cameras[i]
camera2 = chunk.cameras[i + 1]

if not (camera1.transform and camera2.transform):
print("Not possible to estimate distance between {:s} and {:s}".format(camera1.label, camera2.label))
continue

if crs:
coords1 = crs.project(T.mulp(camera1.center))
coords2 = crs.project(T.mulp(camera2.center))
else:
coords1 = T.mulp(camera1.center)
coords2 = T.mulp(camera2.center)

distance = (coords1 - coords2).norm()

dist_list.append((camera1, distance))

print(dist_list)

It should return the list where each element is a corresponding pair of the camera instance and the distance from this camera to the next one in the list. In case at least one of the neighboring cameras is not aligned such pair is skipped.
Best regards,
Alexey Pasumansky,
Agisoft LLC

nickgeo

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Python Script to calculate platform speed
« Reply #6 on: May 23, 2016, 11:37:56 PM »
Hello Alexey,

You are amazing! Millions of kudos!