OpenCV  4.4.0
Open Source Computer Vision
Information Flow Alpha Matting

This project was part of Google Summer of Code 2019.

Student: Muskaan Kularia

Mentor: Sunita Nayak

Alphamatting is the problem of extracting the foreground from an image. The extracted foreground can be used for further operations like changing the background in an image.

Given an input image and its corresponding trimap, we try to extract the foreground from the background. Following is an example:

Input Image:

Input Trimap:

Output alpha Matte:

This project is implementation of [4] . It required implementation of parts of other papers [2,3,4].


This module uses the Eigen package.

Build the sample code of the alphamat module using the following two cmake commands run inside the build folder:

cmake -DOPENCV_EXTRA_MODULES_PATH=<path to opencv_contrib modules> -DBUILD_EXAMPLES=ON ..
cmake --build . --config Release --target example_alphamat_information_flow_matting

Please refer to OpenCV building tutorials for further details, if needed.


The built target can be tested as follows:

example_alphamat_information_flow_matting -img=<path to input image file> -tri=<path to the corresponding trimap> -out=<path to save output matte file>

Source Code of the sample

1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at
5 #include <iostream>
6 #include "opencv2/highgui.hpp"
7 #include <opencv2/core.hpp>
8 #include <opencv2/imgproc.hpp>
9 #include <opencv2/alphamat.hpp>
11 using namespace std;
12 using namespace cv;
13 using namespace cv::alphamat;
15 const char* keys =
16  "{img || input image name}"
17  "{tri || input trimap image name}"
18  "{out || output image name}"
19  "{help h || print help message}"
20 ;
22 int main(int argc, char* argv[])
23 {
24  CommandLineParser parser(argc, argv, keys);
25  parser.about("This sample demonstrates Information Flow Alpha Matting");
27  if (parser.has("help"))
28  {
29  parser.printMessage();
30  return 0;
31  }
33  string img_path = parser.get<std::string>("img");
34  string trimap_path = parser.get<std::string>("tri");
35  string result_path = parser.get<std::string>("out");
37  if (!parser.check()
38  || img_path.empty() || trimap_path.empty())
39  {
40  parser.printMessage();
41  parser.printErrors();
42  return 1;
43  }
45  Mat image, tmap;
47  image = imread(img_path, IMREAD_COLOR); // Read the input image file
48  if (image.empty())
49  {
50  printf("Cannot read image file: '%s'\n", img_path.c_str());
51  return 1;
52  }
54  tmap = imread(trimap_path, IMREAD_GRAYSCALE);
55  if (tmap.empty())
56  {
57  printf("Cannot read trimap file: '%s'\n", trimap_path.c_str());
58  return 1;
59  }
61  Mat result;
62  infoFlow(image, tmap, result);
64  if (result_path.empty())
65  {
66  namedWindow("result alpha matte", WINDOW_NORMAL);
67  imshow("result alpha matte", result);
68  waitKey(0);
69  }
70  else
71  {
72  imwrite(result_path, result);
73  printf("Result saved: '%s'\n", result_path.c_str());
74  }
76  return 0;
77 }
bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
STL namespace.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
Designed for command line parsing.
Definition: utility.hpp:788
the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal s...
Definition: highgui.hpp:183
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
If set, always convert image to the single channel grayscale image (codec internal conversion)...
Definition: imgcodecs.hpp:66
void infoFlow(InputArray image, InputArray tmap, OutputArray result)
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
Definition: alphamat.hpp:16
n-dimensional dense array class
Definition: mat.hpp:791
bool empty() const
Returns true if the array has no elements.
int waitKey(int delay=0)
Waits for a pressed key.


[1] Yagiz Aksoy, Tunc Ozan Aydin, Marc Pollefeys, "[Designing Effective Inter-Pixel Information Flow for Natural Image Matting](", CVPR, 2017.

[2] Roweis, Sam T., and Lawrence K. Saul. "[Nonlinear dimensionality reduction by locally linear embedding](" Science 290.5500 (2000): 2323-2326.

[3] Anat Levin, Dani Lischinski, Yair Weiss, "[A Closed Form Solution to Natural Image Matting](", IEEE TPAMI, 2008.

[4] Qifeng Chen, Dingzeyu Li, Chi-Keung Tang, "[KNN Matting](", IEEE TPAMI, 2013.

[5] Yagiz Aksoy, "[Affinity Based Matting Toolbox](".