This is the authors' implementation of KNN Matting.

@inproceedings{knn_matting_cvpr12,
        AUTHOR = "Chen Q. and Li, D. and Tang, C.K.",
        TITLE = "KNN Matting",
        BOOKTITLE = "IEEE Conference on Computer Vision and Pattern Recognition, 2012 (CVPR12)",
        YEAR = "2012"}


There are four matlab codes for different purpose.
1. knn_matting_scribble_hsv.m for scribble input in HSV space
2. knn_matting_scribble_rgb.m for scribble input in RGB space
3. knn_matting_user_input_image.m for user input and a single image
4. knn_matting_user_input_brdf.m for user input and BRDF data (may need large memory)

The input method
1.Left click on each layer (Press Space to seperate layers)
2.press 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.

The vlfeat library is from http://www.vlfeat.org/
Natural image files can be obtained from www.alphamatting.com
SVBRDF data is from Jason Lawence inverse shaded tree database.
http://ist.cs.princeton.edu/



Disclaimer:

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. 

Should you have problems running the program, feel free to contact Qifeng Chen, cqf@stanford.edu
