Skip to content

Commit 0e20320

Browse files
committed
Add files via upload
Add files via upload Add files via upload Update README.md Update and rename mattingClass.cpp to globalmatting.cpp Rename mattingClass.h to globalmatting.h Update and rename globalmatting.h to globalmatting.hpp Update globalmatting.cpp Add files via upload Update global_matting_sample.cpp Update test_global_matting.cpp Update globalmatting.cpp Update globalmatting.hpp Update globalmatting.hpp Update globalmatting.cpp Update globalmatting.cpp Update globalmatting.hpp Update globalmatting.hpp Update ximgproc.hpp Update ximgproc.hpp Update ximgproc.hpp Update CMakeLists.txt Update globalmatting.hpp Update globalmatting.hpp Update globalmatting.hpp Update globalmatting.hpp Update globalmatting.hpp Update globalmatting.cpp Update globalmatting.cpp Update test_global_matting.cpp Update test_global_matting.cpp Update test_global_matting.cpp Update global_matting_sample.cpp Update globalmatting.cpp Update globalmatting.hpp Removing trailing whitespaces Removing whitespaces Rename global_matting_sample.cpp to globalmatting.cpp Rename test_global_matting.cpp to test_globalmatting.cpp Update globalmatting.cpp Update globalmatting.cpp Update globalmatting.cpp Update globalmatting.cpp Update globalmatting.cpp Update test_globalmatting.cpp Update test_globalmatting.cpp Update test_globalmatting.cpp Update globalmatting.hpp Update globalmatting.cpp Update test_globalmatting.cpp Removing whitespaces Removed whitespaces Update test_globalmatting.cpp Update test_globalmatting.cpp Update test_globalmatting.cpp Update globalmatting.cpp Removing global structure from header file Added the global structure into cpp file Added license and removed extra header files Added License information Removed whitespaces Update globalmatting.cpp Create globalmatting.md Update globalmatting.md Update globalmatting.md Removed square function from the public header Added the square function to the source file Added license information and removed BaseTest Using cvtest::findDatafile Bringing y to outer loop and x in inner loop Added license information to sample Update test_globalmatting.cpp Removed trailing whitespaces Update README.md Update README.md Update globalmatting.md Not using BaseTest legacy code anymore Put all the testing code in the Test() Fixed buildbot errors Fixed syntactical errors Fixed whitespace errors Removing highgui dependency It is redundant for the module Removed highgui header file highgui module is not used in header files Using CV_CheckTypeEQ function Fixed buildbot errors Fixed whitespace errors Added __OPENCV_XIMGPROC prefix and _HPP___ suffix Removed unnecessary header files Using Scalar::all(0) instead of (uchar)0 Replacing Scalar::all(0) with cv::Scalar::all(0) Replacing Scalar::all(0) with (uchar)0 Using opencv's random function Removed whitespace errors Added images used in tutorials No external links on embedded images are now used Fixed image path error
1 parent 0915b7e commit 0e20320

16 files changed

+778
-0
lines changed

Diff for: modules/ximgproc/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Extended Image Processing
1616
- Pei&Lin Normalization
1717
- Ridge Detection Filter
1818
- Binary morphology on run-length encoded images
19+
- Global sampling based method for alpha matting

Diff for: modules/ximgproc/include/opencv2/ximgproc.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "ximgproc/run_length_morphology.hpp"
6060
#include "ximgproc/edgepreserving_filter.hpp"
6161
#include "ximgproc/color_match.hpp"
62+
#include "ximgproc/globalmatting.hpp"
6263

6364

6465
/** @defgroup ximgproc Extended Image Processing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
#ifndef __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__
5+
#define __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__
6+
7+
#include <opencv2/imgproc.hpp>
8+
#include <opencv2/ximgproc/edge_filter.hpp>
9+
10+
11+
namespace cv
12+
{
13+
14+
namespace ximgproc
15+
{
16+
class CV_EXPORTS GlobalMatting
17+
{
18+
private:
19+
20+
std::vector<cv::Point> findBoundaryPixels(const cv::Mat_<uchar> &trimap, int a, int b);
21+
22+
// Eq. 2
23+
float calculateAlpha(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I);
24+
25+
// Eq. 3
26+
float colorCost(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I, float alpha);
27+
28+
// Eq. 4
29+
float distCost(const cv::Point &p0, const cv::Point &p1, float minDist);
30+
31+
float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1);
32+
float nearestDistance(const std::vector<cv::Point> &boundary, const cv::Point &p);
33+
34+
35+
36+
37+
void expansionOfKnownRegions(const cv::Mat_<cv::Vec3b> &image,
38+
cv::Mat_<uchar> &trimap,
39+
int r, float c);
40+
41+
// erode foreground and background regions to increase the size of unknown region
42+
void erodeFB(cv::Mat_<uchar> &trimap, int r);
43+
44+
45+
46+
struct Sample
47+
{
48+
int fi, bj;
49+
float df, db;
50+
float cost, alpha;
51+
};
52+
53+
void calculateAlphaPatchMatch(const cv::Mat_<cv::Vec3b> &image,
54+
const cv::Mat_<uchar> &trimap,
55+
const std::vector<cv::Point> &foregroundBoundary,
56+
const std::vector<cv::Point> &backgroundBoundary,
57+
std::vector<std::vector<Sample> > &samples);
58+
59+
void expansionOfKnownRegionsHelper(const cv::Mat &_image,
60+
cv::Mat &_trimap,
61+
int r, float c);
62+
63+
64+
// erode foreground and background regions to increase the size of unknown region
65+
void erodeFB(cv::Mat &_trimap, int r);
66+
67+
void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter);
68+
void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf);
69+
public:
70+
GlobalMatting();
71+
72+
void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf);
73+
74+
void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9);
75+
76+
};
77+
78+
}
79+
}
80+
#endif

Diff for: modules/ximgproc/samples/globalmatting.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#include <opencv2/ximgproc.hpp>
5+
#include <iostream>
6+
#include <opencv2/core.hpp>
7+
#include <opencv2/highgui.hpp>
8+
9+
using namespace std;
10+
using namespace cv;
11+
using namespace ximgproc;
12+
int main(int argc,char** argv)
13+
{
14+
if(argc<3)
15+
{
16+
cout<<"arg1: Directory of Input image"<<endl;
17+
cout<<"arg2: Directory of its trimap"<<endl;
18+
cout<<"arg3(optional): Enter the number of iterations to run expansion of trimap"<<endl;
19+
return -1;
20+
}
21+
string img_path = argv[1];
22+
string tri_path = argv[2];
23+
int niter = 9;
24+
if(argc==4)
25+
{
26+
niter = atoi(argv[3]);
27+
}
28+
cv::Mat image = cv::imread(img_path, cv::IMREAD_COLOR);
29+
cv::Mat trimap = cv::imread(tri_path, cv::IMREAD_GRAYSCALE);
30+
if(image.empty() || trimap.empty())
31+
{
32+
cout<<"Could not load the inputs"<<endl;
33+
return -2;
34+
}
35+
// (optional) exploit the affinity of neighboring pixels to reduce the
36+
// size of the unknown region. please refer to the paper
37+
// 'Shared Sampling for Real-Time Alpha Matting'.
38+
39+
cv::Mat foreground, alpha;
40+
41+
GlobalMatting gm;
42+
43+
gm.getMat(image,trimap,foreground,alpha,niter);
44+
45+
cv::imwrite("alpha-matte.png", alpha);
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)