-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.m
executable file
·248 lines (221 loc) · 8.33 KB
/
assignment.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
%% Operate
img_s = imread('./Input_images/girl.png');
sz = size(img_s);
mask = imread('./Input_images/girl2.png');
szm = size(mask);
if(length(szm)==3)
if(szm(3)==3)
mask = rgb2gray(mask);
end
end
img_p = padarray(img_s,[4 4],'replicate','both');
%% vis
figure(1);
imshow(img_s);
figure(2);
imshow(mask);
%imshow(img_p);
disp(size(img_s));
%% Grad
[ix1,iy1] = gradient(double(img_s(:,:,1)));
[ix2,iy2] = gradient(double(img_s(:,:,2)));
[ix3,iy3] = gradient(double(img_s(:,:,3)));
%disp(size(ix));
%ix = abs(ix1)+abs(ix2)+abs(ix3);
%iy = abs(iy1)+abs(iy2)+abs(iy3);
ix = ix1+ix2+ix3;
iy = iy1+iy2+iy3;
figure(3);
imshow(ix);
figure(4);
imshow(iy);
%% Rotation
temp = ix;
ix = -iy;
iy = temp;
ix = (ix)/(255);
iy = (iy)/(255);
%ix = im2bw(ix,0.1);
%iy = im2bw(iy,0.1);
%disp(ix/255);
figure(5);
imshow(ix);
figure(6);
imshow(iy);
%% Gaussian
%ig = imgaussfilt(double(img_s(:,:,1)),2);
%disp(ig);
%figure();
%imshow(ig/255);
%% Normal
img_im = img_s; % Inpainted image
source = ~mask; % source region
C = double(source);
patch_size = 11.0;
sz = size(img_s);
D = repmat(0.001,size(mask));
img_p = padarray(img_s,[floor(patch_size/2) floor(patch_size/2)],'replicate','both');
img_c = padarray(img_s,[floor(patch_size/2) floor(patch_size/2)],'replicate','both');
C_new = padarray(C,[floor(patch_size/2) floor(patch_size/2)],'replicate','both'); % confidence matrix
C_old = padarray(C,[floor(patch_size/2) floor(patch_size/2)],'replicate','both');
C_nc = C_new;
img_pnc = img_p;
while(any(mask(:)))
delta = edge(mask,'approxcanny');
%figure();
%imshow(delta);
[cnx, cny] = find(abs(delta)>0);
%disp(cnx);
[Npx1,Npy1] = gradient(double(mask));
[Npx2,Npy2] = gradient(255-double(mask));
%figure();
%imshow(Npx1);
%disp(Npx1);
Npx =abs(double((Npx1)-(Npx2))/(255*255));
Npy =abs(double((Npy1)-(Npy2))/(255*255));
Npx = im2bw(Npx,0.000001);
Npy = im2bw(Npy,0.000001);
figure(7);
imshow(abs(Npx));
figure(8);
imshow(abs(Npy));
%Npx =double(Npx2/255);
%Npy =double(Npy2/255);
%disp(size(Npx));
%figure();
%imshow(abs(255*Npx));
%figure();
%imshow(abs(255*Npy));
cn = [cnx cny];
N_delta = zeros(size(cn));
for i=1:length(cn)
N_delta(i,1) = Npx(cn(i,1),cn(i,2));
N_delta(i,2) = Npy(cn(i,1),cn(i,2));
D(cn(i,1),cn(i,2)) = D(cn(i,1),cn(i,2))+100*abs(ix(cn(i,1),cn(i,2)).*N_delta(i,1))+100*abs((iy(cn(i,1),cn(i,2)).*N_delta(i,2)));
%disp(N_delta(i));
end
figure(5);
imshow(abs(D));
% confidence calculation
sum_p=0.0;
max_ind = floor(patch_size/2);
C_new = padarray(C,[floor(patch_size/2) floor(patch_size/2)],'replicate','both'); % confidence matrix
for i=1:length(cn)
midx = cn(i,1);
midy = cn(i,2);
for j=(midx-floor(patch_size/2)):(midx+floor(patch_size/2))
for k=(midy-floor(patch_size/2)):(midy+floor(patch_size/2))
sum_p = sum_p + double(C_new(j+max_ind,k+max_ind));
end
end
C_new(midx+max_ind,midy+max_ind) = (double(sum_p)/square(patch_size));
sum_p=0.0;
end
% Priority calculation
C_ch = C_new(max_ind+1:sz(1)+max_ind,max_ind+1:sz(2)+max_ind);
P = C_ch.*D;
if (~isempty(cn))
cur_patch = [cn(1,1),cn(1,2)];
for i=1:length(cn)
if (abs(P(cur_patch(1),cur_patch(2)))<abs(P(cn(i,1),cn(i,2))))
cur_patch(1) = cn(i,1);
cur_patch(2) = cn(i,2);
end
end
else
break;
end
% Best exemplar determination
temp_img = zeros(patch_size,patch_size);
overlap = 10000000;
patch_img = double(img_c(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind,:));
C_patch = double(C_new(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind));
C_patch = im2bw(C_patch,0.0001);
for i=max_ind+1:sz(1)+max_ind
for j=max_ind+1:sz(2)+max_ind
temp_img = double(img_pnc(i-max_ind:i+max_ind,j-max_ind:j+max_ind,:));
count = 0.0;
C_img = double(C_nc(i-max_ind:i+max_ind,j-max_ind:j+max_ind));
tar_pat = 1;
for t=1:patch_size
for s=1:patch_size
if(C_img(t,s)<0.0001)
tar_pat = 0;
end
end
end
%disp(i);
if(tar_pat==1)
conf = 1.0;
for m=1:patch_size
for n=1:patch_size
if(C_patch(m,n)>0)
a1 = (temp_img(m,n,1));
a2 = (temp_img(m,n,2));
a3 = (temp_img(m,n,3));
b1 = (patch_img(m,n,1));
b2 = (patch_img(m,n,2));
b3 = (patch_img(m,n,3));
%count = count+sum(square(temp_img(m,n,:)-patch_img(m,n,:)));
temp_ls = [a1,a2,a3];
patch_ls = [b1,b2,b3];
t_h = 0.33*a1+0.33*a2+0.33*a3;
p_h = 0.33*b1+0.33*b2+0.33*b3;
wr = b1/(b1+b2+b3);
wg = b2/(b1+b2+b3);
wb = b3/(b1+b2+b3);
dist1 = abs(a1-b1);
dist2 = abs(a2-b2);
dist3 = abs(a3-b3);
wg_s = dist1+dist2+dist3+dist1*dist2*dist3;
count = count+sum(wg_s);
end
end
end
count = count/conf;
if(count<overlap)
%disp(count);
%disp(temp_img);
%disp(patch_img);
overlap = count;
ptx = i;
pty = j;
%disp(ptx);
%disp(pty);
end
end
%disp(overlap);
end
end
% Replace best patch
img_c(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind,1) = img_pnc(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind,1);
img_c(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind,2) = img_pnc(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind,2);
img_c(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind,3) = img_pnc(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind,3);
figure(12);
imshow(img_pnc(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind,:))
img_im = img_c(max_ind+1:sz(1)+max_ind,max_ind+1:sz(2)+max_ind,:);
figure(9);
imshow(img_im);
% Update confidence and mask
mask_ch = padarray(mask,[floor(patch_size/2) floor(patch_size/2)],'replicate','both');
mask_ch(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind) = 0;
mask = mask_ch(max_ind+1:sz(1)+max_ind,max_ind+1:sz(2)+max_ind);
figure(10);
imshow(mask);
C_old(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind) = C_new(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind);
%C = im2bw(C,0.0001);
ix_ch = padarray(ix,[floor(patch_size/2) floor(patch_size/2)],'replicate','both');
iy_ch = padarray(iy,[floor(patch_size/2) floor(patch_size/2)],'replicate','both');
ix_ch(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind) = ix_ch(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind);
iy_ch(cur_patch(1):cur_patch(1)+2*max_ind,cur_patch(2):cur_patch(2)+2*max_ind) = iy_ch(ptx-max_ind:ptx+max_ind,pty-max_ind:pty+max_ind);
ix = ix_ch(max_ind+1:sz(1)+max_ind,max_ind+1:sz(2)+max_ind);
iy = iy_ch(max_ind+1:sz(1)+max_ind,max_ind+1:sz(2)+max_ind);
%imshow(mask);
C = C_old(max_ind+1:sz(1)+max_ind,max_ind+1:sz(2)+max_ind);
end
img_last = img_im;
imshow(img_last);
%%
%figure(9);
%imshow(img_im);
imwrite(img_im,'./Result_images/girl_im11.jpg','jpg');