Streamlining Your Agisoft Metashape Workflow with Python
Written on
Chapter 1: Introduction to Python in Metashape
Agisoft Metashape is an exceptional tool for generating high-resolution drone orthomosaics and intricate 3D point clouds and mesh models. Its user-friendly graphical user interface (GUI) is suitable for a variety of users. If you are just starting, Aleena Rayamajhi has put together a detailed article to help you navigate the standard GUI effectively.
Once you become accustomed to the interface, you may find that many tasks are repetitive. Automating such tasks is crucial for improving efficiency. Additionally, errors during processing, such as incorrect tie points or key point limits, can result in wasted time and incorrect outputs.
The Professional version of Metashape offers a Python API that can significantly enhance your workflow. If you haven't utilized it yet, now is the time! Implementing the Metashape Python API allows for batch processing of multiple projects and guarantees consistent, reproducible results by applying the same settings across tasks.
One of the major advantages of the Python API is the ability to save your project automatically after each processing step. This is particularly beneficial when processing large sets of images, which can take considerable time—sometimes even running overnight. Manual saving can be problematic, especially if your computer loses power unexpectedly. The Python API mitigates this risk by ensuring your work is saved regularly.
In this article, you will discover how to leverage Metashape's Python API to automate the initial alignment phase of your workflow. Before diving into coding, it's advisable to read the user manuals and properly configure Metashape for your setup. If you're a newcomer, I also recommend exploring Agisoft tutorials and watching relevant YouTube videos.
I highly suggest reviewing the USGS Open File Report on Processing Coastal Imagery in Agisoft Metashape. This report provides a thorough overview of the GUI and processing workflows tailored for drone aerial imagery in coastal settings. While some settings may not be applicable to other scenarios (such as turntables or multi-camera setups), aspects like GPU acceleration configuration and log file generation are universally relevant.
Chapter 1.1: Understanding Chunks
In Metashape, 'Chunks' serve as organizational units for cameras, tie points, reference data, and additional information. Both documents and chunks are accessible as objects in the Python API, with attributes that can be set or retrieved. Here’s a simple example:
doc = Metashape.app.document
for chunk in doc.chunks:
if chunk.label == 'chunk1':
# perform operations
When naming a chunk, ensure that the label makes sense as it will be stored as chunk.label. For instance, if this is your first run in a project, you might name the chunk 'run1'.
Adding Photos to a Chunk
To add photos to the 'run1' chunk, right-click on it, select Add → Add Photos, and navigate to your desired image directory. After selecting the images, click 'Open.'
Once the images are added, save your project. It’s important to establish a clear naming convention for your project files to simplify coding. For example, you might use a format like 20230828_mcourse.psx, where the date is structured as yyyymmdd.
Chapter 1.2: Configuring Camera Settings
If you're using images from a DJI Phantom 3 drone, Metashape automatically extracts GPS data from the EXIF information. If your camera's settings are not included, you can input them manually via Tools → Camera Calibration. After ensuring that all settings are correct, save your project and proceed to the coding phase.
Chapter 2: The Python Code
The following Python code enables you to configure the parameters for image alignment. These parameters correspond to specific settings found in the GUI:
match_accuracy = 1 # High accuracy
gen_pre = True # Enable generic preselection
ref_pre = True # Enable reference preselection
mask_filt = True # Filter points using masks
mask_tie = True # Apply masks to tie points
key_lim = 60000 # Limit of key points
tie_lim = 0 # Limit of tie points
This code also allows for camera optimization after the initial alignment. To disable this feature, set optimus = False.
Note:
- The project path/name is structured as a list to accommodate multiple projects.
- The chunk labels should also be in a list format to process multiple chunks.
# Example code snippet
Projs = ["/data2/metashape_course/20230828_mcourse.psx"]
clbls = ["run1"]
doc = Metashape.app.document
for proj in Projs:
doc.open(proj)
for clbl in clbls:
for chunk in doc.chunks:
if chunk.label != clbl:
continueprint(f"Now processing project <{proj}>...")
# Process the chunk...
doc.save() # Save after alignment
Chapter 3: Executing the Code in Metashape
To execute the code in Metashape, copy it into a Python or text editor and save it as 'metashape_align.py'. Adjust the project path in the Projs variable, then open Metashape and navigate to Tools → Run Script. Select your script and enter '-r' in the Arguments dialog before clicking 'Ok.'
If everything is set up correctly, Metashape will begin aligning your images!
Video Tutorials
To further assist in your learning, check out the following video tutorials:
This tutorial covers the basics of the Agisoft Metashape workflow.
This comprehensive tutorial explores advanced features in Agisoft Metashape, including cloud processing, mesh creation, and more.
Conclusion
By following the steps outlined in this guide, you should be able to automate the image alignment process in Agisoft Metashape using Python, enhancing both efficiency and accuracy in your workflow.