Skip to content

[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

Closed
wants to merge 71 commits into from
Closed
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
2b35f51
Add files via upload
Nerdyvedi Aug 25, 2019
65d4268
Add files via upload
Nerdyvedi Aug 25, 2019
ab0015d
Add files via upload
Nerdyvedi Aug 25, 2019
75bb304
Update README.md
Nerdyvedi Aug 26, 2019
977d524
Update and rename mattingClass.cpp to globalmatting.cpp
Nerdyvedi Aug 26, 2019
0cf5b80
Rename mattingClass.h to globalmatting.h
Nerdyvedi Aug 26, 2019
cba44e8
Update and rename globalmatting.h to globalmatting.hpp
Nerdyvedi Aug 26, 2019
61bfe98
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
c226204
Add files via upload
Nerdyvedi Aug 26, 2019
8a03fe6
Update global_matting_sample.cpp
Nerdyvedi Aug 26, 2019
0421025
Update test_global_matting.cpp
Nerdyvedi Aug 26, 2019
c1b2aaa
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
f71d0ce
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
7679b63
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
74155ca
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
3781902
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
dcce846
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
16d0c32
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
1b22801
Update ximgproc.hpp
Nerdyvedi Aug 26, 2019
2f915e9
Update ximgproc.hpp
Nerdyvedi Aug 26, 2019
622caaf
Update ximgproc.hpp
Nerdyvedi Aug 26, 2019
071c424
Update CMakeLists.txt
Nerdyvedi Aug 26, 2019
6ea2ca5
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
ad6f044
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
afc6138
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
6bcba8b
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
403d8f5
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
4ce6e54
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
864c7ff
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
5cd2e45
Update test_global_matting.cpp
Nerdyvedi Aug 26, 2019
130ed66
Update test_global_matting.cpp
Nerdyvedi Aug 26, 2019
8a49f43
Update test_global_matting.cpp
Nerdyvedi Aug 26, 2019
6d70772
Update global_matting_sample.cpp
Nerdyvedi Aug 26, 2019
c2336a4
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
ccbf27f
Update globalmatting.hpp
Nerdyvedi Aug 26, 2019
315c4ae
Removing trailing whitespaces
Nerdyvedi Aug 26, 2019
cadd0fe
Removing whitespaces
Nerdyvedi Aug 26, 2019
cbed56d
Rename global_matting_sample.cpp to globalmatting.cpp
Nerdyvedi Aug 26, 2019
63686e0
Rename test_global_matting.cpp to test_globalmatting.cpp
Nerdyvedi Aug 26, 2019
c3411d7
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
8744607
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
c1cc64c
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
c07720b
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
ffca714
Update globalmatting.cpp
Nerdyvedi Aug 26, 2019
0d6e6a8
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
147e9ec
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
e7df48b
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
fd92eec
Update globalmatting.hpp
Nerdyvedi Aug 27, 2019
f2773a1
Update globalmatting.cpp
Nerdyvedi Aug 27, 2019
0a7cd13
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
cdd78a8
Removing whitespaces
Nerdyvedi Aug 27, 2019
3bd6b02
Removed whitespaces
Nerdyvedi Aug 27, 2019
18e313d
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
02df381
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
6eacc41
Update test_globalmatting.cpp
Nerdyvedi Aug 27, 2019
2850fe6
Update globalmatting.cpp
Nerdyvedi Aug 27, 2019
405aa9b
Removing global structure from header file
Nerdyvedi Aug 31, 2019
f24a6b6
Added the global structure into cpp file
Nerdyvedi Aug 31, 2019
65818c6
Added license and removed extra header files
Nerdyvedi Aug 31, 2019
60fef49
Added License information
Nerdyvedi Aug 31, 2019
ac5f5ba
Removed whitespaces
Nerdyvedi Aug 31, 2019
0af5e5f
Update globalmatting.cpp
Nerdyvedi Aug 31, 2019
cda943d
Create globalmatting.md
Nerdyvedi Sep 1, 2019
0782161
Update globalmatting.md
Nerdyvedi Sep 1, 2019
e5e6be1
Update globalmatting.md
Nerdyvedi Sep 3, 2019
afce16a
Removed square function from the public header
Nerdyvedi Sep 8, 2019
7e739df
Added the square function to the source file
Nerdyvedi Sep 8, 2019
f238144
Added license information and removed BaseTest
Nerdyvedi Sep 8, 2019
48abe60
Using cvtest::findDatafile
Nerdyvedi Sep 8, 2019
93f54a5
Bringing y to outer loop and x in inner loop
Nerdyvedi Sep 8, 2019
0a7ca9e
Added license information to sample
Nerdyvedi Sep 8, 2019
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
2 changes: 1 addition & 1 deletion modules/ximgproc/CMakeLists.txt
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)
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 method for 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
109 changes: 109 additions & 0 deletions modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp
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>
Copy link
Member

Choose a reason for hiding this comment

The 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>
Copy link
Member

Choose a reason for hiding this comment

The 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
{
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;
}



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);
Copy link
Member

Choose a reason for hiding this comment

The 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
45 changes: 45 additions & 0 deletions modules/ximgproc/samples/globalmatting.cpp
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;
}
Loading