frcs-colour-transfer
/
pdf_transfer.m
101 строка · 2.5 Кб
1%
2% simple implementation of N-Dimensional PDF Transfer
3%
4% [DR] = pdf_transferND(D0, D1, rotations);
5%
6% D0, D1 = NxM matrix containing N-dimensional features
7% rotations = { {R_1}, ... , {R_n} } with R_i PxN
8%
9% note that we can use more than N projection axes. In this case P > N
10% and the inverse transformation is done by least mean square.
11% Using more than N axes leads to a more stable (but also slower)
12% convergence.
13%
14% (c) F. Pitie 2007
15%
16% see reference:
17% Automated colour grading using colour distribution transfer. (2007)
18% Computer Vision and Image Understanding.
19%
20function [DR] = pdf_transfer(D0, D1, Rotations, varargin)
21
22nb_iterations = length(Rotations);
23
24numvarargs = length(varargin);
25if numvarargs > 1
26error('pdf_transfer:TooManyInputs', ...
27'requires at most 1 optional input');
28end
29
30optargs = {1};
31optargs(1:numvarargs) = varargin;
32[relaxation] = optargs{:};
33
34prompt = '';
35
36for it=1:nb_iterations
37fprintf(repmat('\b',[1, length(prompt)]))
38prompt = sprintf('IDT iteration %02d / %02d', it, nb_iterations);
39fprintf(prompt);
40
41R = Rotations{it};
42nb_projs = size(R,1);
43
44% apply rotation
45
46D0R = R * D0;
47D1R = R * D1;
48D0R_ = zeros(size(D0));
49
50% get the marginals, match them, and apply transformation
51for i=1:nb_projs
52% get the data range
53datamin = min([D0R(i,:) D1R(i,:)])-eps;
54datamax = max([D0R(i,:) D1R(i,:)])+eps;
55u = (0:(300-1))/(300-1)*(datamax - datamin) + datamin;
56
57% get the projections
58p0R = hist(D0R(i,:), u);
59p1R = hist(D1R(i,:), u);
60
61% get the transport map
62f = pdf_transfer1D(p0R, p1R);
63
64% apply the mapping
65D0R_(i,:) = (interp1(u, f', D0R(i,:))-1)/(300-1)*(datamax-datamin) + datamin;
66end
67
68D0 = relaxation * (R \ (D0R_ - D0R)) + D0;
69end
70
71fprintf(repmat('\b',[1, length(prompt)]))
72
73
74DR = D0;
75
76end
77
78%
79% 1D - PDF Transfer
80%
81function f = pdf_transfer1D(pX,pY)
82nbins = max(size(pX));
83
84eps = 1e-6; % small damping term that faciliates the inversion
85
86PX = cumsum(pX + eps);
87PX = PX/PX(end);
88
89PY = cumsum(pY + eps);
90PY = PY/PY(end);
91
92% inversion
93
94f = interp1(PY, 0:nbins-1, PX, 'linear');
95f(PX<=PY(1)) = 0;
96f(PX>=PY(end)) = nbins-1;
97if sum(isnan(f))>0
98error('colour_transfer:pdf_transfer:NaN', ...
99'pdf_transfer has generated NaN values');
100end
101end
102
103