-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_image_analogy_LPE.m
185 lines (162 loc) · 8.29 KB
/
create_image_analogy_LPE.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
function [ B_prime_pyramid,B_prime_pyramid_ini,debug ] = create_image_analogy_LPE( A, A_prime, B )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
global N_BIG;
global NUM_FEATURES;
global NNF;
global SaveFolderName;
[B_height, B_width, ~] = size(B);
[A_height, A_width, ~] = size(A);
%% Create luminance maps
% A = rgb2ntsc(A);
% A_prime = rgb2ntsc(A_prime);
% B = rgb2ntsc(B);
%% Construct Gaussian pyramids for A, A' and B
A_pyramid = {A};
A_prime_pyramid = {A_prime};
B_pyramid = {B};
B_prime_pyramid = {zeros(size(A_prime,1),size(A_prime,2),3)};
B_prime_pyramid_ini = {zeros(size(A_prime,1),size(A_prime,2),3)};
L=6;
min_size=0.2;
scale_factor=(1-min_size)/(L-1);
% Was 50, 50
for k=1:L-1
scaledsize= [floor(size(A_pyramid{1},1)*(min_size+scale_factor*(L-k-1))) floor(size(A_pyramid{1},2)*(min_size+scale_factor*(L-k-1)))];
A_pyramid{end+1} = imresize(A_pyramid{1}, scaledsize);
A_prime_pyramid{end+1} = imresize(A_prime_pyramid{1}, scaledsize);
B_pyramid{end+1} = imresize(B_pyramid{1},scaledsize);
B_prime_pyramid{end+1} = imresize(B_prime_pyramid{1}, scaledsize);
B_prime_pyramid_ini{end+1} = imresize(B_prime_pyramid_ini{1}, scaledsize);
% [A_height, A_width, ~] = size(A_pyramid{end});
end
% L = size(A_pyramid, 2);
% Extended pyramids, for ANN only
A_pyramid_extend = cell(L);
B_pyramid_extend = cell(L);
% [B_extend_h, B_extend_w, ~] = size(B_pyramid_extend{1});
for l=1:L %Çø¼ä0-255
% A_pyramid_extend{l} = extend_image_LPE(A_pyramid{l}, 0, 1);
% B_pyramid_extend{l} = extend_image_LPE(B_pyramid{l}, 0,1);
% A_pyramid_extend{l} = cat(3,A_pyramid{l}(:,:,4),rgb2gray(A_pyramid{l}(:,:,1:3)),rgb2gray(A_pyramid{l}(:,:,10:12)),rgb2gray(A_pyramid{l}(:,:,4:6)),rgb2gray(A_pyramid{l}(:,:,7:9)),rgb2gray(A_pyramid{l}(:,:,13:15)),rgb2gray(A_pyramid{l}(:,:,19:21)));
A_pyramid_extend{l} = cat(3,A_pyramid{l}(:,:,1),...
A_pyramid{l}(:,:,2),...
A_pyramid{l}(:,:,3),...
A_pyramid{l}(:,:,4),...
A_pyramid{l}(:,:,5),...
A_pyramid{l}(:,:,6),...
A_pyramid{l}(:,:,7),...
A_pyramid{l}(:,:,8),...
A_pyramid{l}(:,:,9),...
A_pyramid{l}(:,:,10),...
A_pyramid{l}(:,:,11),...
A_pyramid{l}(:,:,12),...
A_pyramid{l}(:,:,13),...
A_pyramid{l}(:,:,14),...
A_pyramid{l}(:,:,15),...
A_pyramid{l}(:,:,16),...
A_pyramid{l}(:,:,17),...
A_pyramid{l}(:,:,18),...
A_pyramid{l}(:,:,19),...
A_pyramid{l}(:,:,20),...
A_pyramid{l}(:,:,21));
% B_pyramid_extend{l} = cat(3,B_pyramid{l}(:,:,4),rgb2gray(B_pyramid{l}(:,:,1:3)),rgb2gray(B_pyramid{l}(:,:,10:12)),rgb2gray(B_pyramid{l}(:,:,4:6)),rgb2gray(B_pyramid{l}(:,:,7:9)),rgb2gray(B_pyramid{l}(:,:,13:15)),rgb2gray(B_pyramid{l}(:,:,19:21)));
B_pyramid_extend{l} = cat(3,B_pyramid{l}(:,:,1),...
B_pyramid{l}(:,:,2),...
B_pyramid{l}(:,:,3),...
B_pyramid{l}(:,:,4),...
B_pyramid{l}(:,:,5),...
B_pyramid{l}(:,:,6),...
B_pyramid{l}(:,:,7),...
B_pyramid{l}(:,:,8),...
B_pyramid{l}(:,:,9),...
B_pyramid{l}(:,:,10),...
B_pyramid{l}(:,:,11),...
B_pyramid{l}(:,:,12),...
B_pyramid{l}(:,:,13),...
B_pyramid{l}(:,:,14),...
B_pyramid{l}(:,:,15),...
B_pyramid{l}(:,:,16),...
B_pyramid{l}(:,:,17),...
B_pyramid{l}(:,:,18),...
B_pyramid{l}(:,:,19),...
B_pyramid{l}(:,:,20),...
B_pyramid{l}(:,:,21));
end
fprintf('Finding best match...\n\n');
%% Main loop, find the best match
% TODO: doing single scale for quicker testin right now -- remove this
% line.
% L = 1;
psz = 5;
w = (psz-1)/2;
NNF_pre=[];
debug={};
for l = L:-1:1
fprintf('\nl: %d/%d\n===========\n', l, L);
% Loop over B'
B_prime_sz = size(B_pyramid_extend{l});
%
[NNF_l, debug]=PatchMatch_LPE(B_pyramid_extend,A_pyramid_extend,l,L,NNF_pre,debug,psz);
NNF_pre=NNF_l;
% [NNF_l, debug]=PatchMatch_LPE_uniform(B_pyramid_extend,A_pyramid_extend,l,L,NNF_pre,debug,psz);
% NNF_pre=NNF_l;
% imshow(debug.source_used/10)
% fprintf('Reconstructing Initial NNF Image... ');
% for ii = (1+w):psz:B_prime_sz(1)-w
% for jj = (1+w):psz:B_prime_sz(2)-w
% temp_i=debug.NNF_ini{l}(ii,jj,1)-w:debug.NNF_ini{l}(ii,jj,1)+w;
% temp_j=debug.NNF_ini{l}(ii,jj,2)-w:debug.NNF_ini{l}(ii,jj,2)+w;
% B_prime_pyramid_ini{l}(ii-w:ii+w,jj-w:jj+w,:)...
% = A_prime_pyramid{l}(...
% temp_i,...
% temp_j,...
% :);
% end
% end
% fprintf('Done!\n');
fprintf('Reconstructing Output Image... ');
for ii = (1+w):1:B_prime_sz(1)-w
for jj = (1+w):1:B_prime_sz(2)-w
B_prime_pyramid{l}(ii,jj,:) ...
= A_prime_pyramid{l}(NNF_l(ii,jj,1),NNF_l(ii,jj,2),:);
end
end
fprintf('Done!\n');
%
% fprintf('Reconstructing Output Image... ');
% for ii = (1+w):1:B_prime_sz(1)-w
% for jj = (1+w):1:B_prime_sz(2)-w
% B_prime_pyramid{l}(ii,jj,:) ...
% = mean(mean(A_prime_pyramid{l}(NNF_l(ii,jj,1)-w:NNF_l(ii,jj,1)+w,NNF_l(ii,jj,2)-w:NNF_l(ii,jj,2)+w,:)));
% %= A_prime_pyramid{l}(NNF_l(ii,jj,1),NNF_l(ii,jj,2),:);
% end
% end
% fprintf('Done!\n');
%
% fprintf('Reconstructing Output Image... ');
% for ii = (1+w):psz:B_prime_sz(1)-w
% for jj = (1+w):psz:B_prime_sz(2)-w
% B_prime_pyramid{l}(ii-w:ii+w,jj-w:jj+w,:) ...
% = A_prime_pyramid{l}(NNF_l(ii,jj,1)-w:NNF_l(ii,jj,1)+w,NNF_l(ii,jj,2)-w:NNF_l(ii,jj,2)+w,:);
% end
% end
% fprintf('Done!\n');
figure;
imshow(B_prime_pyramid{l}/255);
reconstImg = uint8(B_prime_pyramid{l});
reconstImg_ini = uint8(B_prime_pyramid_ini{l});
imwrite(reconstImg_ini,fullfile('results',SaveFolderName,strcat('B_prime_ini',num2str(l),'.bmp')),'BMP');
imwrite(reconstImg,fullfile('results',SaveFolderName,strcat('B_prime_level',num2str(l),'.bmp')),'BMP');
end
B_prime = B_prime_pyramid{1};
%figure(1),imshow(B_prime/255);
% for l=L:-1:1
% reconstImg = uint8(B_prime_pyramid{l});
% reconstImg_ini = uint8(B_prime_pyramid_ini{l});
% imwrite(reconstImg_ini,fullfile('results',SaveFolderName,strcat('B_prime_ini',num2str(l),'.bmp')),'BMP');
% imwrite(reconstImg,fullfile('results',SaveFolderName,strcat('B_prime_level',num2str(l),'.bmp')),'BMP');
%
% end
% imwrite(debug.source_used/max(max(debug.source_used)),fullfile('results',SaveFolderName,strcat('source_used',num2str(l),'.bmp')),'BMP');
end