Forum

Author Topic: more on Import and Export Cameras python commands  (Read 17349 times)

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
more on Import and Export Cameras python commands
« on: May 08, 2014, 06:49:11 PM »
I am using a python procedure to export  4 pre-calibrated and aligned cameras in a file named c01_c04.xml, and their  spatial positions and orientations in another file named c01_c04.txt.
The spatial positions are calculated using targets having known coordinates.
I wish to use the same cameras for other projects, using the tool "Import Cameras" to import the c01_c04.xml file, and the "Ground control" Import to load the c01_c04.txt file, because I wish to align the photos using the ground control option.
Unfortunately the "ground_control.save" python command exports only the estimated camera positions, and if I try to use, as in the attached examples,  the  "ground_control.load" to import the saved positions, Photoscan does not read the values. To obtain the desired result, I am obliged to modify manually and externally the c01_c04.txt file to write the estimated positions and orientations as real ones.
Is there any possibility available to write the c01_c04.txt file saving only the positions and orientations of the cameras to be able to use them in other projects? May be it could be possible to use other python general commands to write in the correct form this file.
Please, Alexey, could you help me?
Thank you very much.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #1 on: May 12, 2014, 04:19:12 PM »
Hello lmg,

So you need to generate text file using etimated camera locations to be loaded in the new projects/chunks using ground_control.load option? I'll post export code a little bit later, but using Ground Control pre-selection is not reasonable in four camera case., unless you need to reference the chunk using previously obtained coordinates.

Best regards,
Alexey Pasumansky,
Agisoft LLC

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #2 on: May 12, 2014, 06:05:48 PM »
Hello Alexey,
Yes, I need to generate text file using estimated camera locations to be loaded in the new projects/chunks using ground_control.load option.
Thank you for the code that you will post later. I have this done this manually, editing the estimated camera location coordinates file, and I can assure that the  use of Ground Control pre-selection is reasonable also in a four camera case, because I need to reference the chunk using previously obtained coordinates.
Best regards
LMG

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #3 on: May 21, 2014, 07:27:14 PM »
Hello Alexey,
Have you completed the code for exporting camera coordinates?
Bye

LMG

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #4 on: May 29, 2014, 11:41:59 AM »
Hello LMG,

I think this should be working as expected:

Code: [Select]
import PhotoScan

path = PhotoScan.app.getSaveFileName("Please specify export path and filename:")

file = open(path, "wt")
chunk = PhotoScan.app.document.activeChunk
if chunk.transform:
T = chunk.transform
else:
T = PhotoScan.Matrix().diag([1,1,1,1])
print("Script started")

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

coords = T.mulp(camera.center)
file.write(camera.label + "\t{:.5f}".format(coords[0]) + "\t{:.5f}".format(coords[1]) + "\t{:.5f}".format(coords[2]) + "\n")

file.close()
print("Script finished")
Best regards,
Alexey Pasumansky,
Agisoft LLC

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #5 on: June 03, 2014, 11:41:43 AM »
Thank you Alexey.
I am going to try your script.
Reading your script, it seems to me that only camera positions will be exported. Is it possible to export also the orientations?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #6 on: June 03, 2014, 11:55:13 AM »
Hello LMG,

Yes it's possible, but would it be reasonable, since the camera orientation angles are not utilized during processing?
Best regards,
Alexey Pasumansky,
Agisoft LLC

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #7 on: June 11, 2014, 01:24:46 PM »
If the camera orientations are not utilized during the processing, of course it is not useful to export them.
Thank you very much.

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #8 on: September 04, 2014, 12:23:16 AM »
Hello LMG,

I think this should be working as expected:

Code: [Select]
import PhotoScan

path = PhotoScan.app.getSaveFileName("Please specify export path and filename:")

file = open(path, "wt")
chunk = PhotoScan.app.document.activeChunk
if chunk.transform:
T = chunk.transform
else:
T = PhotoScan.Matrix().diag([1,1,1,1])
print("Script started")

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

coords = T.mulp(camera.center)
file.write(camera.label + "\t{:.5f}".format(coords[0]) + "\t{:.5f}".format(coords[1]) + "\t{:.5f}".format(coords[2]) + "\n")

file.close()
print("Script finished")

Hello Alexey,
I cannot import the cameras coordinates. Could you write the equivalent code to import the c01_c04.txt file ?
Thank you

LMG

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #9 on: September 09, 2014, 11:25:46 AM »
Hello LMG,

And does chunk.ground_control.load(path) option work?
Best regards,
Alexey Pasumansky,
Agisoft LLC

lmg

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #10 on: September 10, 2014, 07:59:26 PM »
Now the command doc.activeChunk.ground_control.load(file, "csv") works; it was a problem related to the format of the .txt file, that needs TAB to separate different fields.

oto

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #11 on: November 03, 2016, 10:57:28 AM »
Hello!

I updated the example code to also export orientation in OPK. Unfortunately after importing the exported cameras in other software the orientation is not correct for some cameras. Compared rotation matrix and it matches in both softwares. Could it be that orientation is wrong related to fact that photos in chunk are not made with same camera model?

Code: [Select]
#export cameras coordinates and orientation as omega,phi,kappa (OPK)
#compatibility: Agisoft PhotoScan Professional 1.2.6

import PhotoScan
import math

path = PhotoScan.app.getSaveFileName("Please specify export path and filename:")

file = open(path, "wt")
#chunk = PhotoScan.app.document.activeChunk
doc = PhotoScan.app.document
chunk = doc.chunk


if chunk.transform:
#T = chunk.transform
T = chunk.transform.matrix
else:
T = PhotoScan.Matrix().diag([1,1,1,1])


print("Script started")

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

coords = T.mulp(camera.center)
#x, y, z = T.mulp(camera.center)

#omega, phi, kappa = PhotoScan.utils.mat2opk(T.rotation)
m = chunk.transform.matrix * camera.transform * PhotoScan.Matrix().diag([1,-1,-1,1]) #camera transformation matrix has been multiplied by 180-degree rotation matrix, since the direction of Z axis in camera system is inverted compared to the world coordinate system.
rotation = m.rotation()
omega, phi, kappa = PhotoScan.utils.mat2opk(rotation)
print(camera.label)
print(rotation)
#print(omega, phi, kappa)
#print(x)

file.write(camera.label + "\t{:.5f}".format(coords[0]) + "\t{:.5f}".format(coords[1]) + "\t{:.5f}".format(coords[2]) + "\t{:.5f}".format(omega)+"\t{:.5f}".format(phi)+"\t{:.5f}".format(kappa)+"\n")
#yaw, pitch, roll = PhotoScan.utils.mat2ypr(PhotoScan.utils.opk2mat(PhotoScan.Vector((omega, phi, kappa))).t())

file.close()
print("Script finished")

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #12 on: November 04, 2016, 06:16:38 PM »
Hello oto,

Which task you are trying to solve? You can export cameras in OPK format straight away using Tools Menu -> Export Cameras or using "opk" format option in chunk.exportCameras() function.
Best regards,
Alexey Pasumansky,
Agisoft LLC

oto

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #13 on: November 08, 2016, 09:50:52 AM »
I would like to export camera positions and rotation to CSV file. Problem is that script works but for some reason the rotation for some cameras is wrong. Will try exportCameras function but still interesting why the rotation is wrong when imported in other software.  ???

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: more on Import and Export Cameras python commands
« Reply #14 on: November 08, 2016, 12:09:55 PM »
Hello oto,

Another application may be using different rotation order of omega, phi, kappa angles.
Best regards,
Alexey Pasumansky,
Agisoft LLC