Forum

Author Topic: Reset adjusted camera calibration parameters with python  (Read 7953 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: 1500
    • 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: 1500
    • 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: 1500
    • 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()

caipi

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #8 on: May 13, 2025, 11:55:13 AM »
Same issues, unfortunately, does not seem to work for Metashape >= 2.1.4. It seems like Agisoft resets it after reopening the project... Here is my code:

```
doc = ms.Document()

doc.open(path=file, read_only=False)

for chunk in doc.chunks:
    for sensor in chunk.sensors:
        calibration = sensor.calibration

        print(calibration.cx)
        calibration.cx = 0.0
        print(calibration.cx)

        sensor.calibration = calibration
        print(sensor.calibration.cx)

        doc.save()
```

This gives 21.505 with 0 and 0 for the the three print statements... However, when resetting cx in the UI everything is fine. Do I need to call some save() to the calibration itself?
« Last Edit: May 13, 2025, 12:48:49 PM by caipi »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15438
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #9 on: May 13, 2025, 04:10:45 PM »
Hello caipi,

Can you please check, if adding the following line before doc.save() line in posted loop helps:

Code: [Select]
sensor.meta["Calibration"] = "cx=0"
Best regards,
Alexey Pasumansky,
Agisoft LLC

caipi

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #10 on: May 13, 2025, 06:53:33 PM »
Did not fix it, however, I figured it.

Sometimes, I do not know yet why, Agisoft lies in a state where the "Adjusted Calibration" tab is greyed. Somehow, it did yield always 27.505... although the tab was not active and accessible...
,
My main goal was also actually not to reset anything, but watch the k4, b1 and b2 parameters to check, if "Optimize cameras" has been run by the user before, or if this step is still open.

Thanks still for the answer.


PS: meta did contain some stuff, however, nothing related to the Calibration. Its just a dict with some sensor info about its name...

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15438
    • View Profile
Re: Reset adjusted camera calibration parameters with python
« Reply #11 on: May 14, 2025, 01:00:38 PM »
Hello caipi,

I have suggested to modify sensor meta in order to force trigger for Metashape that sensor was modified, so all info about it should be re-written when the document is saved.

Adjusted tab in GUI may be disabled if no cameras corresponding to the sensor are aligned, or if all calibration parameters are fixed.
Best regards,
Alexey Pasumansky,
Agisoft LLC