Forum

Author Topic: Reset adjusted camera calibration parameters with python  (Read 7527 times)

kbarn

  • Newbie
  • *
  • Posts: 5
    • View Profile
Reset adjusted camera calibration parameters with python
« on: September 25, 2019, 03:14:04 AM »
I have a set of 10 projects in which I would like to re-start the camera optimization process.
I have twelve projects, each with at least 6 sensors, thus I would like to accomplish resetting the cx, cy, k1-4, b1, b2, and p1-4 to 0.0 with python instead of by hand.

I have tried out a couple of guesses at how to do this, but have not been successful.

I would expect that the following would work, but did not find that it changed the values. Any recommendations are welcome.

Code: [Select]
for file in files:
    doc = Metashape.Document()
    doc.open(file)
    for chunk in doc.chunks:
        for sensor in chunk.sensors:
            sensor.calibration.cx = 0
            sensor.calibration.cy = 0.
            sensor.calibration.b1 = 0.
            sensor.calibration.b2 = 0.
            sensor.calibration.k1 = 0.
            sensor.calibration.k2 = 0.
            sensor.calibration.k3 = 0.
            sensor.calibration.k4 = 0.
            sensor.calibration.p1 = 0.
            sensor.calibration.p2 = 0.
            sensor.calibration.p3 = 0.
            sensor.calibration.p4 = 0.
    doc.save()

thank you,
katy

Paulo

  • Hero Member
  • *****
  • Posts: 1456
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #1 on: September 25, 2019, 03:49:17 AM »
Katy,

maybe try this:
Code: [Select]
    for chunk in doc.chunks:
        for sensor in chunk.sensors:
            calibration = sensor.calibration
            calibration.cx = 0
            calibration.cy = 0.
            calibration.b1 = 0.
            calibration.b2 = 0.
            calibration.k1 = 0.
            calibration.k2 = 0.
            calibration.k3 = 0.
            calibration.k4 = 0.
            calibration.p1 = 0.
            calibration.p2 = 0.
            calibration.p3 = 0.
            calibration.p4 = 0.
            sensor.calibration = calibration
Best Regards,
Paul Pelletier,
Surveyor

kbarn

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #2 on: September 25, 2019, 06:37:59 PM »
Thanks for the recommendation Paul. I tried it and it also didn't work. The other idea I thought to try is below (it also didn't work, but raised a TypeError).

Code: [Select]
for file in files:
    doc = Metashape.Document()
    doc.open(file)
    for chunk in doc.chunks:
        for sensor in chunk.sensors:
            calib = Metashape.Calibration
            calib.f = sensor.calibration.f
            calib.cx = 0.
            calib.cy = 0.
            calib.b1 = 0.
            calib.b2 = 0.
            calib.k1 = 0.
            calib.k2 = 0.
            calib.k3 = 0.
            calib.k4 = 0.
            calib.p1 = 0.
            calib.p2 = 0.
            calib.p3 = 0.
            calib.p4 = 0.
            sensor.calibration = calib
    doc.save()

This raises the following error (I'm running Metashape 1.5.4)

Code: [Select]
TypeError: expected  Metashape.Calibration object

Paulo

  • Hero Member
  • *****
  • Posts: 1456
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #3 on: September 25, 2019, 07:08:11 PM »
Katy,

maybe replace
Code: [Select]
calib = Metashape.Calibration by
Code: [Select]
calib = Metashape.Calibration()
Best Regards,
Paul Pelletier,
Surveyor

kbarn

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #4 on: September 25, 2019, 08:33:04 PM »
Thanks again for your recommendations Paul.

That did fix the TypeError... but didn't fix the underlying issue.


Paulo

  • Hero Member
  • *****
  • Posts: 1456
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #5 on: September 26, 2019, 03:05:25 AM »
Katy,

funny but following script does do set all camera calibration  parameters to 0 , except f
Code: [Select]
import Metashape as PhotoScan
doc = PhotoScan.app.document
chunk = doc.chunk
print("Script started")


for sensor in chunk.sensors:
calibration = sensor.calibration
calibration.cx = 0
calibration.cy = 0
calibration.p1 = 0
calibration.p2 = 0
calibration.b1 = 0
calibration.b2 = 0
calibration.k1 = 0
calibration.k2 = 0
calibration.k3 = 0
calibration.k4 = 0
sensor.calibration = calibration


print("ok")

see attached screen capture...
Best Regards,
Paul Pelletier,
Surveyor

kbarn

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #6 on: September 26, 2019, 03:22:00 AM »
Hi Paul,

I just tried out your script and it does work for me! Not entirely sure why, but no matter.

Thanks for your help!
Katy

kbarn

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #7 on: September 27, 2019, 09:32:45 PM »
A final update for the hive-mind, I found a way that allowed me to use Paul's recommendations and loop through files. This worked for me with version 1.5.4. Thanks again Paul for your recommendations!

Code: [Select]
for file in files:
   
    Metashape.app.document.open(file)

    doc = Metashape.app.document

    chunk = doc.chunk # could add another loop here if more than one chunk.

    for sensor in chunk.sensors:
        calibration = sensor.calibration
        calibration.cx = 0
        calibration.cy = 0
        calibration.p1 = 0
        calibration.p2 = 0
        calibration.p3 = 0
        calibration.p4 = 0
        calibration.b1 = 0
        calibration.b2 = 0
        calibration.k1 = 0
        calibration.k2 = 0
        calibration.k3 = 0
        calibration.k4 = 0
        sensor.calibration = calibration

  doc.save()