-
Notifications
You must be signed in to change notification settings - Fork 9
/
System_Matrix_TV.m
261 lines (216 loc) · 7.68 KB
/
System_Matrix_TV.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
249
250
251
252
253
254
255
256
257
258
259
260
261
% TV/TGV variational optical flow (stereo)
%
% Author: Christoph Vogel
function G = System_Matrix_TV( alpha1, M, N, single, mask, constraints, g)
% part 1 : setup the system matrix (sparse)
% order ux, uy, wxx, wxy, wyx,wyy, same for v
if ~exist('single', 'var')
single =0;
end
% missing: insert 0 rows at appropriate positions
test = reshape(1:N*M, M,N)';
test1 = test(1:end,1:end-1);
test2 = test(1:end,2:end);
% dual positions - leave out first row, and first column:
testDX = reshape(1:N*M, M,N);
testDX = testDX(1:end,2:end);
testDX = testDX(:)';
testDY = reshape(1:N*M, M,N);
testDY = testDY(2:end,1:end)';
testDY = testDY(:)';
% gradient: U
guxids_x = cat( 1, [1:M*N-M], [M+1:M*N] );
guxids_y = cat( 1, testDX, testDX );
guxids_v = cat( 1, alpha1 * ones(1,M*N-M), -alpha1 * ones(1,M*N-M));
% use the standard w here, full image, start at 2nd column
%gwxids_x = 2*M*N + [M+1:M*N];
%gwxids_y = testDX;
%gwxids_v = -alpha1 * ones(1,(M*N-M));
ypos = M*N;
guyids_x = cat(1,test1(:)', test2(:)');
guyids_y = ypos + cat( 1, testDY, testDY );
guyids_v = cat(1, alpha1*ones(1,numel(guyids_x)/2), -alpha1*ones(1,numel(guyids_x)/2));
% use the standard w here, full image, start at 2nd column
if exist('mask', 'var')
% mask=false(size(mask));
% mask(1:200,:)=true;
% mask(1:1000) = true;
mask_out_x = cat( 1, mask(1:M*N-M)|mask(M+1:M*N), mask(1:M*N-M)|mask(M+1:M*N) );
mask = mask';
% mask = false(size(mask));
mask1 = mask(1:end,1:end-1);
mask2 = mask(1:end, 2:end);
mask1 = mask1 | mask2;
mask_out_y = cat(1,mask1(:)', mask1(:)');
guyids_x(mask_out_y) = [];
guyids_y(mask_out_y) = [];
guyids_v(mask_out_y) = [];
guxids_x(mask_out_x) = [];
guxids_y(mask_out_x) = [];
guxids_v(mask_out_x) = [];
% guxids_v(mask_out_x) = 0;
% guyids_v(mask_out_y) = 0;
end
%gwyids_x = 3*M*N + test2(:)';
%gwyids_y = ypos+testDY;
%gwyids_v = -alpha1 * ones(1,(M*N-N));
%
if ~exist('g','var')
xids_1 = guxids_x;
yids_1 = guxids_y;
vids_1 = guxids_v;
xids_2 = guyids_x;
yids_2 = guyids_y;
vids_2 = guyids_v;
xids = cat(1, xids_1(:), xids_2(:));
yids = cat(1, yids_1(:), yids_2(:));
vids = cat(1, vids_1(:), vids_2(:));
% G = sparse(yids, xids, vids);
if ~single
%%%%%%%%%%%%% Gradient V
xids_1 = N*M+guxids_x;
yids_1 = 2*N*M+guxids_y;
vids_1 = guxids_v;
xids_2 = N*M+guyids_x;
yids_2 = 2*N*M+guyids_y;
vids_2 = guyids_v;
Uxids = cat(1, xids_1(:), xids_2(:));
Uyids = cat(1, yids_1(:), yids_2(:));
Uvids = cat(1, vids_1(:), vids_2(:));
xids = cat(1, xids(:), Uxids(:));
yids = cat(1, yids(:), Uyids(:));
vids = cat(1, vids(:), Uvids(:));
% if (2*N>max(xids) || 2*M>max(yids))
% xids(end+1) = 2*M;
% yids(end+1) = 2*N;
% vids(end+1) = 0.000001;
% end
% else
% last entry:
% xids(end+1) = max(xids);
% yids(end+1) = max(yids);
% vids(end+1) = 0.000001;
% if (N>max(xids) || M>max(yids))
% xids(end+1) = N*M;
% yids(end+1) = 2*N*M;
% vids(end+1) = 0.000001;
% end
end
else % weighted, sort and combine by y index!
idy = reshape(1:N*M, M,N);
id_dx1 = [zeros(M,1), idy(:,1:end-1)];
id_dx2 = [zeros(M,1), idy(:,2:end)];
id_dy1 = [zeros(1,N); idy(1:end-1,:)];
id_dy2 = [zeros(1,N); idy(2:end,:)];
g1 = squeeze(g(:,:,1));
g2 = squeeze(g(:,:,2));
g3 = squeeze(g(:,:,3));
g4 = squeeze(g(:,:,4));
xidux = cat(1, id_dx1(:)', id_dx2(:)', id_dy1(:)', id_dy2(:)');
xiduy = cat(1, id_dx1(:)', id_dx2(:)', id_dy1(:)', id_dy2(:)');
xidvx = N*M+xiduy;%cat(1, id_dx1(:)', id_dx2(:)', id_dy1(:)', id_dy2(:)', 3*N*M+idy(:)');
xidvy = N*M+cat(1, id_dx1(:)', id_dx2(:)', id_dy1(:)', id_dy2(:)');
yid = repmat( idy(:)', 4, 1);
valx = alpha1*cat(1, g1(:)', -g1(:)', g2(:)', -g2(:)');
valy = alpha1*cat(1, g3(:)', -g3(:)', g4(:)', -g4(:)');
validity = cat(1, id_dx1(:)', id_dx2(:)', id_dy1(:)', id_dy2(:)');
validity =validity>0; % also remove 0 entries if 0 in valxes
valvalx = valx.*validity;
valvaly = valy.*validity;
% invalidate whole rows:
valvalx = sum(abs(valvalx(1:4,:)), 1) > 0;
valvaly = sum(abs(valvaly(1:4,:)), 1) > 0;
validateX = abs(valx)>0 & bsxfun(@and, valvalx, validity);
validateY = abs(valy)>0 & bsxfun(@and, valvaly, validity);
xidux = xidux(validateX);
xiduy = xiduy(validateY);
xidvx = xidvx(validateX);
xidvy = xidvy(validateY);
yidx = yid(validateX);
yidy = yid(validateY);
valx = valx(validateX);
valy = valy(validateY);
if ~single
xids = cat(1, xidux(:), xiduy(:), xidvx(:), xidvy(:));
yids = cat(1, yidx(:), N*M+yidy(:),2*N*M+yidx(:),3*N*M+yidy(:));
vids = cat(1, valx(:), valy(:), valx(:), valy(:));
% if (2*N>max(xids) || 2*M>max(yids))
% xids(end+1) = 2*N;
% yids(end+1) = 2*M;
% vids(end+1) = 0.000001;
% end
else
xids = cat(1, xidux(:), xiduy(:));
yids = cat(1, yidx(:), N*M+yidy(:));
vids = cat(1, valx(:), valy(:));
% last entry:
% if (N>max(xids) || M>max(yids))
% xids(end+1) = M;
% yids(end+1) = N;
% vids(end+1) = 0.000001;
% end
end
end
if exist('constraints', 'var') && isfield(constraints, 'weights')
% append constraints:
% find 4 neighs p_i of constriaint position
www = constraints.weights;
ff = cat( 1, floor (constraints.p(:,1))', floor (constraints.p(:,2))');
fc = bsxfun(@plus, ff, [0,1]');%cat( 1, floor (constraints.p(:,1))', ceil (constraints.p(:,2))');
cf = bsxfun(@plus, ff, [1,0]'); %cat( 1, ceil (constraints.p(:,1))', floor (constraints.p(:,2))');
cc = ff+1;%cat( 1, ceil (constraints.p(:,1))', ceil (constraints.p(:,2))');
% and their weights w_i - sum w_i = 1
wff = sum((ff-constraints.p').^2,1);
wfc = sum((fc-constraints.p').^2,1);
wcf = sum((cf-constraints.p').^2,1);
wcc = sum((cc-constraints.p').^2,1);
[~,eliminate] = max(cat(1,wff,wfc,wcf,wcc));
test = cat(3,ff,fc,cf,cc);
lastrow = 4*N*M;%max(yids(:));
newxids=zeros(1,3*2*size(constraints.p, 1));
newyids=zeros(1,3*2*size(constraints.p, 1));
newvids=zeros(1,3*2*size(constraints.p, 1));
for i = 1: size(constraints.p, 1)
points = squeeze(test(:,i,:));
points(:, eliminate(i) ) = [];
ids = sub2ind ([M,N], points(2,:), points(1,:))';
weights = cat(1,points, ones(1,3))\[constraints.p(i,:),1]';
% high constraints also on smoothness at positions:
% these are the posistions the point is present
%{
%[find(xids==ids(1)), find(xids==ids(2)), find(xids==ids(3)]
% the y ids where a point is present, now find all positions - ugh
theYs = unique(yids([find(xids==ids(1)); find(xids==ids(2)); find(xids==ids(3))]));
theids = find(ismember(yids, theYs));
vids( theids ) = vids( theids ) * www(i);
% v flow
theYs = unique(yids([find(xids==ids(1)+N*M); find(xids==ids(2)+N*M); find(xids==ids(3)+N*M)]));
theids = find(ismember(yids, theYs));
vids( theids ) = vids( theids ) * www(i);
%}
% too slow:
% xids = cat (1, xids, ids);
% yids = cat (1, yids, repmat( lastrow+2*i-1, [3,1]));
% vids = cat (1, vids, www(i)*weights );
%
% xids = cat (1, xids, ids+N*M);
% yids = cat (1, yids, repmat( lastrow+2*i, [3,1]));
% vids = cat (1, vids, www(i)*weights );
newxids(6*(i-1)+1:6*i) = [ids;ids+N*M];
%newyids(6*(i-1)+1:6*i) = [repmat( lastrow+2*i-1, [3,1]); repmat( lastrow+2*i, [3,1])];
newyids(6*(i-1)+1:6*i) = [lastrow+2*i-1;lastrow+2*i-1;lastrow+2*i-1;lastrow+2*i;lastrow+2*i;lastrow+2*i];
newvids(6*(i-1)+1:6*i) = [www(i)*weights;www(i)*weights];
%union( union (find(xids==ids(1)), find(xids==ids(2))), find(xids==ids(3)))
end
xids = cat (1, xids, newxids');
yids = cat (1, yids, newyids');
vids = cat (1, vids, newvids');
% add a row to the matrix:
% sum w_i u_i = u
% weighted sum of their flow vectors is the constraint flow vector
end
if single
G = sparse(yids, xids, vids, 2*N*M, N*M);
else
G = sparse(yids, xids, vids, 4*N*M+ numel(constraints.uv(:)), 2*N*M);
end