Forum

Author Topic: sensor calibration via python API  (Read 2047 times)

MartinKobe

  • Newbie
  • *
  • Posts: 6
    • View Profile
sensor calibration via python API
« on: May 24, 2024, 08:04:30 PM »
Hey guys,

Iam trying to set calbration for two sensors installed in an streoscopic setup:

Code: [Select]
   
       # filling up number of sensors according to number of cameras
        while len(project.chunk.sensors) != len(project.chunk.cameras):
            project.chunk.addSensor()
       
        # set parameters from sensor calibration file content       
        for camera in project.chunk.cameras:
   
            sensor              = camera.sensor
            sensor.label        = camera.label
            camKey              = sensor.label.split('_')[-1]
            sensor.type         = Metashape.Sensor.Type.Frame
            sensor.focal_length = config['sensor'][camKey]['constant']
            sensor.pixel_height = config['sensor'][camKey]['pix_size']
            sensor.pixel_width  = config['sensor'][camKey]['pix_size']
           
            princ_x             = config['sensor'][camKey]['princ_x']
            princ_y             = config['sensor'][camKey]['princ_y']
            focal_len           = config['sensor'][camKey]['constant']
            pixel_size          = config['sensor'][camKey]['pix_size']
           
            cal                 = Metashape.Calibration()
            cal.width           = config['sensor'][camKey]['width']
            cal.height          = config['sensor'][camKey]['height']
            cal.cx              = princ_x/pixel_size
            cal.cy              = princ_y/pixel_size
            cal.f               = focal_len/pixel_size

            sensor.user_calib        = cal
            sensor.fixed_calibration = True

until here, everything works obviously fine. console output:
Quote
for cam in StereoProject.doc.chunk.cameras:

    print(f'camera:       {cam.label}\n'
          f'focal_length: {cam.sensor.focal_length}\n'
          f'pixel_size:   {cam.sensor.pixel_size}\n'
          f'cx (px):      {cam.sensor.calibration.cx}\n'
          f'cy (px):      {cam.sensor.calibration.cy}\n'
          f'f (px):       {cam.sensor.calibration.f}\n')
         
camera:       distorted_stereoCam_E0
focal_length: 15.90096
pixel_size:   Vector([0.00345, 0.00345])
cx (px):      -24.07246376811594
cy (px):      22.217391304347824
f (px):       4608.973913043478

camera:       distorted_stereoCam_E1
focal_length: 15.96475
pixel_size:   Vector([0.00345, 0.00345])
cx (px):      -11.518840579710144
cy (px):      27.91594202898551
f (px):       4627.463768115942

and now comes the beloved blackbox-moment.

I save the project via API using something like
Quote
project.doc.chunk.save()
and then I open it with the GUI.

Please have a look at the attachment (as I dont know how to embedd an image here).
As you can see, everything was saved but of the resolution, focal length and pixel size of only one camera....

I get desperate, pls help :-P Does anyone knows where my mistake is?

best,
Martin
« Last Edit: May 24, 2024, 08:08:39 PM by MartinKobe »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: sensor calibration via python API
« Reply #1 on: June 07, 2024, 01:36:17 PM »
Hello Martin,

On the screenshot I also see that the image related to the second sensor has 0x0 dimensions.

If you are adding new sensors to the project, you need to define sensor.width and sensor.height according to the image dimensions in the corresponding calibration group, like you are doing for the calibration.
Can you please try fixing the width&height issue for the newly created sensors first and check, if it also resolves the originally reported problem?
Best regards,
Alexey Pasumansky,
Agisoft LLC

MartinKobe

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: sensor calibration via python API
« Reply #2 on: June 17, 2024, 12:17:51 PM »
Hello Alexey,

Thank you for the reply. The problem could have been solved!

Best regards