-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExp1_just_inpainting.m
176 lines (153 loc) · 5.67 KB
/
Exp1_just_inpainting.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
%% General
res = 512;
width = round(0.2*512);
D = diffusion_map(zeros(512),1,0);
split = floor((res-width)/2) + [0,width-1];
dsplit = round(linspace(1,res,8));
dsplit = [dsplit(2),dsplit(4); dsplit(5),dsplit(7)];
mask = true(res);
mask(floor(res/4):floor(3*res/4),floor(res/4):floor(3*res/4)) = false;
% mask(floor(2.5*res/6):floor(3.5*res/6),floor(res/4):floor(3*res/4)) = false;
%% Rectangle
gt = zeros(res);
gt(split(1):split(2),:) = 1;
c1 = ones(res);
c1(split(1)+[-1,0,1],:) = 0;c1(split(2)+[-1,0,1],:) = 0;
c2 = ones(res);
cvx_begin quiet
variable u1([res,res])
Du = reshape(D.smooth_deriv(u1,0,1,1),res*res,2);
minimise sum(norms([1.*Du(:,1),1.*Du(:,2)],2,2))
subject to
u1(mask) == gt(mask)
cvx_end
cvx_begin quiet
variable u2([res,res])
Du = reshape(D.smooth_deriv(u2,0,1,1),res*res,2);
minimise sum(norms([c1(:).*Du(:,1),c2(:).*Du(:,2)],2,2))
subject to
u2(mask) == gt(mask)
cvx_end
clear cvx*
figure;
subplot(2,2,1);imagesc((gt+1).*mask-1);title('Input');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,2);imagesc(u1);title('TV Output');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,3);imagesc(c1);caxis([0,1]); title('c_1');set(gca,'fontsize', 18);
subplot(2,2,4);imagesc(u2);title('DTV Output');set(gca,'fontsize', 18);caxis([-1,1]);
%% Noisy Rectangle
gt = zeros(res);
gt(split(1):split(2),:) = 1;
rng(1);noise = 2*randn(size(gt));
c1 = ones(res);
c1(split(1)+(-2:2),:) = 0;c1(split(2)+(-2:2),:) = 0;
c2 = ones(res);
c1 = imgaussfilt(c1,2);c2 = imgaussfilt(c2,2);
cvx_begin quiet
variable u1([res,res])
Du = norms(D.smooth_deriv(u1,0,1,1),2,3);
% minimise sum_square_abs(u1(:)-gt(:)-noise(:)) + 10*sum(Du(:))
minimise sum(Du(:))
subject to
sum_square_abs(u1(:)-gt(:)-noise(:)) <= sum_square_abs(noise(:))
cvx_end
cvx_begin quiet
variable u2([res,res])
Du = norms(cat(3,c1,c2).*D.smooth_deriv(u2,0,1,1),2,3);
% minimise sum_square_abs(u2(:)-gt(:)-noise(:)) + 10*sum(Du(:))
minimise sum(Du(:))
subject to
sum_square_abs(u2(:)-gt(:)-noise(:)) <= sum_square_abs(noise(:))
cvx_end
clear cvx*
figure;
subplot(2,2,1);imagesc(gt+noise);title('Input');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,2);imagesc(u1);title('TV Output');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,3);imagesc(c1);caxis([0,1]); title('c_1');set(gca,'fontsize', 18);
subplot(2,2,4);imagesc(u2);title('DTV Output');set(gca,'fontsize', 18);caxis([-1,1]);
%% Parallel Rectangles
gt = zeros(res);
gt(dsplit(1,1):dsplit(1,2),:) = 1;
gt(dsplit(2,1):dsplit(2,2),:) = 1;
c1 = ones(res);
c1(dsplit(1,1)+[-1,0,1],:) = 0;c1(dsplit(1,2)+[-1,0,1],:) = 0;
c1(dsplit(2,1)+[-1,0,1],:) = 0;c1(dsplit(2,2)+[-1,0,1],:) = 0;
c2 = ones(res);
% c1 = imgaussfilt(c1,2);c2 = imgaussfilt(c2,2);
cvx_begin quiet
variable u1([res,res])
Du = norms(D.smooth_deriv(u1,0,1,1),2,3);
minimise sum(Du(:))
subject to
u1(mask) == gt(mask)
cvx_end
cvx_begin quiet
variable u2([res,res])
Du = reshape(D.smooth_deriv(u2,0,1,1),res*res,2);
minimise sum(norms([c1(:).*Du(:,1),c2(:).*Du(:,2)],2,2))
subject to
u2(mask) == gt(mask)
cvx_end
clear cvx* tmpD
figure;
subplot(2,2,1);imagesc((gt+1).*mask-1);title('Input');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,2);imagesc(u1);title('TV Output');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,3);imagesc(c1);caxis([0,1]); title('c_1');set(gca,'fontsize', 18);
subplot(2,2,4);imagesc(u2);title('DTV Output');set(gca,'fontsize', 18);caxis([-1,1]);
%% Intersecting Rectangles
gt = zeros(res);
gt(split(1):split(2),:) = 1;
gt(:,split(1):split(2)) = 1;
c1 = .7*ones(res);
c1(split(1)+[-1,0,1],:) = 0;c1(split(2)+[-1,0,1],:) = 0;
c1(:,split(1)+[-1,0,1],:) = 0;c1(:,split(2)+[-1,0,1]) = 0;
c2 = ones(res);
c1 = imgaussfilt(c1,2);c2 = imgaussfilt(c2,2);
tmpD = diffusion_map(gt, 16, 16, 'tmp', [1,1,2]);
tmpD.c1_val = c1.^2;tmpD.c2_val = c2.^2;
cvx_begin quiet
variable u1([res,res])
Du = norms(D.smooth_deriv(u1,0,1,1),2,3);
minimise sum(Du(:))
subject to
u1(mask) == gt(mask)
cvx_end
cvx_begin quiet
variable u2([res,res])
Du = reshape(tmpD*D.smooth_deriv(u2,0,1,1),res*res,2);
minimise sum(norms(Du,2,2))
subject to
u2(mask) == gt(mask)
cvx_end
clear cvx* tmpD
figure;
subplot(2,2,1);imagesc((gt+1).*mask-1);title('Input');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,2);imagesc(u1);title('TV Output');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,3);imagesc(c1);caxis([0,1]); title('c_1');set(gca,'fontsize', 18);
subplot(2,2,4);imagesc(u2);title('DTV Output');set(gca,'fontsize', 18);caxis([-1,1]);
%% Gradient Rectangle
gt = zeros(res);
gt(split(1):split(2),:) = repmat(linspace(0,1,res),width,1);
c1 = ones(res);
c1(split(1)+[-1,0,1],:) = 0;c1(split(2)+[-1,0,1],:) = 0;
c2 = ones(res);
% c1 = imgaussfilt(c1,2);c2 = imgaussfilt(c2,2);
cvx_begin quiet
variable u1([res,res])
Du = norms(D.smooth_deriv(u1,0,1,1),2,3);
minimise sum(Du(:))
subject to
u1(mask) == gt(mask)
cvx_end
cvx_begin quiet
variable u2([res,res])
Du = reshape(D.smooth_deriv(u2,0,1,1),res*res,2);
minimise sum(norms([c1(:).*Du(:,1),c2(:).*Du(:,2)],2,2))
subject to
u2(mask) == gt(mask)
cvx_end
clear cvx*
figure;
subplot(2,2,1);imagesc((gt+1).*mask-1);title('Input');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,2);imagesc(u1);title('TV Output');set(gca,'fontsize', 18);caxis([-1,1]);
subplot(2,2,3);imagesc(c1);caxis([0,1]); title('c_1');set(gca,'fontsize', 18);
subplot(2,2,4);imagesc(u2);title('DTV Output');set(gca,'fontsize', 18);caxis([-1,1]);