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 [aksoy2017designing] . 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
19 "{img || input image name}"
20 "{tri || input trimap image name}"
21 "{out || output image name}"
22 "{help h || print help message}"
25int main(
int argc,
char* argv[])
28 parser.about(
"This sample demonstrates Information Flow Alpha Matting");
30 if (parser.has(
"help"))
32 parser.printMessage();
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");
43 || img_path.empty() || trimap_path.empty())
45 parser.printMessage();
53 image = imread(img_path, IMREAD_COLOR);
56 printf(
"Cannot read image file: '%s'\n", img_path.c_str());
61 tmap =
imread(trimap_path, IMREAD_GRAYSCALE);
64 printf(
"Cannot read trimap file: '%s'\n", trimap_path.c_str());
72 if (result_path.empty())
76 imshow(
"result alpha matte", result);
83 printf(
"Result saved: '%s'\n", result_path.c_str());
Designed for command line parsing.
Definition: utility.hpp:818
n-dimensional dense array class
Definition: mat.hpp:811
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 > ¶ms=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.
Definition: alphamat.hpp:25
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106
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.