Alpha Matte Compositing
Implement alpha compositing for foreground/background blending, a key technique in image matting. This process involves combining a foreground image with a background image using a matte, which is a grayscale image defining the opacity of the foreground.
The matte, represented by , is used to control the blending of the foreground and background colors. The compositing equation is based on the principle of linear blending, where the resulting color is a weighted sum of the foreground and background colors.
- Define the foreground, background, and matte images.
- Apply the compositing equation to each pixel.
Return the composited image with the same shape, stored as 8-bit unsigned integers (uint8). Fractional values are truncated, not rounded: becomes .
This technique is widely used in digital image editing.
Example
foreground = person image background = beach scene alpha = person matte (1 inside, 0 outside)
Person composited onto beach
- Inputs:
foregroundandbackgroundare RGB images with values 0 to 255;alphais an matte, one opacity per pixel: on the person, on the beach, fractional along hair and soft edges. - Every pixel follows , with that pixel's single weighting all three colour channels.
- Take one edge pixel: red foreground , green background , . Then , and stored as 8-bit integers with the fraction dropped it becomes , a dark yellow halfway between.
- With that pixel is exactly ,
[255, 0, 0]; with it is exactly . - Output: an image, the person composited onto beach: person pixels where the matte is 1, beach where it is 0, smooth blends in between. Notice , so a blend never leaves the 0 to 255 range.
Constraints
- foreground: Foreground image (H, W, 3)
- background: Background image (H, W, 3)
- alpha: Alpha matte (H, W) in [0, 1]
- Return: Composited image