Skip to content

Commit 0f85769

Browse files
committed
alpha matting code
1 parent 78e3879 commit 0f85769

26 files changed

+3040
-0
lines changed

modules/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ $ cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -D BUILD_opencv_<r
1010

1111
- **aruco**: ArUco and ChArUco Markers -- Augmented reality ArUco marker and "ChARUco" markers where ArUco markers embedded inside the white areas of the checker board.
1212

13+
- **alphamat**: Computer Vision based Alpha Matting -- Given an input image and a trimap, generate an alpha matte.
14+
1315
- **bgsegm**: Background segmentation algorithm combining statistical background image estimation and per-pixel Bayesian segmentation.
1416

1517
- **bioinspired**: Biological Vision -- Biologically inspired vision model: minimize noise and luminance variance, transient event segmentation, high dynamic range tone mapping methods.

modules/alphamat/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if(NOT HAVE_EIGEN)
2+
set(DISABLE_MSG "Module opencv_alphamat disabled because the following dependencies are not found:")
3+
set(DISABLE_MSG "${DISABLE_MSG} Eigen")
4+
message(STATUS ${DISABLE_MSG})
5+
ocv_module_disable(alphamat)
6+
endif()
7+
8+
cmake_minimum_required (VERSION 3.0)
9+
project (myproject)
10+
set (CMAKE_CXX_STANDARD 11)
11+
12+
ocv_define_module(alphamat opencv_core
13+
opencv_imgproc
14+
)

modules/alphamat/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Computer Vision based Alpha Matting
2+
3+
This project was part of the Google Summer of Code 2019.
4+
5+
####Student: Muskaan Kularia
6+
####Mentor: Sunita Nayak
7+
***
8+
Alphamatting is the problem of extracting the foreground from an image. Given the input of an image and its corresponding trimap, we try to extract the foreground from the background.
9+
10+
This project is implementation of "[[Designing Effective Inter-Pixel Information Flow for Natural Image Matting](http://people.inf.ethz.ch/aksoyy/ifm/)]" by Yağız Aksoy, Tunç Ozan Aydın and Marc Pollefeys[1]. It required implementation of parts of other papers [2,3,4].
11+
12+
13+
## References
14+
15+
[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.
16+
17+
[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.
18+
19+
[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.
20+
21+
[4] Qifeng Chen, Dingzeyu Li, Chi-Keung Tang, "[KNN Matting](http://dingzeyu.li/files/knn-matting-tpami.pdf)", IEEE TPAMI, 2013.
22+
23+
[5] Yagiz Aksoy, "[Affinity Based Matting Toolbox](https://github.com/yaksoy/AffinityBasedMattingToolbox)".

modules/alphamat/doc/alphamat.bib

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@inproceedings{aksoy2017designing,
2+
title={Designing effective inter-pixel information flow for natural image matting},
3+
author={Aksoy, Yagiz and Ozan Aydin, Tunc and Pollefeys, Marc},
4+
booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
5+
pages={29--37},
6+
year={2017}
7+
}
8+
9+
@article{roweis2000nonlinear,
10+
title={Nonlinear dimensionality reduction by locally linear embedding},
11+
author={Roweis, Sam T and Saul, Lawrence K},
12+
journal={science},
13+
volume={290},
14+
number={5500},
15+
pages={2323--2326},
16+
year={2000},
17+
publisher={American Association for the Advancement of Science}
18+
}
19+
20+
@inproceedings{shahrian2013improving,
21+
title={Improving image matting using comprehensive sampling sets},
22+
author={Shahrian, Ehsan and Rajan, Deepu and Price, Brian and Cohen, Scott},
23+
booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
24+
pages={636--643},
25+
year={2013}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
/** Information Flow algorithm implementaton for alphamatting*/
6+
7+
#ifndef _OPENCV_ALPHAMAT_INFOFLOW_HPP_
8+
#define _OPENCV_ALPHAMAT_INFOFLOW_HPP_
9+
10+
#include <Eigen/Sparse>
11+
using namespace Eigen;
12+
13+
namespace cv{ namespace alphamat{
14+
15+
void solve(SparseMatrix<double> Wcm,SparseMatrix<double> Wuu,SparseMatrix<double> Wl,SparseMatrix<double> Dcm,
16+
SparseMatrix<double> Duu,SparseMatrix<double> Dl, SparseMatrix<double> T, Mat& wf, Mat& alpha);
17+
18+
CV_EXPORTS_W void infoFlow(Mat& image, Mat& tmap, Mat& result);
19+
20+
}}
21+
#endif
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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/base.hpp>
8+
#include <opencv2/core/utility.hpp>
9+
#include <opencv2/imgproc.hpp>
10+
#include <opencv2/infoflow.hpp>
11+
12+
using namespace std;
13+
using namespace cv;
14+
using namespace cv::alphamat;
15+
16+
const char* keys =
17+
{
18+
"{img || input image name}"
19+
"{tri || input trimap image name}"
20+
"{out || output image name}"
21+
};
22+
23+
int main(int argc, char *argv[])
24+
{
25+
bool show_help = (argc == 1);
26+
show_help = show_help || (argc == 2 && string(argv[1]) == "--help");
27+
show_help = show_help || (argc == 2 && string(argv[1]) == "-h");
28+
29+
if (show_help)
30+
{
31+
printf("\nThis sample demonstrates Information Flow alpha matting\n"
32+
"Call:\n"
33+
" %s -img=<string> -tri=<string> [-out=<string>]\n\n", argv[0]);
34+
return 0;
35+
}
36+
37+
CommandLineParser parser(argc, argv, keys);
38+
if (!parser.check())
39+
{
40+
parser.printErrors();
41+
return -1;
42+
}
43+
44+
string img_path = parser.get<std::string>("img");
45+
string trimap_path = parser.get<std::string>("tri");
46+
string result_path = parser.get<std::string>("out");
47+
48+
Mat image, tmap;
49+
50+
image = imread(img_path, IMREAD_COLOR); // Read the input image file
51+
if (image.empty())
52+
{
53+
printf("Cannot read image file: %s\n", img_path.c_str());
54+
return -1;
55+
}
56+
57+
tmap = imread(trimap_path, IMREAD_GRAYSCALE);
58+
if (tmap.empty())
59+
{
60+
printf("Cannot read trimap file: %s\n", trimap_path.c_str());
61+
return -1;
62+
}
63+
64+
Mat result;
65+
infoFlow(image, tmap, result);
66+
67+
if (result_path.empty())
68+
{
69+
namedWindow("result alpha matte", WINDOW_NORMAL);
70+
imshow("result alpha matte", result);
71+
waitKey(0);
72+
}
73+
else
74+
{
75+
imwrite(result_path, result);
76+
}
77+
78+
imshow("Result Matte", result);
79+
return 0;
80+
81+
}
842 KB
Loading
Loading
Loading
3.66 KB
Loading

0 commit comments

Comments
 (0)