Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Nerdyvedi committed Dec 1, 2019
1 parent 0915b7e commit 0e20320
Show file tree
Hide file tree
Showing 16 changed files with 778 additions and 0 deletions.
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
80 changes: 80 additions & 0 deletions modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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
{
private:

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);

void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9);

};

}
}
#endif
48 changes: 48 additions & 0 deletions modules/ximgproc/samples/globalmatting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.
#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

0 comments on commit 0e20320

Please sign in to comment.