Algorithm Search⌘K

Problemadvanced

Image matting

Also called alpha matting.

Recover the opacity alpha of the foreground at each pixel, and often its color, from an image that is a blend alpha F + (1 - alpha) B and a trimap or scribbles.

Time
O(n^3) worst
Where
n = the number of pixels

Image matting is the problem of pulling a soft foreground out of an image. Each pixel color II is modeled as a blend I=αF+(1−α)BI = \alpha F + (1 - \alpha) B of a foreground color FF and a background color BB, with an opacity α∈[0,1]\alpha \in [0, 1]. The task is to find α\alpha, and usually FF, at each pixel, so that the foreground can be put on a new background with compositing. Unlike a hard mask from interactive segmentation, the matte keeps hair, fur, smoke, motion blur and the mixed pixels on every edge. With 3 equations and 7 unknowns per color pixel, the problem has no unique answer without more input. That input is a known backing color, two shots against two backings, or a trimap or scribbles from a user together with an assumption about the image.

In plain words

When you cut out a person from a photo, the edge is not sharp. A strand of hair covers only part of a pixel, so the pixel color is a mix of the hair and the wall behind it. Matting finds, for each pixel, how much of it belongs to the person, from none to all, and what color the person has there.

One photo alone does not say this, because many mixes give the same color. So you need more help: a known wall color, two photos against two walls, or a rough map from the user that says "surely person", "surely wall" and "not sure".

Definition

The input is an image II with nn pixels and colors in R3\mathbb R^3. The model is the compositing equation

Ip=αpFp+(1−αp)Bp,αp∈[0,1],I_p = \alpha_p F_p + (1 - \alpha_p) B_p, \qquad \alpha_p \in [0, 1],

for each pixel pp. Porter and Duff (1984) used the same equation, the over operator, to put a foreground with an alpha channel on a background. Matting runs it the other way. The output is αp\alpha_p for each pixel, and in many uses also FpF_p (and BpB_p).

At each color pixel there are 3 equations and 7 unknowns (α\alpha, FF and BB), so the problem must have more input. The common forms are:

  • Known backing. BB is a known constant color, as in blue screen work.
  • Two backings. Two images of the same foreground against two known backings B1B_1 and B2B_2.
  • Trimap. The user marks each pixel as sure foreground (α=1\alpha = 1), sure background (α=0\alpha = 0) or unknown. Only the unknown band must be solved.
  • Scribbles. A few strokes of sure foreground and background, with all other pixels unknown.

An answer is judged against a ground truth matte, for example by the sum of absolute differences or the mean squared error over the unknown pixels. These scores are benchmark results.

Variants

  • Constant-color (blue screen) matting. One known backing BB. Smith and Blinn (1996) showed that this has no unique solution in general; it becomes solvable only with an extra rule on FF, such as "the foreground has no blue".
  • Triangulation matting. Two shots against two backings. This gives 6 equations for 4 unknowns (α\alpha and FF), and the solution is unique when the backings differ at the pixel (Smith and Blinn 1996).
  • Natural image matting. An arbitrary background, with a trimap or scribbles. This is the usual meaning of "image matting" today.
  • Video matting. A matte for each frame that also stays stable in time.
  • Environment and shadow matting. Extended models that also capture how the foreground bends or reflects the light from the background, or the shadow that it casts. They do not fit the equation above.

Examples

One backing is not enough. Let the backing be blue, B=(0,0,255)B = (0, 0, 255), and the observed pixel I=(120,24,126)I = (120, 24, 126). The pair α=0.6\alpha = 0.6, F=(200,40,40)F = (200, 40, 40) explains it, since 0.6F+0.4B=(120,24,24)+(0,0,102)0.6 F + 0.4 B = (120, 24, 24) + (0, 0, 102). So do α=1\alpha = 1 with F=IF = I, and α=0.8\alpha = 0.8 with F=(150,30,93.75)F = (150, 30, 93.75). Each α\alpha from 129/255≈0.506129/255 \approx 0.506 to 11 gives a valid FF with all channels in [0,255][0, 255], so the data cannot pick one.

Two backings fix it. Shoot the same pixel again against green, B2=(0,255,0)B_2 = (0, 255, 0), and get I2=(120,126,24)I_2 = (120, 126, 24). Subtracting the two equations removes FF:

I1−I2=(1−α)(B1−B2).I_1 - I_2 = (1 - \alpha)(B_1 - B_2).

Here I1−I2=(0,−102,102)I_1 - I_2 = (0, -102, 102) and B1−B2=(0,−255,255)B_1 - B_2 = (0, -255, 255), so 1−α=0.41 - \alpha = 0.4 and α=0.6\alpha = 0.6. In general, the least squares answer is α=1−(I1−I2)⋅(B1−B2)/∥B1−B2∥2\alpha = 1 - (I_1 - I_2) \cdot (B_1 - B_2) / \lVert B_1 - B_2 \rVert^2. Then αF=I1−(1−α)B1=(120,24,24)\alpha F = I_1 - (1 - \alpha) B_1 = (120, 24, 24), so F=(200,40,40)F = (200, 40, 40).

