Skip to content
Source Code for KNN Matting, CVPR 2012 / TPAMI 2013. MATLAB code ready to run. Simple and robust implementation under 40 lines.
Branch: master
Clone or download

Latest commit

Latest commit 5777a39 Jul 20, 2017


Type Name Latest commit message Commit time
Failed to load latest commit information.
src installation script update Dec 30, 2015
LICENSE Create LICENSE Jul 20, 2017 installation script update Dec 30, 2015

KNN Matting

Qifeng Chen, Dingzeyu Li, Chi-Keung Tang
The Hong Kong University of Science and Technology
CVPR 2012 / TPAMI 2013

KNN Matting

Installation Steps

Linux and Mac

run "bash" to download all the required libraries and data. It would take several minutes to tens of minutes, depending on the network connection.

Windows or Manual Installation

  • Download the VLFeat library and extract into the same directory.
  • Download the training dataset from .
  • Extract corresponding files into ${KNN_MATTING_DIR}/vlfeat/ and ${KNN_MATTING_DIR}/data/, for details please see the ${KNN_MATTING_DIR}/src/run_demo.m.

Optional Data

  • SVBRDF data from Jason Lawence, the inverse shaded tree database.

Running the Demo

We have been running our codes since Matlab R2011b. The latest version of code is tested on Matlab R2015a. Please let us know if you run into problem.

The input method 1.Left click on each layer (Press Space to seperate layers) Enter to terminate

Parameters to change are input at the begining of the code lambda: see equ(12) level: the degree of spatial coherence. normally between 0.5 and 3 factor: the degree of hue. normally between 0.5 and 3 im: an image or BRDF data scrib: scribble l: input windows size is (l*2+1)^2 nn: the number of neighbors. It can be a vector of two elements. For example [10;2] means 10 neighbors with default(level) spatial coherence and 2 neighbors with weak spatial coherence.

Note: Scribble inputs in RGB space usually perform better than HSV space.

More Information

For more information, please go to our project site for the detailed paper.


The code is free for academic/research purpose. Use at your own risk and we are not responsible for any loss resulting from this code. Feel free to submit pull request for bug fixes.


Qifeng Chen ( and Dingzeyu Li (

You can’t perform that action at this time.