PIXELBANKv9.1.0
Menu

Solve Matting Equation for Alpha

Solve the matting equation for unknown alpha given observed color and known foreground/background.

The matting equation states that an observed pixel is a blend of foreground and background:

I=αF+(1−α)BI = \alpha F + (1 - \alpha) B

If we know F (foreground color) and B (background color), we can solve for alpha:

α=I−BF−B\alpha = \frac{I - B}{F - B}

In practice, we use the channel with the largest |F - B| difference for numerical stability, as channels where F ≈ B provide unreliable estimates.

This is used in chroma keying (green screen) where F is known (subject) and B is known (green screen).

Example:

Input:
solve_alpha((128, 50, 50), (255, 100, 100), (0, 0, 0))
Output:
0.502
Reasoning:

Finding alpha from RGB channels: Channel differences |F - B|:

  • R: |255 - 0| = 255 (largest - use this)
  • G: |100 - 0| = 100
  • B: |100 - 0| = 100

Using red channel: α = (128 - 0) / (255 - 0) = 128/255 ≈ 0.502

Constraints:

  • observed, foreground, background: RGB tuples [0, 255]
  • Return alpha clamped to [0, 1] and rounded to 4 decimal places
  • Use the channel with largest |F - B| for best accuracy
🔒

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

0/0
Run code to see test results.
Solve Matting Equation for Alpha - Medium | PixelBank