GithubHelp home page GithubHelp logo

global-matting's Introduction

Global Matting

This project is built by reproducing the global matting algorithm in the CVPR 2011 paper:

He, Kaiming, et al. "A global sampling method for alpha matting." In CVPR’11, pages 2049–2056, 2011.

Benchmark

After evaluating the results on the alpha matting evaluation website, this implementation (with matting Laplacian as post-processing) ranks 5th in SAD and 6th in MSE (the original implementation ranks 10th in SAD and 9th in MSE). Running time is less than 1 seconds for an 800x600 image. Hence, this implementation is one of the highest ranked among the fast matting methods.

Example

Code

#include "globalmatting.h"

// you can get the guided filter implementation
// from https://github.com/atilimcetin/guided-filter
#include "guidedfilter.h"

int main()
{
    cv::Mat image = cv::imread("GT04-image.png", CV_LOAD_IMAGE_COLOR);
    cv::Mat trimap = cv::imread("GT04-trimap.png", CV_LOAD_IMAGE_GRAYSCALE);

    // (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'.
    expansionOfKnownRegions(image, trimap, 9);

    cv::Mat foreground, alpha;
    globalMatting(image, trimap, foreground, alpha);

    // filter the result with fast guided filter
    alpha = guidedFilter(image, alpha, 10, 1e-5);
    for (int x = 0; x < trimap.cols; ++x)
        for (int y = 0; y < trimap.rows; ++y)
        {
            if (trimap.at<uchar>(y, x) == 0)
                alpha.at<uchar>(y, x) = 0;
            else if (trimap.at<uchar>(y, x) == 255)
                alpha.at<uchar>(y, x) = 255;
        }

    cv::imwrite("GT04-alpha.png", alpha);

    return 0;
}

Result

Image Trimap Alpha

License

MIT License.

global-matting's People

Contributors

atilimcetin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

global-matting's Issues

findBoundaryPixels return nothing to foregroundBoundary

Dear Atılım,

I encountered problem when using the library on iOS, which happened when I trying to get boundary:

    std::vector<cv::Point> foregroundBoundary = findBoundaryPixels(trimap, 255, 128);
    std::vector<cv::Point> backgroundBoundary = findBoundaryPixels(trimap, 0, 128);

the function findBoundaryPixels didn't find the boundary , and cause calculateAlphaPatchMatch(image, trimap, foregroundBoundary, backgroundBoundary, samples); crash due to the fact that the size of foregroundBoundary is zero.

I use cvtColor to change the type of the image.

    cv::Mat img = [self cvMatFromUIImage:sourceImage];
    cv::cvtColor(img , img , CV_RGBA2RGB);
    cv::Mat trimap = [self cvMatFromUIImage:trimapImage];
    cv::cvtColor(trimap , trimap , CV_RGB2GRAY);

I'm thinking is my preparation for the image have gone wrong, don't know if you can help with this.

noise alpha

I'v tried this repo to generated some alpha map from trimp(first row) and the alpha result(second row) as below.Can some one tell what's wrong with this, it seems some noise data .

self-face8
self-face8

undefined reference to `guidedFilter(cv::Mat const&, cv::Mat >>const&, int, double, int)'

Hello,

I am interested in testing your code given the speed you claim. However, I get an error.
i run your code this way:
g++ globalmatting.cpp -o globalmattingpkg-config --cflags --libs opencv`

But I get this error:

/tmp/ccsQc2Bz.o: In function main': globalmatting.cpp:(.text+0x4010): undefined reference toguidedFilter(cv::Mat const&, cv::Mat >>const&, int, double, int)'
collect2: error: ld returned 1 exit status

How to fix this ?

Thank you in advance.
Begueradj.

Pixels of the unknown region

Hello,

Thank you for sharing the implementation.

In this file I do not see where and how you did find the coordinates of the pixels of the unknown region ? I can not see that in any of your functions.

Regards,
BEGUERADJ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.