-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_depth_confidence.c
229 lines (184 loc) · 4.43 KB
/
compute_depth_confidence.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
#include "header.h"
void compute_depth_confidence(
int *image_arr,
int *depthmap_arr,
int width,
int height,
int radius,
double gamma_p,
double gamma_c,
double sigma,
double *conf_arr
)
{
double dist_dbl;
int i;
int j;
int dist_c_siz;
double *dist_c;
int dist_c_ind;
int dist_p_siz;
double *dist_p;
int dist_p_ind;
int pixel;
int depth;
double weight_sum;
int i_radius;
int j_radius;
int i2;
int j2;
int pixel2;
int depth2;
double weight_p;
double weight_c;
int cind;
double weight;
int nbr_pixels;
double *E_depth_arr;
double *E_depth_squared_arr;
double E_depth;
double E_depth_squared;
double V_depth;
double *V_depth_arr;
double conf;
double val;
/*
Tabulate proximity strengths dist_p
(array of size (2*radius+1)^2)
*/
dist_p_siz= (2*radius+1)*(2*radius+1);
dist_p= (double *)calloc(dist_p_siz,sizeof(double));
dist_p_ind= 0;
for ( i= -radius ; i<= +radius ; i++ ) {
for ( j= -radius ; j<= +radius ; j++ ) {
dist_dbl= (double)(i*i+j*j);
dist_dbl= sqrt(dist_dbl);
dist_p[dist_p_ind]= exp(-2.*dist_dbl/gamma_p);
if ( i == 0 ) {
fprintf(stdout,"proximity weight (i=0) = %g\n",
dist_p[dist_p_ind]);
}
dist_p_ind++;
}
}
/*
Tabulate color similarity strengths dist_c
(array of size 3*255+1)
*/
dist_c_siz= 3*255+1;
dist_c= (double *)calloc(dist_c_siz,sizeof(double));
for ( dist_c_ind= 0 ;
dist_c_ind< dist_c_siz ;
dist_c_ind++ ) {
dist_dbl= (double)dist_c_ind;
dist_dbl/= 3.;
dist_c[dist_c_ind]= exp(-dist_dbl/gamma_c);
}
/*
Compute the expectation for the depth
by applying the convolution kernel to the depth
Compute the expectation for the depth squared
by applying the convolution kernel to the depth squared
Compute the variance for the depth
*/
nbr_pixels= width*height;
E_depth_arr= (double *)calloc(nbr_pixels,sizeof(double));
E_depth_squared_arr= (double *)calloc(nbr_pixels,sizeof(double));
V_depth_arr= (double *)calloc(nbr_pixels,sizeof(double));
for ( i= 0 ; i< height ; i++ ) {
for ( j= 0 ; j< width ; j++ ) {
pixel= i*width+j;
depth= depthmap_arr[pixel];
/*
Apply edge-preserving filter
*/
weight_sum= 0;
E_depth= 0;
E_depth_squared= 0;
for ( i_radius= -radius ; i_radius<= +radius ; i_radius++ ) {
for ( j_radius= -radius ; j_radius<= +radius ; j_radius++ ) {
i2= i+i_radius;
j2= j+j_radius;
pixel2= i2*width+j2;
/*
Check if neighboring pixel is out of bounds
*/
if ( i2 < 0 )
continue;
if ( i2 > height-1 )
continue;
if ( j2 < 0 )
continue;
if ( j2 > width-1 )
continue;
/*
Neighboring pixel is in bounds
*/
depth2= depthmap_arr[pixel2];
/*
Get proximity weight
*/
dist_p_ind= (radius+i_radius)*(2*radius+1)+radius+j_radius;
weight_p= dist_p[dist_p_ind];
/*
Get color similarity weight
*/
dist_c_ind= 0;
for ( cind= 0 ; cind< 3 ; cind++ ) {
dist_c_ind+= abs(image_arr[3*pixel2+cind]-image_arr[3*pixel+cind]);
}
weight_c= dist_c[dist_c_ind];
/*
Compute total weight
*/
weight= weight_p * weight_c;
/*
Add weighted contribution of neighboring pixel
*/
E_depth+= weight*(double)depth2;
E_depth_squared+= weight*(double)depth2*(double)depth2;
weight_sum+= weight;
}
}
E_depth/= weight_sum;
E_depth_squared/= weight_sum;
/*
Compute the depth variance
*/
V_depth= E_depth_squared-E_depth*E_depth;
V_depth_arr[pixel]= V_depth;
}
}
/*
Compute the confidence
*/
for ( i= 0 ; i< height ; i++ ) {
for ( j= 0 ; j< width ; j++ ) {
pixel= i*width+j;
V_depth= V_depth_arr[pixel];
val= -V_depth/(2.*sigma*sigma);
conf= exp(val);
conf_arr[pixel]= conf;
}
}
/*
Free dist_p
*/
free(dist_p);
/*
Free dist_c
*/
free(dist_c);
/*
Free E_depth_arr
*/
free(E_depth_arr);
/*
Free E_depth_squared_arr
*/
free(E_depth_squared_arr);
/*
Free V_depth_arr
*/
free(V_depth_arr);
}