In this post we are going to discuss alpha matting. Alpha matting is the process of overlaying a foreground image with transparency over a background image. The transparency is often the fourth channel of an image ( e.g. in a transparent PNG), but it can also be a separate image. This transparency mask is often called the alpha mask or the alpha matte.
In the below figure, the foreground image is shown on the left, the background image and the gray scale alpha mask is shown on the middle and the composite image obtained by blending the foreground image and the background image using an alpha mask is shown on the right.
The math behind alpha blending is straight forward. At every pixel of the image, we need to combine the foreground image color (F) and the background image color (B) using the alpha mask \((\alpha)\).
Note that the value of \(\alpha\) used in the equation is actually the pixel value in the alpha mask divided by \(255\). So, in the equation below, \(0 \leq \alpha \leq 1\).
$$I = \alpha F + (1 – \alpha) B$$
From the equation above, you can make the following observations.
- When \(\alpha = 0\), the output pixel color is the background.
- When \(\alpha = 1\), the output pixel color is simply the foreground.
- When \(0 < \alpha < 1\) the output pixel color is a mix of the background and the foreground. For realistic blending, the boundary of the alpha mask usually has pixels that are between 0 and 1.
\(\alpha\)- Matting using OpenCV
To find the alpha matted image first of all let us import the required library:
#import the required library import numpy as np import cv2 import matplotlib.pyplot as plt
Now we read the foreground, background and alpha-matte image using cv2.imread() function of openCV.
# Read the image foreground = cv2.imread('foreground.png') #Foreground Image background = cv2.imread('background.jpg') #Background Image alpha = cv2.imread('alpha.png') #alpha mask #print the shape of these images print('Foreground Shape: ',foreground.shape) print('Background Shape: ',background.shape) print('Alpha Mask Shape', alpha.shape)
We print the shape of the three images. if there shape are different then we must resize all the images to same size so that we can perform matrix operation. Let us resize all the images to \((563, 800, 3)\). so that all the images are of same size.
bg = cv2.resize(background,(800,563)) alpha = cv2.resize(alpha, (800,563))
Now convert all the images to floating type to get higher precision.
# Convert these images two floating point image. fg = foreground.astype(float) bg = bg.astype(float) # Normalize the alpha mask to keep intensity between 0 and 1 alpha = alpha.astype(float)/255
Now we use the equation \(I = \alpha F + (1 – \alpha) B\) to calculate the alpha blended images and display it:
# Multiply the foreground with the alpha matte foreground = cv2.multiply(alpha, fg) # Multiply the background with ( 1 - alpha ) background = cv2.multiply(1.0 - alpha, bg) # Add the masked foreground and background. outImage = cv2.add(foreground, background) # Display image cv2.imshow("fg", outImage/255) cv2.waitKey(0) cv2.destroyAllWindows()
The composite image is shown below.
This article is contributed by Ram Kripal. If you like eLgo Academy and would like to contribute, you can mail your article to email@example.com. See your article appearing on the eLgo Academy page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.