-
Notifications
You must be signed in to change notification settings - Fork 7
/
matte.m
231 lines (195 loc) · 6.54 KB
/
matte.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
function [ alpha, mse, W ] = matte( image, scribs, localWinRad, NLSearchRad, NLVariance, hoodSize, gt, Win, h2in )
%MATTE Gets the nonlocal matte.
% image and scribs are required, the rest are optional.
% localWinRad - How big the windows are when comparing image patches.
% NLSearchRad - Radius of NL search (to limit computation).
% NLVariance - NL Variance (bigger = more similar neighbors).
% hoodSize - How many NL neighbors to include in linear coeff. estimation.
% gt - The ground truth alpha matte for comparison
% Win - The optional weight matrix so we won't have to recalculate it.
% h2in - The h^2 parameter for Ain
if( ischar(image) )
I = double(imread(image))./255;
else
I = image;
end
%============YOOOHOOOOO!!==============
% Convert to grayscale
I = mean(I,3);
% Some constants.
if( ~exist('localWinRad') || isempty(localWinRad) )
localWinRad = 3; % How big the windows are when comparing image patches.
end
if( ~exist('NLSearchRad') || isempty(NLSearchRad) )
NLSearchRad = 10; % Radius of NL search (to limit computation).
end
if( ~exist('NLVariance') || isempty(NLVariance) )
NLVariance = 1e0; % NL Variance (bigger = more similar neighbors).
end
if( ~exist('hoodSize') || isempty(hoodSize) )
hoodSize = 15; % How many NL neighbors to include in linear coeff. estimation.
end
lambda = 1e-5; % Regularization param for linear coeff solution. (small)
%c = 1e5; % Regularization param for constrained alpha vals. (large)
if( ~exist('Win') || isempty(Win) || ~exist('h2in') || isempty(h2in) )
%W = NLAdjacency(image, localWinRad, NLSearchRad, NLVariance, hoodSize);
W = NLAdjacency(I, localWinRad, NLSearchRad, NLVariance, hoodSize);
A = NLWeights( W );
else
A = NLWeights( Win.^(h2in/NLVariance) );
end
if( ischar(scribs) )
scribs = double(imread(scribs)) ./255;
end
scribs = mean(scribs,3);
[rows, cols, chans] = size(I);
imsize = rows*cols;
centrows = rows - 2*localWinRad;
centcols = cols - 2*localWinRad;
centsize = centrows * centcols;
[constr, cvals] = scribData( scribs, localWinRad );
constr = reshape( constr, centsize, 1 );
cvals = reshape( cvals, centsize, 1 );
inds = reshape([1:imsize], rows, cols);
%centinds = reshape( (1:centsize), centrows, centcols );
centToImageInds = inds( (1+localWinRad):(rows-localWinRad), (1+localWinRad):(cols-localWinRad), :);
centToImageInds = reshape( centToImageInds, centsize, 1 );
I = reshape( I, imsize, chans );
% hood = zeros( imsize, hoodSize );
% f = ones( imsize, hoodSize);
hood = zeros( centsize, hoodSize );
f = ones( centsize, hoodSize);
%for i=1:imsize
j = 1;
for i=1:centsize
%[y, ndx] = sort( full(A(i,:)), 2, 'descend');
%[y, hood(i,:)] = weakSort( A(:,i), hoodSize, 'max');
[y, hood(i,:)] = weakSort( A(i,:)', hoodSize, 'max');
%[y, hood(i,:)] = mexWeakSort( A(i,:)', hoodSize );
%hood( i, : ) = ndx([1:hoodSize]);
%Q = diag(y([1:hoodSize]));
%y = y ./ sum(y(:));
Q = diag(y);
%X = I( ndx([1:hoodSize]), : );
%X = I( hood(i,:), : );
X = I( centToImageInds(hood(i,:)), : );
X(:, end+1) = ones(hoodSize,1);
%xprime = I( i, : );
xprime = I( centToImageInds(i), : );
xprime( :, end+1) = 1;
% Just testing the approximate rank of X.
% Remove these lines in practice.
%[U,S,V] = svd(X,0);
%rank(i) = sum( diag(S) > 0.004 ) / 4;
f(i,:) = xprime * ((X'*Q*X + lambda*diag([ones(1,chans), 0])) \ (X'*Q));
%f(i,:) = xprime * ((X'*Q*X + lambda*diag([ones(1,chans+1)])) \ (X'*Q));
if( j > centsize / 100 )
fprintf(1, '%.0f percent done\n', i/centsize*100);
j = 1;
else
j = j + 1;
end
end
clear inds I y ndx Q X xprime
fprintf(1, 'Done processing. Making matrices...\n');
%memory
%F = sparse( repmat([1:imsize]', 1, hoodSize), hood, f, imsize, imsize );
F = sparse( repmat([1:centsize]', 1, hoodSize), hood, f, centsize, centsize );
clear hood f
%% Solution Method 1. Stupid.
% C = spdiags( c * constr, 0, imsize, imsize );
% L2 = speye(imsize) - F;
% clear F;
%
% fprintf(1, 'Getting solution...\n');
%
% alpha = ((L2'*L2 + C) \ C) * cvals;
% alpha = reshape( alpha, rows, cols );
%% End Solution Method 1.
%% Solution Method 2. Better.
%L = speye(imsize,imsize) - F;
L = speye(centsize,centsize) - F;
clear F
L = L'*L;
% Get permutation matrix.
fprintf(1,'Getting permutation matrix...');
%P = sparse(imsize,imsize);
P = sparse(centsize,centsize);
k = sort(find(constr > 0));
j = size(k,1) + 1;
for i=1:size(k,1)
P(i,k(i)) = 1;
end
k = sort(find(constr == 0));
for i=1:size(k,1)
P(j-1 + i, k(i)) = 1;
end
fprintf(1,'done\n');
fprintf(1, 'Permuting laplacian...');
G = P*L*P'; % Modified laplacian.
clear L
fprintf(1, 'done\nPermuting constraints...');
vperm = P*cvals;
fprintf(1, 'done\n');
%G4 = G(j:imsize,j:imsize);
%G3 = G(j:imsize,1:j-1);
G4 = G(j:centsize,j:centsize);
G3 = G(j:centsize,1:j-1);
clear G;
ya = vperm(1:j-1);
clear vperm;
% For debugging...
%save debug.mat F G4 G3 ya;
fprintf(1, 'Solving...');
spparms('spumoni',1); % Verbose info about matrix solving.
% This works, but G4 is not usually invertible since it's basically a graph
% Laplacian, so we are really just
% asking Matlab for argmin( || G4 yb + G3 ya ||^2 ).
%yb = G4 \ (-G3*ya);
% Do LUPQ factorization to find solution since we expect that G4 can be
% decomposed into pretty sparse L and U under a permutation (P,Q).
% tic;
% [Lo,Up,p,q] = lu(G4);
% yb = q*(Up \ (Lo \ ( p*(-G3*ya) )));
% toc;
% clear Up Lo p q;
% Try Cholesky decomposition.
% tic;
% R = chol(G4);
% yb = R\(R'\(-G3*ya));
% toc;
% clear R;
% Cholesky decomposition with a sparsity inducing permutation matrix S.
% tic;
% [R,p,S] = chol(G4);
% yb = S*(R\(R'\(S'*(-G3*ya))));
% toc;
% clear R p S;
% Lower cholesky decomposition with permutation matrix S. Supposedly takes
% less memory and time. S'*G4*S = R*R'.
tic;
[R,p,S] = chol(G4, 'lower');
yb = S*(R' \ (R \ (S'*(-G3*ya))));
toc;
clear R p S;
fprintf(1, 'done\n');
alpha = P' * [ya;yb];
alpha = reshape(alpha,centrows,centcols);
%% End Solution method 2.
% Clip the matte.
alpha = min( 1, alpha );
alpha = max( 0, alpha );
%rank = reshape( rank, centrows, centcols );
% If we have a ground truth, get ssd.
if( ~exist('gt') || isempty(gt) )
return;
end
if( ischar(gt) )
G = double(imread(gt))./255;
else
G = gt;
end
G = mean(G,3);
%ssd = sum(sum((alpha(localWinRad+1:rows-localWinRad,localWinRad+1:cols-localWinRad) - G(localWinRad+1:rows-localWinRad,localWinRad+1:cols-localWinRad)).^2));
mse = sum(sum((alpha - G(localWinRad+1:rows-localWinRad,localWinRad+1:cols-localWinRad)).^2)) / (centrows*centcols);
fprintf(1, 'MSE: %f\nSSD: %f\n', mse, mse*centrows*centcols);