v9.1.1
Menu

Trimap-Based Alpha Estimation

hardPROImage MattingCVCV: Computational Photography

Implement a Trimap-Based Alpha Estimation algorithm to estimate the alpha matte from a given trimap, which divides an image into definite foreground, definite background, and unknown regions. The goal is to estimate the alpha values in the unknown region using color statistics.

The trimap is a mask that categorizes each pixel into one of three categories: definite foreground (trimap value 255255, α=1\alpha=1), definite background (trimap value 00, α=0\alpha=0), and unknown (any value strictly between, e.g. 128128; α\alpha to be estimated). This problem is a fundamental challenge in Image Matting, a technique used to separate the foreground from the background in an image.

  1. Identify the definite foreground and background regions from the trimap.
  2. Compute the color statistics, such as the mean color, for the foreground and background regions.
  3. Estimate the alpha value for each pixel in the unknown region using the computed color statistics.
α=dbgdfg+dbg\alpha = \frac{d_{bg}}{d_{fg} + d_{bg}}

where dfgd_{fg} and dbgd_{bg} are the Euclidean distances in RGB space (pixel values scaled to [0,1][0, 1]) from the pixel's color to the foreground and background mean colors. If a region is empty, use white (1,1,1)(1,1,1) as the foreground mean or black (0,0,0)(0,0,0) as the background mean.

Return the alpha matte as a 2D float array with the same height and width as the trimap, values in [0,1][0, 1].

This technique is widely used in image and video editing applications.

Example

Input
Image and trimap
Output
Alpha matte
Reasoning
  • Input: an 8×8×38 \times 8 \times 3 RGB image whose top 4 rows are white (255,255,255)(255,255,255) and bottom 4 rows black, plus an 8×88 \times 8 trimap: rows 0–2 are 255255 (foreground), rows 3–4 are 128128 (unknown), rows 5–7 are 00 (background).
  • Color means: scaled to [0,1][0,1], every foreground pixel is (1,1,1)(1,1,1) and every background pixel is (0,0,0)(0,0,0), so μfg=(1,1,1)\mu_{fg}=(1,1,1) and μbg=(0,0,0)\mu_{bg}=(0,0,0).
  • Row 3 (unknown, white): dfg=0d_{fg}=0, dbg=12+12+12=3d_{bg}=\sqrt{1^2+1^2+1^2}=\sqrt{3}, so α=30+3=1\alpha=\frac{\sqrt3}{0+\sqrt3}=1.
  • Row 4 (unknown, black): dfg=3d_{fg}=\sqrt3, dbg=0d_{bg}=0, so α=0\alpha=0.
  • Output: an 8×88 \times 8 alpha matte, shape (8, 8): rows 0–3 are 11, rows 4–7 are 00. The unknown band snaps to whichever mean its color matches.
  • Notice that in-between colors give fractional alpha: with a red foreground (1,0,0)(1,0,0), a blue background (0,0,1)(0,0,1) and an unknown pixel (200,50,0)/255(200,50,0)/255, dfg≈0.2915d_{fg}\approx0.2915 and dbg≈1.2859d_{bg}\approx1.2859, giving α≈0.8152\alpha\approx0.8152.

Constraints

  • Input parameters: image (3D numpy array, RGB), trimap (2D numpy array, integer values in {0, 1, 255} representing background, foreground, and unknown regions respectively)
  • Valid ranges: Pixel values in image are in [0, 255], trimap values are in {0, 1, 255}
  • Output format: Return alpha matte as 2D numpy array (float32) with values in [0, 1]
  • Special conditions: The input image and trimap are assumed to have the same height and width, and the trimap is a valid mask with no isolated unknown regions.

Learn this in

More from CV: Computational Photography

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.