Forum

Author Topic: Calibrating a specific Camera Model  (Read 2603 times)

bbbares

  • Newbie
  • *
  • Posts: 21
    • View Profile
Calibrating a specific Camera Model
« on: March 05, 2020, 06:46:22 PM »
I am writing a script that follows the general workflow guide laid out in the user manual. I have gotten everything to work except for camera calibration. Ideally I would like to load in all of the photos and then calibrate a specific camera model associated with many photos as opposed to calibrating a new camera connected to each photo added. I was wondering if this was possible and if so how this can be implemented.

Here is my in progress script:

Code: [Select]
# Import Metashape
try:
    import Metashape
except ImportError:
    import sys

    print("This library requires Agisoft Metashape")
    sys.exit(1)

# Import other libraries
from pathlib import Path

# Checking Version Compatibility
print("--- Checking version compatibility...")
compatible_major_version = "1.6"
found_major_version = ".".join(Metashape.app.version.split('.')[:2])
if found_major_version != compatible_major_version:
    raise Exception("Incompatible Metashape version: {} != {}".format(found_major_version, compatible_major_version))

# DEFINE PROCESSING SETTINGS
print("--- Defining processing settings...")

# Define: Source Path
SourcePath = Path.home().joinpath('Documents', 'A3M')

# Define: Project Prefix
ProjectPrefix = "Scripted"

# Define: Project Name
ProjectName = "Test1"

# Define: Image pattern
ImagePattern = "*.dng"

# INITIALIZE ENVIRONMENT
# Main app objects
doc = Metashape.app.document
chunk = doc.chunk


# Method to save project
def saveproject():
    print("--- Saving project...")
    doc.save()


# LOAD AND CALIBRATE PHOTOS
print("--- Loading images...")
# Find paths to photos
PhotoPaths = SourcePath.joinpath("Images", ProjectPrefix).rglob(ImagePattern)
# Load photos
Photos = list(map(str, PhotoPaths))
chunk.addPhotos(Photos)
# Calibrate cameras

# Psuedocode <------------------------------------------------------------------------------ Change this
# chunk.cameramodel.sensor = Metashape.Sensor.Fisheye

saveproject()

# ALIGN PHOTOS
print("--- Aligning photos...")
# print("Accuracy: " + AlignPhotosAccuracy)
chunk.matchPhotos()
chunk.alignCameras()
saveproject()

# BUILD DENSE CLOUD
print("--- Building Depth Maps...")
chunk.buildDepthMaps()
print("--- Building Dense Cloud...")
chunk.buildDenseCloud()
saveproject()

# BUILD MODEL
print("--- Building Model...")
chunk.buildModel()
saveproject()

# EXPORT MODEL
ModelPath = SourcePath.joinpath("Models", ProjectPrefix)
try:
    ModelPath.mkdir()
except FileExistsError:
    print("Duplicate directory found, no new directory made")
chunk.exportModel(path=str(ModelPath.joinpath(ProjectName + ".obj")))

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #1 on: March 05, 2020, 06:49:03 PM »
Hello bbbares,

What is the analogue of GUI operation for your task? Should it be putting all the images to the same calibration group and loading the calibration data from external file?
Best regards,
Alexey Pasumansky,
Agisoft LLC

bbbares

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #2 on: March 05, 2020, 07:03:10 PM »
To start I would just like to set the camera type to fisheye as opposed to the default frame value. At this point I don't have calibration files to load in. In the future I would use a variety of camera types such as one full frame and one fisheye. In that case I would like to load all of the images from each camera to the corresponding calibration group and then load the calibration data for each group. The end use case, if it matters, is interior modeling.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #3 on: March 08, 2020, 06:35:44 PM »
Hello bbbares,

The following code sample will switch the camera type for all the calibration groups to Fisheye:
Code: [Select]
for sensor in chunk.sensors:
    sensor.type = Metashape.Sensor.Type.Fisheye
Best regards,
Alexey Pasumansky,
Agisoft LLC

bbbares

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #4 on: March 09, 2020, 09:27:57 PM »
Perfect, Thanks!

What would it look like to add photos from two cameras (say "camera1" and "camera2") to two calibration groups ("group1" and "group 2") and then load the respective calibration files ("file1" and "file2")?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #5 on: March 11, 2020, 05:58:58 PM »
Hello bbbares,

How you are planning to split the calibration groups between camera1 and camera2? Should it be defined as input path lists or by any meta-information?
Best regards,
Alexey Pasumansky,
Agisoft LLC

bbbares

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #6 on: March 11, 2020, 08:57:35 PM »
Hi Alexey,

Everything will be defined by input paths.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Calibrating a specific Camera Model
« Reply #7 on: March 12, 2020, 07:12:32 PM »
Hello bbbares,

You can try the following script. It loads the images from the list (Photos1) then for the cameras represented in the list the new calibration group is created and pre-calibrated values are loaded.

Code: [Select]
chunk.addPhotos(Photos1)

new_sensor_created = False
for camera in chunk.cameras:
if camera.photo.path in Photos1:
if not new_sensor_created:
sensor = chunk.addSensor()
sensor.label = "group1" + camera.sensor.label
sensor.type = Metashape.Sensor.Type.Fisheye
sensor.focal_length = camera.sensor.focal_length
sensor.height = camera.sensor.height
sensor.width = camera.sensor.width
sensor.pixel_size = camera.sensor.pixel_size
calibration = Metashape.Calibration()
calibration.width = sensor.width
calibration.height = sensor.height
calibration.load(calib_file1, format = Metashape.CalibrationFormatXML)
sensor.user_calib = calibration
new_sensor_created = True
camera.sensor = sensor
Best regards,
Alexey Pasumansky,
Agisoft LLC