Forum

Author Topic: Importing Cameras with Custom Function  (Read 5128 times)

dret

  • Newbie
  • *
  • Posts: 3
    • View Profile
Importing Cameras with Custom Function
« on: February 26, 2020, 10:53:31 AM »
Hi

I'm trying to import the camera poses using a python script. As far as I understand, it is possible to change the reference coordinates of a camera by editing the location and rotation attributes of the camera's reference. But this leaves the camera unaligned.
However, directly setting the camera's center seems impossible, as it generates an error that this attribute is not writable. It was mentioned in the answer to this question (https://www.agisoft.com/forum/index.php?topic=1333.0) that those values are not easily accessible. But since it is possible to import camera poses from various formats, i guess that there is a way to edit these attributes.

I'm aware that I could generate an XML- or a bundler output file containing the camera poses. But since I have to implement this conversion first, i would prefer to write a script that directly writes the values into my photoscan project without any detours using temporary files. Is there any possibility to do this?

Cheers,
Daniel

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15465
    • View Profile
Re: Importing Cameras with Custom Function
« Reply #1 on: February 26, 2020, 09:07:57 PM »
Hello Daniel,

The location and orientation of the aligned camera in 3D is defined by chunk.transform matrix which has rotation 3x3 block and translation vector in the last column.
Best regards,
Alexey Pasumansky,
Agisoft LLC

dret

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Importing Cameras with Custom Function
« Reply #2 on: March 04, 2020, 03:30:47 PM »
Hi Alexey

Thanks for your answer. I was able to set the exterior orientation of the cameras this way.
But this leads to my next issue:

I try to set the internal orientation of the camera using this snippet:

Code: [Select]
    cam = chunk.addSensor()
    cam.type = Metashape.Sensor.Frame
    cam.pixel_width = float(values['SensorSizeMMX'])/float(values['SensorSizePIXX'])
    cam.pixel_height = float(values['SensorSizeMMY'])/float(values['SensorSizePIXY'])
    cam.focal_length = float(values['FocalLenghtMM'])

    cam.calibration = Metashape.Calibration()
    cam.calibration.height = int(values['SensorSizePIXY'])
    cam.calibration.width = int(values['SensorSizePIXX'])
    cam.calibration.f = float(values['FocalLengthPIX'])
    cam.calibration.cx = float(values['PPX'])
    cam.calibration.cy = float(values['PPY'])
    cam.fixed = True
    cam.label = values['label']

Followed by assigning the previously defined sensor to the cameras:

Code: [Select]
    for camera in chunk.cameras:
        camera.sensor = cam

When running the script, this works fine. Even if I query some parameters of the calibration after assigning, they are outputted as expected. But when I open the created PSZ file in the normal MetaShape GUI, the camera calibration values have disappeared, as shown in the attached screenshot. The labels are displayed, showing that the assingment of two different cameras has worked. But all the value fields in the calibration dialog contain no value or 0.

Am I doing something completely wrong or are there just some mandatory values I haven't set, that need tp be set for correctly reloading the calibration?

Thanks in advance,
Daniel


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15465
    • View Profile
Re: Importing Cameras with Custom Function
« Reply #3 on: March 08, 2020, 06:52:00 PM »
Hello Daniel,

Please try to adjust your code in the following way:
Code: [Select]
    cam = chunk.addSensor()
    cam.type = Metashape.Sensor.Frame
cam.width = int(values['SensorSizePIXX'])
cam.height = int(values['SensorSizePIXY'])
    cam.pixel_width = float(values['SensorSizeMMX'])/float(values['SensorSizePIXX'])
    cam.pixel_height = float(values['SensorSizeMMY'])/float(values['SensorSizePIXY'])
    cam.focal_length = float(values['FocalLenghtMM'])

    calibration = Metashape.Calibration()
    calibration.height = int(values['SensorSizePIXY'])
    calibration.width = int(values['SensorSizePIXX'])
    calibration.f = float(values['FocalLengthPIX'])
    calibration.cx = float(values['PPX'])
    calibration.cy = float(values['PPY'])
cam.user_calib = calibration
cam.fixed = True
    cam.label = values['label']

So I have added the definition of width and height for the calibration group (sensor) and modified the precalibrated parameters loading method.
Best regards,
Alexey Pasumansky,
Agisoft LLC

dret

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Importing Cameras with Custom Function
« Reply #4 on: March 09, 2020, 01:20:33 PM »
Works like a charm. Thanks a lot!