Skip to content

Implementation of Global sampling based matting #2278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/ximgproc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Extended Image Processing
- Pei&Lin Normalization
- Ridge Detection Filter
- Binary morphology on run-length encoded images
- Global sampling based method for alpha matting
1 change: 1 addition & 0 deletions modules/ximgproc/include/opencv2/ximgproc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "ximgproc/run_length_morphology.hpp"
#include "ximgproc/edgepreserving_filter.hpp"
#include "ximgproc/color_match.hpp"
#include "ximgproc/globalmatting.hpp"


/** @defgroup ximgproc Extended Image Processing
Expand Down
28 changes: 28 additions & 0 deletions modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__
#define __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__

#include <opencv2/imgproc.hpp>
#include <opencv2/ximgproc/edge_filter.hpp>


namespace cv { namespace ximgproc {

class CV_EXPORTS GlobalMatting
{
public:
GlobalMatting();
virtual ~GlobalMatting();

virtual void globalMatting(InputArray image, InputArray trimap, OutputArray foreground, OutputArray alpha, OutputArray conf = noArray()) = 0;

virtual void getMat(InputArray image, InputArray trimap, OutputArray foreground, OutputArray alpha, int niter=9) = 0;
};

CV_EXPORTS Ptr<GlobalMatting> createGlobalMatting();

}} // namespace

#endif // __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__
51 changes: 51 additions & 0 deletions modules/ximgproc/samples/globalmatting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <opencv2/ximgproc.hpp>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;
using namespace ximgproc;

int main(int argc, char** argv)
{
if (argc < 3)
{
cout << "arg1: location of input image" << endl;
cout << "arg2: location of its trimap" << endl;
cout << "arg3(optional): number of iterations to run expansion of trimap" << endl;
return -1;
}
string img_path = argv[1];
string tri_path = argv[2];
int niter = 9;
if (argc == 4)
{
niter = atoi(argv[3]);
}
Mat image = imread(img_path, IMREAD_COLOR);
Mat trimap = imread(tri_path, IMREAD_GRAYSCALE);
if (image.empty() || trimap.empty())
{
cout << "Could not load the inputs" << endl;
return -2;
}
// (optional) exploit the affinity of neighboring pixels to reduce the
// size of the unknown region. please refer to the paper
// 'Shared Sampling for Real-Time Alpha Matting'.

Mat foreground, alpha;

Ptr<GlobalMatting> gm = createGlobalMatting();

gm->getMat(image, trimap, foreground, alpha, niter);

imwrite("alpha-matte.png", alpha);

imshow("input", image);
imshow("trimap", trimap);
imshow("alpha-matte", alpha);
waitKey();

return 0;
}
Loading