-
Notifications
You must be signed in to change notification settings - Fork 45
/
KNNMatting.m
39 lines (32 loc) · 1.51 KB
/
KNNMatting.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
% KNN Matting
% This function implements the image matting approach described in
% Qifeng Chen, Dingzeyu Li, Chi-Keung Tang, "KNN Matting", IEEE
% TPAMI, 2013.
% Optional input parameter 'params' can be customized by editing the
% values in the struct returned by 'getMattingParams('CF').
% - knn_K defines the number of nonlocal neighbors
% - knn_xyw defines the weight of the spatial coordinates in KNN search
% - knn_hsv defines the color space (RGB or HSV) for KNN search
function alpha = KNNMatting(image, trimap, params, suppressMessages)
abmtSetup
tic;
if ~exist('params', 'var') || isempty(params)
params = getMattingParams('KNN');
end
if ~exist('suppressMessages', 'var') || isempty(suppressMessages)
suppressMessages = false;
end
if(~suppressMessages) display('KNN Matting started...'); end
image = im2double(image);
trimap = im2double(trimap(:,:,1));
% Compute KNN affinities
unk = trimap < 0.8 & trimap > 0.2;
dilUnk = imdilate(unk, ones(3, 3));
if(~suppressMessages) display(' Computing KNN affinities...'); end
Lap = affinityMatrixToLaplacian(colorSimilarityAffinities(image, params.knn_K, [], [], params.knn_xyw, params.knn_hsv));
if(~suppressMessages) display(' Solving for alphas...'); end
alpha = solveForAlphas(Lap, trimap, params.lambda, params.usePCGtoSolve);
alpha = reshape(alpha, [size(image, 1), size(image, 2)]);
dur = toc;
if(~suppressMessages) display(['Done. It took ' num2str(dur) ' seconds.']); end
end