Code Monkey home page Code Monkey logo

matting's Introduction

Remove the background of an image

Input Image Input mask Extracted foreground

Simple Example

from matting import alpha_matting, load_image, save_image, estimate_foreground_background, stack_images

image = load_image("plant_image.jpg", "RGB")
trimap = load_image("plant_trimap.png", "GRAY")

alpha = alpha_matting(image, trimap, method="ifm", preconditioner="jacobi", print_info=True)

foreground, background = estimate_foreground_background(image, alpha, print_info=True)

cutout = stack_images(foreground, alpha)

save_image("out/plant_cutout.png", cutout)

The example files can also be found at https://github.com/99991/matting/tree/master/examples.

Install

Install a C compiler (if you want to use alpha matting methods other than vcycle) and then run:

git clone https://github.com/99991/matting.git
cd matting
python setup.py install
cd examples
python simple_example.py
python advanced_example.py

Replace python with python3 if that is not your default.

Installing a C compiler

Linux

sudo apt-get install build-essential

Windows

Install Python and gcc on Windows.

Mac

Install XCode through the App store.

Advanced Example

from matting import alpha_matting, load_image, save_image, stack_images,\
    estimate_foreground_background, METHODS, PRECONDITIONERS
import os

# Input paths
image_path = "plant_image.jpg"
trimap_path = "plant_trimap.png"
new_background_path = "background.png"
# Output paths
alpha_path = "out/plant_alpha_%s_%s.png"
cutout_path = "out/plant_cutout_%s_%s.png"
os.makedirs("out", exist_ok=True)

# Limit image size to make demo run faster
height = 128

# Load input images
# shape (height, width, 3) of data type numpy.float64 in range [0, 1]
image = load_image(image_path, "RGB", "BILINEAR", height=height)
# shape (height, width) of data type numpy.float64 in range [0, 1]
trimap = load_image(trimap_path, "GRAY", "NEAREST", height=height)

# Calculate alpha with various alpha matting methods and preconditioners
for method in METHODS:
    for preconditioner in PRECONDITIONERS[method]:
        alpha = alpha_matting(
            image, trimap,
            method, preconditioner,
            print_info=True)

        # Save alpha
        save_image(alpha_path % (method, preconditioner), alpha)

        foreground, background = estimate_foreground_background(
            image, alpha, print_info=True)

        # Make new image from foreground and alpha
        cutout = stack_images(foreground, alpha)

        # Save cutout
        save_image(cutout_path % (method, preconditioner), cutout)

Citations

Closed form alpha matting based on

Levin, Anat, Dani Lischinski, and Yair Weiss.
"A closed-form solution to natural image matting."
IEEE transactions on pattern analysis and machine intelligence 30.2 (2008): 228-242.

Knn matting based on

Q. Chen, D. Li, C.-K. Tang.
"KNN Matting."
Conference on Computer Vision and Pattern Recognition (CVPR), June 2012.

Lkm matting based on

He, Kaiming, Jian Sun, and Xiaoou Tang.
"Fast matting using large kernel matting laplacian matrices."
Computer Vision and Pattern Recognition (CVPR),
2010 IEEE Conference on. IEEE, 2010.

Ifm matting based on

Aksoy, Yagiz, Tunc Ozan Aydin, and Marc Pollefeys.
"Designing effective inter-pixel information flow for natural image matting."
Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2017.

Vcycle matting based on

Lee, Philip G., and Ying Wu.
"Scalable matting: A sub-linear approach."
arXiv preprint arXiv:1404.3933 (2014).

The information flow alpha matting method is provided for academic use only. If you use the information flow alpha matting method for an academic publication, please cite corresponding publications referenced in the description of each function:

@INPROCEEDINGS{ifm,
author={Aksoy, Ya\u{g}{\i}z and Ayd{\i}n, Tun\c{c} Ozan and Pollefeys, Marc}, 
booktitle={Proc. CVPR}, 
title={Designing Effective Inter-Pixel Information Flow for Natural Image Matting}, 
year={2017}, 
}

matting's People

Contributors

octopusinvitro avatar 99991 avatar foobar2016 avatar

Watchers

James Cloos avatar

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.