-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[GSOC 2019] Global sampling matting #2134
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
Changes from 56 commits
2b35f51
65d4268
ab0015d
75bb304
977d524
0cf5b80
cba44e8
61bfe98
c226204
8a03fe6
0421025
c1b2aaa
f71d0ce
7679b63
74155ca
3781902
dcce846
16d0c32
1b22801
2f915e9
622caaf
071c424
6ea2ca5
ad6f044
afc6138
6bcba8b
403d8f5
4ce6e54
864c7ff
5cd2e45
130ed66
8a49f43
6d70772
c2336a4
ccbf27f
315c4ae
cadd0fe
cbed56d
63686e0
c3411d7
8744607
c1cc64c
c07720b
ffca714
0d6e6a8
147e9ec
e7df48b
fd92eec
f2773a1
0a7cd13
cdd78a8
3bd6b02
18e313d
02df381
6eacc41
2850fe6
405aa9b
f24a6b6
65818c6
60fef49
ac5f5ba
0af5e5f
cda943d
0782161
e5e6be1
afce16a
7e739df
f238144
48abe60
93f54a5
0a7ca9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
set(the_description "Extended image processing module. It includes edge-aware filters and etc.") | ||
ocv_define_module(ximgproc opencv_core opencv_imgproc opencv_calib3d opencv_imgcodecs WRAP python java) | ||
ocv_define_module(ximgproc opencv_core opencv_highgui opencv_imgproc opencv_calib3d opencv_imgcodecs WRAP python java) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#ifndef GLOBAL_MATTING_H | ||
#define GLOBAL_MATTING_H | ||
|
||
#include <opencv2/highgui.hpp> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove highgui from public header and from module dependency. |
||
#include <opencv2/imgproc.hpp> | ||
#include <opencv2/ximgproc/edge_filter.hpp> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include headers which are required to describe this public API only. Remove other. |
||
|
||
#include <string> | ||
#include <cstdlib> | ||
|
||
// for sorting the boundary pixels according to intensity | ||
struct IntensityComp | ||
{ | ||
Nerdyvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IntensityComp(const cv::Mat_<cv::Vec3b> &img_temp) : img(img_temp) | ||
{ | ||
|
||
} | ||
|
||
bool operator()(const cv::Point &p0, const cv::Point &p1) const | ||
{ | ||
const cv::Vec3b &c0 = img(p0.y, p0.x); | ||
const cv::Vec3b &c1 = img(p1.y, p1.x); | ||
|
||
return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); | ||
} | ||
|
||
const cv::Mat_<cv::Vec3b> &img; | ||
}; | ||
|
||
|
||
|
||
namespace cv | ||
{ | ||
|
||
namespace ximgproc | ||
{ | ||
class CV_EXPORTS GlobalMatting | ||
{ | ||
private: | ||
|
||
template <typename T> | ||
inline T sqr(T a) | ||
{ | ||
return a * a; | ||
} | ||
Nerdyvedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
|
||
std::vector<cv::Point> findBoundaryPixels(const cv::Mat_<uchar> &trimap, int a, int b); | ||
|
||
// Eq. 2 | ||
float calculateAlpha(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I); | ||
|
||
// Eq. 3 | ||
float colorCost(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I, float alpha); | ||
|
||
// Eq. 4 | ||
float distCost(const cv::Point &p0, const cv::Point &p1, float minDist); | ||
|
||
float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); | ||
float nearestDistance(const std::vector<cv::Point> &boundary, const cv::Point &p); | ||
|
||
|
||
|
||
|
||
void expansionOfKnownRegions(const cv::Mat_<cv::Vec3b> &image, | ||
cv::Mat_<uchar> &trimap, | ||
int r, float c); | ||
|
||
// erode foreground and background regions to increase the size of unknown region | ||
void erodeFB(cv::Mat_<uchar> &trimap, int r); | ||
|
||
|
||
|
||
struct Sample | ||
{ | ||
int fi, bj; | ||
float df, db; | ||
float cost, alpha; | ||
}; | ||
|
||
void calculateAlphaPatchMatch(const cv::Mat_<cv::Vec3b> &image, | ||
const cv::Mat_<uchar> &trimap, | ||
const std::vector<cv::Point> &foregroundBoundary, | ||
const std::vector<cv::Point> &backgroundBoundary, | ||
std::vector<std::vector<Sample> > &samples); | ||
|
||
void expansionOfKnownRegionsHelper(const cv::Mat &_image, | ||
cv::Mat &_trimap, | ||
int r, float c); | ||
|
||
|
||
// erode foreground and background regions to increase the size of unknown region | ||
void erodeFB(cv::Mat &_trimap, int r); | ||
|
||
void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); | ||
void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); | ||
public: | ||
GlobalMatting(); | ||
|
||
void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid using of 'tabs'. Indentation is 4 spaces (no need to indent after opening of "whole-file" namespaces) |
||
|
||
void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9); | ||
|
||
}; | ||
|
||
} | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#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: Directory of Input image"<<endl; | ||
cout<<"arg2: Directory of its trimap"<<endl; | ||
cout<<"arg3(optional): Enter the 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]); | ||
} | ||
cv::Mat image = cv::imread(img_path, cv::IMREAD_COLOR); | ||
cv::Mat trimap = cv::imread(tri_path, cv::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'. | ||
|
||
cv::Mat foreground, alpha; | ||
|
||
GlobalMatting gm; | ||
|
||
gm.getMat(image,trimap,foreground,alpha,niter); | ||
|
||
cv::imwrite("alpha-matte.png", alpha); | ||
|
||
return 0; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.