-
Notifications
You must be signed in to change notification settings - Fork 1
/
stereo_build_Cn_Cs_matrix.c
275 lines (207 loc) · 3.9 KB
/
stereo_build_Cn_Cs_matrix.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "header.h"
void stereo_build_Cn_Cs_matrix(
int *image_arr,
int width,
int height,
mesh_struct *mesh,
matrix_int_crs_struct *splat_matrix,
int *blur_matrix[5],
double **ptr_Cn_matrix,
int **ptr_Cs_matrix
)
{
arrayHeader *mesh_verts;
int maxInd;
int nbr_verts;
int nbr_pixels;
int *m;
int vert;
int mass;
int pixel;
double *n;
int iter;
double *Cn;
int *Cs;
int i;
double val;
double new_val;
double max_norm;
double tol;
int ind;
double *Bn;
int dim;
int k;
/*
Get nbr of pixels
*/
nbr_pixels= width*height;
/*
Get nbr of verts
*/
mesh_verts= mesh->mesh_verts;
maxInd= arrayMaxId(*mesh_verts);
nbr_verts= maxInd+1;
/*
Build the vert mass vector
*/
myCalloc(m,int,nbr_verts,sizeof(int));
for ( vert= 0 ; vert< nbr_verts ; vert++ ) {
/*
vert represents the row index
*/
/*
Compute the mass of the vert
*/
mass= 0;
for ( ind= splat_matrix->row_ptr[vert] ;
ind< splat_matrix->row_ptr[vert+1] ;
ind++ ) {
pixel= splat_matrix->col_ind[ind];
mass++;
}
/*
Print out the mass
*/
/*
fprintf(stdout,"vert= %d mass= %d\n",vert,mass);
*/
/*
Store the mass
*/
m[vert]= mass;
}
/*
Allocate memory for the n vector
*/
myCalloc(n,double,nbr_verts,sizeof(double));
/*
Allocate memory for the B n vector
*/
myCalloc(Bn,double,nbr_verts,sizeof(double));
/*
Initialize the n vector
*/
for ( vert= 0 ; vert< nbr_verts ; vert++ )
n[vert]= 1;
iter= 0;
tol= 1.0e-6;
START:
iter++;
/*
Let's compute B n
as B1 n + B2 n + B3 n + B4 n + B5 n
*/
/*
Initialize B n
*/
for ( vert= 0 ; vert< nbr_verts ; vert++ )
Bn[vert]= 0;
for ( dim= 0 ; dim< 5 ; dim++ ) {
/*
Print blur_matrix[dim]
*/
/*
for ( i= 0 ; i< nbr_verts ; i++ ) {
fprintf(stdout,"i= %d blur_matrix[%d]= %d %d %d\n",
i,dim,
blur_matrix[dim][i*3+0],
blur_matrix[dim][i*3+1],
blur_matrix[dim][i*3+2]);
}
*/
/*
Recall that the blur matrix is
nbr of row = nbr of verts
nbr of colums = 3
column 0 corresponds to neighboring vert on the left
column 1 corresponds to vert
column 2 corresponds to neighboring vert on the right
It's a 1,2,1 blur
*/
for ( i= 0 ; i< nbr_verts ; i++ ) {
k= blur_matrix[dim][i*3+0];
Bn[i]+= (1./4.)*n[k];
k= blur_matrix[dim][i*3+1];
if ( k != i ) {
error_handler("stereo_build_Cn_Cs_matrix");
}
Bn[i]+= (2./4.)*n[k];
k= blur_matrix[dim][i*3+2];
Bn[i]+= (1./4.)*n[k];
}
}
/*
Assign sqrt ( ( n x m ) / ( B n ) ) to n
*/
/*
Let's compute the max norm of vector n
to check for convergence
*/
max_norm= 0;
for ( vert= 0 ; vert< nbr_verts ; vert++ ) {
val= n[vert];
new_val= sqrt(n[vert]*(double)m[vert]/Bn[vert]);
if ( fabs(new_val-val) > max_norm )
max_norm= fabs(new_val-val);
n[vert]= new_val;
}
fprintf(stdout,"iter = %d max_norm= %lg\n",iter,max_norm);
/*
Use max_iter to stop
*/
/*
if ( iter < max_iter )
goto START;
*/
/*
Use tolerance on the max norm to stop
*/
if ( !( max_norm < tol ) )
goto START;
/*
Allocate memory for the Cn matrix
Cn = diag n
It is a diagonal matrix, so we're gonna store it as a vector
*/
myCalloc(Cn,double,nbr_verts,sizeof(double));
for ( vert= 0 ; vert< nbr_verts ; vert++ )
Cn[vert]= n[vert];
/*
Allocate memory for the Cs matrix
Cs = diag m
It is a diagonal matrix, so we're gonna store it as a vector
*/
myCalloc(Cs,int,nbr_verts,sizeof(int));
for ( vert= 0 ; vert< nbr_verts ; vert++ )
Cs[vert]= m[vert];
/*
Check if ( Cn B Cn ) 1 = m
*/
/*
stereo_build_Cn_Cs_matrix_check(
image_arr,
width,
height,
mesh,
splat_matrix,
blur_matrix,
m,
Cn,
Cs
);
*/
/*
Free m
*/
myFree(m);
/*
Free n
*/
myFree(n);
/*
Free Bn
*/
myFree(Bn);
(*ptr_Cn_matrix)= Cn;
(*ptr_Cs_matrix)= Cs;
}