| OpenCV 4.3.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].

# Building

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.

# Testing

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 http://opencv.org/license.html.
4
5 #include <iostream>
6 #include "opencv2/highgui.hpp"
7 #include <opencv2/core.hpp>
8 #include <opencv2/imgproc.hpp>
9 #include <opencv2/alphamat.hpp>
10
11 using namespace std;
12 using namespace cv;
13 using namespace cv::alphamat;
14
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 ;
21
22 int main(int argc, char* argv[])
23 {
24 CommandLineParser parser(argc, argv, keys);
25 parser.about("This sample demonstrates Information Flow Alpha Matting");
26
27 if (parser.has("help"))
28 {
29 parser.printMessage();
30 return 0;
31 }
32
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");
36
37 if (!parser.check()
38 || img_path.empty() || trimap_path.empty())
39 {
40 parser.printMessage();
41 parser.printErrors();
42 return 1;
43 }
44
45 Mat image, tmap;
46
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 }
53
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 }
60
61 Mat result;
62 infoFlow(image, tmap, result);
63
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 }
75
76 return 0;
77 }
cv::imwrite
bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
std
STL namespace.
highgui.hpp
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
alphamat.hpp
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:796
cv::WINDOW_NORMAL
the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal s...
Definition: highgui.hpp:183
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
core.hpp
cv::IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image (codec internal conversion)...
Definition: imgcodecs.hpp:66
cv::alphamat::infoFlow
void infoFlow(InputArray image, InputArray tmap, OutputArray result)
cv::IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
cv::alphamat
Definition: alphamat.hpp:16
imgproc.hpp
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:791
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.

# References

[1] Yagiz Aksoy, Tunc Ozan Aydin, Marc Pollefeys, "[Designing Effective Inter-Pixel Information Flow for Natural Image Matting](http://people.inf.ethz.ch/aksoyy/ifm/)", CVPR, 2017.

[2] Roweis, Sam T., and Lawrence K. Saul. "[Nonlinear dimensionality reduction by locally linear embedding](https://science.sciencemag.org/content/290/5500/2323)" Science 290.5500 (2000): 2323-2326.

[3] Anat Levin, Dani Lischinski, Yair Weiss, "[A Closed Form Solution to Natural Image Matting](https://www.researchgate.net/publication/5764820_A_Closed-Form_Solution_to_Natural_Image_Matting)", IEEE TPAMI, 2008.

[4] Qifeng Chen, Dingzeyu Li, Chi-Keung Tang, "[KNN Matting](http://dingzeyu.li/files/knn-matting-tpami.pdf)", IEEE TPAMI, 2013.

[5] Yagiz Aksoy, "[Affinity Based Matting Toolbox](https://github.com/yaksoy/AffinityBasedMattingToolbox)".