Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Image Matting

Image matting estimates pixel-level foreground transparency, preserving fine details like hair for natural cutouts and realistic background compositing.

Image matting is the process of estimating how much foreground contributes to each pixel in an image. Its output, an alpha matte, preserves partially transparent details such as individual hairs, fur, and soft edges so a subject can be placed naturally against another background. Also called alpha matting, this computer vision task supports precise cutouts and image compositing. Physical picture-framing mats and medical floor matting are separate meanings of "matting."

How an Alpha Matte Works#

An alpha matte is a single-channel image with values between 0 and 1: 0 represents background, 1 represents fully opaque foreground, and intermediate values represent partial foreground coverage or transparency. Alpha describes the pixel's mixture; a model's confidence describes certainty about a prediction. Fine hair can occupy only part of a pixel, producing an intermediate alpha value even when the strand itself is opaque.

The standard compositing relationship is image = alpha * foreground + (1 - alpha) * background. Matting estimates these components from an existing image; compositing combines the extracted foreground with a replacement background. Estimating foreground color also matters: retaining the original background's color around hair creates colored fringes in the new composite.

Image segmentation assigns pixels to regions. Semantic segmentation assigns class labels, while instance segmentation separates individual objects. Matting estimates continuous foreground opacity. Ultralytics YOLO26 segmentation supports object isolation by turning instance masks into transparent cutouts; those masks can provide an initial foreground outline for a separate matting refinement stage.

Trimaps and Matting Methods#

A trimap supplies three regions: known foreground, known background, and an unknown region containing boundaries or transparent material. This form of data annotation constrains the estimation problem. Like interactive segmentation, it lets users guide extraction through a small amount of input. Incorrectly marking hair as definite background forces its alpha to zero, removing it from the cutout.

Closed-form matting, introduced by Anat Levin, Dani Lischinski, and Yair Weiss in 2006 through A Closed Form Solution to Natural Image Matting, uses local foreground and background color assumptions to construct a sparse linear system. Solving that system estimates alpha throughout the unknown region.

Deep learning methods learn appearance and boundary patterns from labeled examples. Ning Xu, Brian Price, Scott Cohen, and Thomas Huang introduced Deep Image Matting in 2017, combining a convolutional neural network encoder-decoder with refinement. Its Adobe matting data and Composition-1k test set became established evaluation resources. Compositing foregrounds onto varied backgrounds expands training data through data augmentation.

Estimating a Matte in Python#

The PyMatting alpha-estimation API implements closed-form matting. Install it with pip install pymatting pillow, then use foreground and background seed regions on the sample image. Here, gray trimap values leave pixels for the solver to estimate.

from urllib.request import urlopen

import numpy as np
from PIL import Image
from pymatting import estimate_alpha_cf

with urlopen("https://ultralytics.com/images/bus.jpg") as response:
    photo = Image.open(response).convert("RGB")
photo.thumbnail((256, 256))
image = np.asarray(photo, dtype=np.float64) / 255.0

# Mark sky as background and a patch of the bus as foreground.
trimap = np.full(image.shape[:2], 0.5)
trimap[:20, :] = 0.0
trimap[120:145, 115:145] = 1.0

alpha = estimate_alpha_cf(image, trimap)
matte = Image.fromarray(np.uint8(np.clip(alpha, 0, 1) * 255))
matte.save("alpha_matte.png")

The saved grayscale image shows the estimated foreground contribution: white at foreground seeds, black at background seeds, and estimated transitions elsewhere.

Applications and Quality Evaluation#

Portrait editing, product photography, and film compositing use mattes to preserve fine boundaries during background replacement. Portrait video matting extends this operation across frames, where unstable boundaries produce visible flicker. MODNet provides a verifiable example of trimap-free portrait extraction from RGB input for real-time inference. Image inpainting instead synthesizes content inside a selected region.

Matting evaluation compares predicted alpha values with reference mattes. The perceptually motivated matting benchmark, introduced by Christoph Rhemann and colleagues in 2009, uses sum of absolute differences, mean squared error, gradient error, and connectivity error. These complement region-overlap measures such as Intersection over Union: gradient error measures discrepancies in alpha transitions, while connectivity error measures disruptions to connected foreground structure.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision in sports
Computer vision in sports
Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery
Computer vision for aerial imagery
Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace
Computer vision in aerospace
Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security
Computer vision in security
Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics
Computer vision in robotics
Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics
Computer vision in logistics
Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail
Computer vision in retail
Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare
Computer vision in healthcare
Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing
Computer vision in manufacturing
Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive
Computer vision in automotive
Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture
Computer vision in agriculture
Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision in sports
Computer vision in sports
Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery
Computer vision for aerial imagery
Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace
Computer vision in aerospace
Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security
Computer vision in security
Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics
Computer vision in robotics
Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics
Computer vision in logistics
Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail
Computer vision in retail
Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare
Computer vision in healthcare
Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing
Computer vision in manufacturing
Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive
Computer vision in automotive
Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture
Computer vision in agriculture
Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning