-
Notifications
You must be signed in to change notification settings - Fork 28
/
colour_transfer_MKL.m
48 lines (36 loc) · 843 Bytes
/
colour_transfer_MKL.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
%
% colour transfer algorithm based on linear Monge-Kantorovitch solution
%
% IR = colour_transfer_MKL(I_original, I_target, nbiterations);
%
% (c) F. Pitie 2007
%
% see reference:
%
%
function IR = colour_transfer_MKL(I0, I1)
if (ndims(I0)~=3)
error('pictures must have 3 dimensions');
end
X0 = reshape(I0, [], size(I0,3));
X1 = reshape(I1, [], size(I1,3));
A = cov(X0);
B = cov(X1);
T = MKL(A, B);
mX0 = repmat(mean(X0), [size(X0,1) 1]);
mX1 = repmat(mean(X1), [size(X0,1) 1]);
XR = (X0-mX0)*T + mX1;
IR = reshape(XR, size(I0));
function [T] = MKL(A, B)
N = size(A,1);
[Ua,Da2] = eig(A);
Da2 = diag(Da2);
Da2(Da2<0) = 0;
Da = diag(sqrt(Da2 + eps));
C = Da*Ua'*B*Ua*Da;
[Uc,Dc2] = eig(C);
Dc2 = diag(Dc2);
Dc2(Dc2<0) = 0;
Dc = diag(sqrt(Dc2 + eps));
Da_inv = diag(1./(diag(Da)));
T = Ua*Da_inv*Uc*Dc*Uc'*Da_inv*Ua';