Trimaps in Image Matting

Because image matting is a difficult task, most of the algorithm requires some sort of annotation. The most common one is a trimap.


Trimap is a one-channel map that represents the absolute background, foreground, and unknown regions of an image. It is primarily created through human annotation, although some supervised and unsupervised machine-learning solutions can be used to generate them. However, the resulting quality may not be as good as a human annotation.

Example Trimap

Below is a sample image and a corresponding trimap.

Image and Trimap

Sample Application

Here is the foreground selection tool interface of the GIMP image editing program.

Foreground Selection on GIMP

Generating Trimap From Alpha Matte

To generate a trimap from an [[Alpha Matte]] and generate a trimap with morphological operations. First, dilate the mask to expand the regions of foreground and background pixels. Then, erode the mask to shrink the regions of foreground and background pixels. The resulting image will be the trimap, with the foreground and background pixels represented by white and black pixels, respectively, and the unknown pixels represented by gray pixels. Here is a function that extracts trimap from the alpha channel (Python).

import numpy as np
import cv2 as cv

def generate_trimap(alpha):
   k_size = random.choice(range(2, 5))
   iterations = np.random.randint(5, 15)
   kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (k_size, k_size))
   dilated = cv.dilate(alpha, kernel, iterations=iterations)
   eroded = cv.erode(alpha, kernel, iterations=iterations)
   trimap = np.zeros(alpha.shape, dtype=np.uint8)

   trimap[eroded >= 255] = 255
   trimap[dilated <= 0] = 0

   return trimap