A trimap with a natural background. The closed-form matting page works through a small gray image with scribbles. It assumes that FF and BB are nearly constant in each small window, and then α\alpha is the solution of one sparse linear system.

Known algorithms

Method Input Main assumption Cost Kind of result
Triangulation (Smith and Blinn 1996) two backings the same FF and α\alpha in both shots O(n)O(n) exact under the model
Blue screen rules (Vlahos and others) one backing a rule on the colors of FF O(n)O(n) exact only if the rule holds
Bayesian matting (Chuang and others 2001) trimap FF and BB near Gaussian color clusters of nearby known pixels iterative per pixel local optimum of a posterior
Poisson matting (Sun and others 2004) trimap the gradient of α\alpha follows the image gradient sparse Poisson systems heuristic
Closed-form matting (Levin and others 2006) scribbles or trimap FF and BB nearly constant in each small window one sparse linear system global optimum of its cost
Guided filter refinement rough matte α\alpha is locally linear in the image O(n)O(n) heuristic
learned matting (2017 on) trimap, or none trained on composites one network pass learned, no guarantee

Wang and Cohen (2008) sort the classic methods into methods that sample colors of nearby known pixels, methods that spread α\alpha with affinities between pixels, and methods that combine both.

AlgorithmTimeSpacePropertiesCode
Closed-form mattingO(W^3 H^3) worst · averageO(W^2 H^2) extradeterministicexactglobal optimuminteractivesoft output1 language

Bounds

All bounds use the real-RAM model with nn pixels, a fixed window size and a fixed number of neighbors per pixel.

  • Without more input there is no unique answer. At each color pixel, 3 equations and 7 unknowns leave a 4-dimensional set of solutions, and with a known backing the set is still not a single point in general (Smith and Blinn 1996, first example above). This is a theorem about the model, not about any algorithm.
  • Two backings. If B1≠B2B_1 \ne B_2 at the pixel, α\alpha and FF are unique and cost O(1)O(1) per pixel, so O(n)O(n) in total. This is a theorem.
  • Closed-form matting. The cost is a quadratic form in α\alpha with a sparse, positive semidefinite Laplacian, so its minimum under the scribbles is a sparse linear system of size at most nn. Gaussian elimination on it costs O(n3)O(n^3) in the worst case, which is the bound in the front matter. With the grid structure, sparse direct solvers and conjugate gradients are much faster in practice; those times are benchmark results.
  • Lower bound. Each pixel of the unknown band must be read, so Ω(n)\Omega(n) in the worst case. This is a trivial theorem.

Reductions

  • From compositing. Matting inverts the over operator of Porter-Duff compositing; a good matte makes image compositing on a new background look right.
  • From segmentation. A hard mask from interactive segmentation, such as GrabCut, is eroded and dilated to give a trimap.
  • To a quadratic problem. Under the local color line model, FF and BB drop out and matting becomes the minimization of αTLα\alpha^T L \alpha under the scribbles; see closed-form matting.
  • To harmonic interpolation. With other pixel affinities in place of the matting Laplacian, the same system is a random walker with two labels.

Applications

  • Film and television. Blue and green screen keying, and pulling mattes of actors shot on real sets.
  • Photo editing. Cutting out people and objects with their hair for a new background, or to blur or adjust only the background.
  • Video calls. Replacing or blurring the background of a person in real time.
  • Data for learning. Composites of matted foregrounds on many backgrounds give training data for segmentation and matting networks.

References

  1. Thomas Porter and Tom Duff, Compositing digital images, SIGGRAPH 1984, 253-259, the alpha channel and the over operator
  2. Alvy Ray Smith and James F. Blinn, Blue screen matting, SIGGRAPH 1996, 259-268, the proof that matting against one known backing color has no unique solution in general, and triangulation with two backings
  3. Yung-Yu Chuang, Brian Curless, David H. Salesin and Richard Szeliski, A Bayesian approach to digital matting, CVPR 2001, vol. II, 264-271, the trimap, and the maximum a posteriori estimate of F, B and alpha with color clusters
  4. Anat Levin, Dani Lischinski and Yair Weiss, A closed form solution to natural image matting, CVPR 2006, vol. 1, 61-68, Section 1, the count of 3 equations and 7 unknowns per color pixel
  5. Jue Wang and Michael F. Cohen, Image and video matting: a survey, Foundations and Trends in Computer Graphics and Vision 3(2), 97-175, 2008, the chapters on color sampling methods and on matting affinities
  6. Richard Szeliski, Computer Vision: Algorithms and Applications, 1st edition, Springer, 2011, Section 10.4, image matting and compositing

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml