OpenCV  4.9.0-dev
Open Source Computer Vision
Loading...
Searching...
No Matches
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 with soft boundaries from a background 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 image should be preferably a RGB image.

Input Trimap:

The trimap image is a greyscale image that contains information about the foreground(white pixels), background(black pixels) and unknown(grey) pixels.

Output alpha Matte:

The computed alpha matte is saved as a greyscale image where the pixel values indicate the opacity of the extracted foreground object. These opacity values can be used to blend the foreground object into a diffferent backgound, as shown below:

Following are some more results.

The first column is input RGB image, the second column is input trimap, third column is the extracted alpha matte and the last two columns show the foreground object blended on new backgrounds.

This project is implementation of [7] . It also 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:

<path to your opencv build directory>/bin/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 relevant headers
6#include <iostream>
7#include "opencv2/highgui.hpp"
8#include <opencv2/core.hpp>
9#include <opencv2/imgproc.hpp>
10#include <opencv2/alphamat.hpp>
11
12// Set namespaces
13using namespace std;
14using namespace cv;
15using namespace cv::alphamat;
16
17// Set the usage parameter names
18const char* keys =
19 "{img || input image name}"
20 "{tri || input trimap image name}"
21 "{out || output image name}"
22 "{help h || print help message}"
23;
24
25int main(int argc, char* argv[])
26{
27 CommandLineParser parser(argc, argv, keys);
28 parser.about("This sample demonstrates Information Flow Alpha Matting");
29
30 if (parser.has("help"))
31 {
32 parser.printMessage();
33 return 0;
34 }
35
36 // Read the paths to the input image, input trimap and the location of the output image.
37 string img_path = parser.get<std::string>("img");
38 string trimap_path = parser.get<std::string>("tri");
39 string result_path = parser.get<std::string>("out");
40
41 // Make sure the user inputs paths to the input image and trimap
42 if (!parser.check()
43 || img_path.empty() || trimap_path.empty())
44 {
45 parser.printMessage();
46 parser.printErrors();
47 return 1;
48 }
49
50 Mat image, tmap;
51
52 // Read the input image
53 image = imread(img_path, IMREAD_COLOR);
54 if (image.empty())
55 {
56 printf("Cannot read image file: '%s'\n", img_path.c_str());
57 return 1;
58 }
59
60 // Read the trimap
61 tmap = imread(trimap_path, IMREAD_GRAYSCALE);
62 if (tmap.empty())
63 {
64 printf("Cannot read trimap file: '%s'\n", trimap_path.c_str());
65 return 1;
66 }
67
68 Mat result;
69 // Perform information flow alpha matting
70 infoFlow(image, tmap, result);
71
72 if (result_path.empty())
73 {
74 // Show the alpha matte if a result filepath is not provided.
75 namedWindow("result alpha matte", WINDOW_NORMAL);
76 imshow("result alpha matte", result);
77 waitKey(0);
78 }
79 else
80 {
81 // Save the alphamatte
82 imwrite(result_path, result);
83 printf("Result saved: '%s'\n", result_path.c_str());
84 }
85
86 return 0;
87}
Designed for command line parsing.
Definition utility.hpp:820
n-dimensional dense array class
Definition mat.hpp:812
bool empty() const
Returns true if the array has no elements.
void infoFlow(InputArray image, InputArray tmap, OutputArray result)
Compute alpha matte of an object in an image.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition alphamat.hpp:25
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102
STL namespace.

References

[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.