How to Automatically Mask Backgrounds in Agisoft Metashape

How to Automatically Mask Backgrounds in Agisoft Metashape

One of the most effective ways to improve model quality and reduce noise in Agisoft Metashape is by removing unwanted backgrounds from images. While manual masking is tedious, Metashape offers powerful options for automatic background masking. This article explains the available methods, step-by-step workflows, and how to automate masking with Python scripts.

Why Mask Backgrounds?

  • Reduce reconstruction noise caused by cluttered or irrelevant backgrounds
  • Improve alignment accuracy, especially in controlled environments
  • Speed up dense cloud generation by limiting processing to key areas

Method 1: Using Background Masking from Background Image

If you have a photo of the background without the object (for example, a turntable studio or blank background), Metashape can automatically subtract the background by comparison.

  1. Select all the photos that need masking.
  2. Go to Tools → Import Masks.
  3. Choose “From Background” as the method.
  4. Set the path to the background image (same resolution, same camera).
  5. Apply and review the generated masks.

This method works best in static setups where lighting and perspective do not change significantly.

Method 2: Using Color Range for Masking

If your background is uniform (e.g., a green screen or white paper), you can mask based on color.

  1. Select the photo(s).
  2. Use the Magic Wand Tool in the Masking toolbar.
  3. Click on the background area. Adjust tolerance as needed.
  4. Click Add Selection to turn the area into a mask.
  5. Apply the mask to all photos using Tools → Apply Masks to All Cameras.

For batch workflows, this approach can be tedious unless automated (see Python section below).

Method 3: Using Masking from Model

If you already have a rough model, you can use it to generate masks for the original images by projecting the 3D geometry onto the photos:

  1. Build a sparse or dense model.
  2. Go to Tools → Import Masks.
  3. Select “From Model” and choose “Keep Key Area Only”.
  4. Metashape will mask all areas not covered by the model projection.

This method is particularly helpful for eliminating floating background points from early-stage models.

Method 4: Using Background Masks via Python Automation

For large projects or consistent studio setups, Python scripting allows you to automate masking across hundreds of photos.

import Metashape

doc = Metashape.app.document
chunk = doc.chunk

background_path = "C:/background.jpg"

for camera in chunk.cameras:
    chunk.importMask(path=background_path,
                     source=Metashape.MaskSourceBackground,
                     operation=Metashape.MaskOperationReplacement,
                     cameras=[camera])

This script imports a background image and generates masks for all cameras using it. You can modify it to work with color-based masks too.

Exporting and Reusing Masks

You can save time by exporting your masks for future projects:

  • Go to File → Export Masks
  • Choose the format: PNG, BMP, or TIFF
  • Reload in future sessions via Import Masks → From File

Best Practices

  • Use consistent lighting to avoid false mask edges
  • Avoid shadows or reflections on background surfaces
  • Don’t use lossy compressed background images
  • Verify masks in the “Mask View” for each camera

Masking Limitations

  • Masks are resolution-dependent: a slight shift in camera position can break the mask
  • Color masking is less effective with textured or complex backgrounds
  • Masking only applies to certain processing steps (e.g., dense cloud, model), not tie point generation

Use Cases for Background Masking

  • Product 3D scanning with turntables
  • Museum object digitization
  • Lab research photogrammetry with controlled lighting
  • Removing trees or structures in aerial photos

Conclusion

Automatic background masking in Agisoft Metashape allows you to streamline your reconstruction process, improve the accuracy of your models, and reduce time spent on manual edits. Whether you’re working with controlled object scans or noisy aerial datasets, mastering masking tools can significantly enhance your results. Use the built-in options or automate with Python to scale your workflow to new levels.