frcs-colour-transfer

Форк
0
/
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
%
20
function [DR] = pdf_transfer(D0, D1, Rotations, varargin)
21

22
nb_iterations = length(Rotations);
23

24
numvarargs = length(varargin);
25
if numvarargs > 1
26
    error('pdf_transfer:TooManyInputs', ...
27
        'requires at most 1 optional input');
28
end
29

30
optargs = {1};
31
optargs(1:numvarargs) = varargin;
32
[relaxation] = optargs{:};
33

34
prompt = '';
35

36
for it=1:nb_iterations
37
    fprintf(repmat('\b',[1, length(prompt)]))
38
    prompt = sprintf('IDT iteration %02d / %02d', it, nb_iterations);
39
    fprintf(prompt);
40
    
41
    R = Rotations{it};    
42
    nb_projs = size(R,1);
43
  
44
    % apply rotation
45
    
46
    D0R = R * D0;
47
    D1R = R * D1;
48
    D0R_ = zeros(size(D0));
49

50
    % get the marginals, match them, and apply transformation
51
    for i=1:nb_projs
52
        % get the data range
53
        datamin = min([D0R(i,:) D1R(i,:)])-eps;
54
        datamax = max([D0R(i,:) D1R(i,:)])+eps;
55
        u = (0:(300-1))/(300-1)*(datamax - datamin) + datamin;
56
        
57
        % get the projections
58
        p0R = hist(D0R(i,:), u);
59
        p1R = hist(D1R(i,:), u);
60

61
        % get the transport map
62
        f = pdf_transfer1D(p0R, p1R);
63
        
64
        % apply the mapping
65
        D0R_(i,:) = (interp1(u, f', D0R(i,:))-1)/(300-1)*(datamax-datamin) + datamin;
66
    end
67

68
    D0 = relaxation * (R \ (D0R_ - D0R)) + D0;
69
end
70

71
fprintf(repmat('\b',[1, length(prompt)]))
72

73

74
DR = D0;
75

76
end
77

78
%
79
% 1D - PDF Transfer
80
%
81
function f = pdf_transfer1D(pX,pY)
82
    nbins = max(size(pX));
83

84
    eps = 1e-6; % small damping term that faciliates the inversion
85
    
86
    PX = cumsum(pX + eps);
87
    PX = PX/PX(end);
88

89
    PY = cumsum(pY + eps);
90
    PY = PY/PY(end);
91

92
    % inversion
93

94
    f = interp1(PY, 0:nbins-1, PX, 'linear');
95
    f(PX<=PY(1)) = 0;
96
    f(PX>=PY(end)) = nbins-1;
97
    if sum(isnan(f))>0
98
        error('colour_transfer:pdf_transfer:NaN', ...
99
              'pdf_transfer has generated NaN values');
100
    end   
101
end
102

103

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.