Debian Codegraph @

cvd_src/convolution.cc

raw symbols SCIP
1 #include "cvd/convolution.h"
2 #include <algorithm>
3 #include <cmath>
4 using namespace std;
5
6 namespace CVD
7 {
8
9 namespace Internal
10 {
11
12 void convolveSeparableGray(unsigned char* I, unsigned int width, unsigned int height, const int kernel[], unsigned int size, int divisor)
13 {
14 std::vector<unsigned char> buffer(width > height ? width : height);
15 unsigned char* p = I;
16 unsigned int i, j, m;
17 if(size % 2 == 0)
18 {
19 throw Exceptions::Convolution::OddSizedKernelRequired("convolveSeparable");
20 }
21 for(i = height; i > 0; i--)
22 {
23 for(j = 0; j < width - size + 1; j++)
24 {
25 int sum = 0;
26 for(m = 0; m < size; m++)
27 sum += p[j + m] * kernel[m];
28 buffer[j] = (unsigned char)(sum / divisor);
29 }
30 memcpy(p + size / 2, &buffer.front(), width - size + 1);
31 p += width;
32 }
33 for(j = 0; j < width - size + 1; j++)
34 {
35 p = I + j + (size / 2) * width;
36 for(i = 0; i < height; i++)
37 buffer[i] = I[i * width + j];
38 for(i = 0; i < height - size + 1; i++)
39 {
40 int sum = 0;
41 for(m = 0; m < size; m++)
42 sum += buffer[i + m] * kernel[m];
43 *p = (unsigned char)(sum / divisor);
44 p += width;
45 }
46 }
47 }
48
49 };
50
51 void convolveGaussian5_1(Image<byte>& I)
52 {
53 int w = I.size().x;
54 int h = I.size().y;
55 int i, j;
56 for(j = 0; j < w; j++)
57 {
58 byte* src = I.data() + j;
59 byte* end = src + w * (h - 4);
60 while(src != end)
61 {
62 int sum = (3571 * (src[0] + src[4 * w])
63 + 16004 * (src[w] + src[3 * w])
64 + 26386 * src[2 * w]);
65 *(src) = static_cast<uint8_t>(sum >> 16);
66 src += w;
67 }
68 }
69 for(i = h - 5; i >= 0; i--)
70 {
71 byte* src = I.data() + i * w;
72 byte* end = src + w - 4;
73 while(src != end)
74 {
75 int sum = (3571 * (src[0] + src[4])
76 + 16004 * (src[1] + src[3])
77 + 26386 * src[2]);
78 *(src + 2 * w + 2) = static_cast<uint8_t>(sum >> 16);
79 ++src;
80 }
81 }
82 }
83
84 double compute_van_vliet_variance(const double d[])
85 {
86 const double a = d[0];
87 const double b = d[1];
88 const double num = 2 * (a * (a * (a - 2) + 1 + b * b) - 2 * b * b);
89 const double den1 = a * a - b * b - 2 * a + 1;
90 const double den2 = 2 * (a - 1) * b;
91 const double inv_den = 1.0 / (den1 * den1 + den2 * den2);
92
93 const double inv_den3 = 1.0 / ((d[2] - 1) * (d[2] - 1));
94
95 return 2 * (num * inv_den + d[2] * inv_den3);
96 }
97
98 double compute_van_vliet_variance(const double d[], double J[3])
99 {
100 const double a = d[0];
101 const double b = d[1];
102 const double num = 2 * (a * (a * (a - 2) + 1 + b * b) - 2 * b * b);
103 const double den1 = a * a - b * b - 2 * a + 1;
104 const double den2 = 2 * (a - 1) * b;
105 const double inv_den = 1.0 / (den1 * den1 + den2 * den2);
106 const double inv_den3 = 1.0 / ((d[2] - 1) * (d[2] - 1));
107
108 double N_D = num * inv_den;
109 J[0] = 2 * inv_den * (2 * (a * (3 * a - 4) + (1 + b * b)) - N_D * 4 * (den1 * (a - 1) + den2 * b));
110 J[1] = 2 * inv_den * (4 * b * (a - 2) - N_D * 4 * (den2 * (a - 1) - den1 * b));
111 J[2] = 2 * inv_den3 * (1 - d[2] * inv_den3 * 2 * (d[2] - 1));
112
113 return 2 * (N_D + d[2] * inv_den3);
114 }
115
116 void compute_scaling_jacobian(const double d[], double J[])
117 {
118 double rr = d[0] * d[0] + d[1] * d[1];
119 double lnr = 0.5 * log(rr);
120 double theta = atan2(d[1], d[0]);
121 J[0] = d[0] * lnr - d[1] * theta;
122 J[1] = d[0] * theta + d[1] * lnr;
123 J[2] = d[2] * log(d[2]);
124 }
125
126 void scale_d(double d[], double p)
127 {
128 double theta = atan2(d[1], d[0]);
129 double rr = d[0] * d[0] + d[1] * d[1];
130 double new_theta = theta * p;
131 double new_r = pow(rr, p * 0.5);
132 d[0] = new_r * cos(new_theta);
133 d[1] = new_r * sin(new_theta);
134 d[2] = pow(d[2], p);
135 }
136
137 void compute_van_vliet_scaled_d(double sigma, double d[])
138 {
139 // Approximately sigma = 2
140 d[0] = 1.41650;
141 d[1] = 1.00829;
142 d[2] = 1.86543;
143
144 // Cubic fitted to log(p) s.t. poles->poles^p gives sigma
145 const double A = 3.69892409013e-03;
146 const double B = -4.28967830150e-02;
147 const double C = -3.38065667167e-01;
148 const double D = 5.44264202732e-01;
149
150 double log_var = 2 * log(sigma);
151 double log_p = D + log_var * (C + log_var * (B + log_var * A));
152 double predicted_p = exp(log_p);
153
154 scale_d(d, predicted_p);
155
156 double total_p = 1;
157 const double target_var = sigma * sigma;
158
159 // Newton-Rapheson on scale of poles
160
161 for(int i = 0; i < 5; ++i)
162 {
163 double sj[3], vj[3];
164 compute_scaling_jacobian(d, sj);
165 double v = target_var - compute_van_vliet_variance(d, vj);
166 double step = v / (vj[0] * sj[0] + vj[1] * sj[1] + vj[2] * sj[2]);
167 if(abs(step) < 1e-6)
168 break;
169 double exp_step = exp(std::min(std::max(step, -1.0), 1.0));
170 scale_d(d, exp_step);
171 total_p *= exp_step;
172 }
173 }
174
175 void build_van_vliet_b(const double d[], double b[])
176 {
177 double B = 1.0 / (d[2] * (d[0] * d[0] + d[1] * d[1]));
178 b[2] = -B;
179 b[1] = B * (2 * d[0] + d[2]);
180 b[0] = -B * (d[0] * d[0] + d[1] * d[1] + 2 * (d[0] * d[2]));
181 }
182
183 void compute_van_vliet_b(double sigma, double b[])
184 {
185 double d[3];
186 compute_van_vliet_scaled_d(sigma, d);
187 build_van_vliet_b(d, b);
188 }
189
190 void compute_triggs_M(const double b[], double M[][3])
191 {
192 const double a1 = -b[0];
193 const double a2 = -b[1];
194 const double a3 = -b[2];
195
196 const double factor = 1.0 / ((1 + a1 - a2 + a3) * (1 - a1 - a2 - a3) * (1 + a2 + (a1 - a3) * a3));
197 M[0][0] = factor * (1 - a2 - a1 * a3 - a3 * a3);
198 M[0][1] = factor * (a3 + a1) * (a2 + a1 * a3);
199 M[0][2] = factor * a3 * (a1 + a2 * a3);
200 M[1][0] = a1 * M[0][0] + M[0][1];
201 M[1][1] = a2 * M[0][0] + M[0][2];
202 M[1][2] = a3 * M[0][0];
203 M[2][0] = a1 * M[1][0] + M[1][1];
204 M[2][1] = a2 * M[1][0] + M[1][2];
205 M[2][2] = a3 * M[1][0];
206 }
207
208 inline void forward_to_backward(const double M[][3], const double i_plus, const double inv_alpha, double& y1, double& y2, double& y3)
209 {
210 const double u_plus = i_plus * inv_alpha;
211 const double v_plus = u_plus * inv_alpha;
212 double x1 = y1 - u_plus, x2 = y2 - u_plus, x3 = y3 - u_plus;
213 y1 = M[0][0] * x1 + M[0][1] * x2 + M[0][2] * x3 + v_plus;
214 y2 = M[1][0] * x1 + M[1][1] * x2 + M[1][2] * x3 + v_plus;
215 y3 = M[2][0] * x1 + M[2][1] * x2 + M[2][2] * x3 + v_plus;
216 }
217
218 template <class T>
219 inline T clamp01(T x) { return x < 0 ? 0 : (x > 1 ? 1 : x); }
220
221 // Implementation of Young-van Vliet third-order recursive gaussian filter.
222 // See "Recursive Gaussian Derivative Filters", by van Vliet, Young and Verbeck, 1998
223 // and "Boundary Conditions for Young - van Vliet Recursive Filtering", by Triggs and Sdika, 2005
224 // Can result in values just outside of the input range
225 void van_vliet_blur(const double b[], const CVD::BasicImage<float> in, CVD::BasicImage<float> out)
226 {
227 assert(in.size() == out.size());
228 const int w = in.size().x;
229 const int h = in.size().y;
230
231 double M[3][3];
232 compute_triggs_M(b, M);
233
234 vector<double> tmp(w);
235
236 const double b0 = b[0];
237 const double b1 = b[1];
238 const double b2 = b[2];
239
240 const int rw = w % 4;
241 const double alpha = 1 + b0 + b1 + b2;
242 const double inv_alpha = 1.0 / alpha;
243
244 for(int i = 0; i < h; ++i)
245 {
246 const float* p = in[i] + w - 1;
247
248 double y3, y2, y1;
249 y3 = y2 = y1 = inv_alpha * *p;
250
251 for(int j = w - 1; j - 3 >= 0; j -= 4, p -= 4)
252 {
253 double y0 = p[0] - (b0 * y1 + b1 * y2 + b2 * y3);
254 y3 = p[-1] - (b0 * y0 + b1 * y1 + b2 * y2);
255 y2 = p[-2] - (b0 * y3 + b1 * y0 + b2 * y1);
256 y1 = p[-3] - (b0 * y2 + b1 * y3 + b2 * y0);
257 tmp[j] = y0;
258 tmp[j - 1] = y3;
259 tmp[j - 2] = y2;
260 tmp[j - 3] = y1;
261 }
262
263 for(int j = rw - 1; j >= 0; --j, --p)
264 {
265 double y0 = p[0] - (b0 * y1 + b1 * y2 + b2 * y3);
266 tmp[j] = y0;
267 y3 = y2;
268 y2 = y1;
269 y1 = y0;
270 }
271
272 {
273 const double i_plus = p[1];
274 double y0 = i_plus - (b0 * y1 + b1 * y2 + b2 * y3);
275 y3 = y2;
276 y2 = y1;
277 y1 = y0;
278 forward_to_backward(M, i_plus, inv_alpha, y1, y2, y3);
279 }
280
281 float* o = out[i];
282 for(int j = 0; j + 3 < w; j += 4, o += 4)
283 {
284 double y0 = tmp[j] - (b0 * y1 + b1 * y2 + b2 * y3);
285 y3 = tmp[j + 1] - (b0 * y0 + b1 * y1 + b2 * y2);
286 y2 = tmp[j + 2] - (b0 * y3 + b1 * y0 + b2 * y1);
287 y1 = tmp[j + 3] - (b0 * y2 + b1 * y3 + b2 * y0);
288 o[0] = (float)y0;
289 o[1] = (float)y3;
290 o[2] = (float)y2;
291 o[3] = (float)y1;
292 }
293
294 for(int j = w - rw; j < w; ++j, ++o)
295 {
296 double y0 = tmp[j] - (b0 * y1 + b1 * y2 + b2 * y3);
297 o[0] = (float)y0;
298 y3 = y2;
299 y2 = y1;
300 y1 = y0;
301 }
302 }
303
304 tmp.resize(h);
305
306 const double alpha_fourth = alpha * alpha * alpha * alpha;
307
308 const int rh = h % 4;
309 const int stride = out.row_stride();
310 for(int i = 0; i < w; ++i)
311 {
312 double y3, y2, y1;
313
314 const float* in = out[h - 1] + i;
315 y3 = y2 = y1 = inv_alpha * *in;
316
317 for(int j = h - 1; j - 3 >= 0; j -= 4, in -= stride)
318 {
319 double y0 = in[0] - (b0 * y1 + b1 * y2 + b2 * y3);
320 in -= stride;
321 y3 = in[0] - (b0 * y0 + b1 * y1 + b2 * y2);
322 in -= stride;
323 y2 = in[0] - (b0 * y3 + b1 * y0 + b2 * y1);
324 in -= stride;
325 y1 = in[0] - (b0 * y2 + b1 * y3 + b2 * y0);
326 tmp[j] = y0;
327 tmp[j - 1] = y3;
328 tmp[j - 2] = y2;
329 tmp[j - 3] = y1;
330 }
331
332 for(int j = rh - 1; j >= 0; --j, in -= stride)
333 {
334 double y0 = in[0] - (b0 * y1 + b1 * y2 + b2 * y3);
335 tmp[j] = y0;
336 y3 = y2;
337 y2 = y1;
338 y1 = y0;
339 }
340
341 {
342 const double i_plus = in[stride];
343 double y0 = i_plus - (b0 * y1 + b1 * y2 + b2 * y3);
344 y3 = y2;
345 y2 = y1;
346 y1 = y0;
347 forward_to_backward(M, i_plus, inv_alpha, y1, y2, y3);
348 }
349
350 float* o = out[0] + i;
351 for(int j = 0; j + 3 < h; j += 4)
352 {
353 double y0 = tmp[j] - (b0 * y1 + b1 * y2 + b2 * y3);
354 y3 = tmp[j + 1] - (b0 * y0 + b1 * y1 + b2 * y2);
355 y2 = tmp[j + 2] - (b0 * y3 + b1 * y0 + b2 * y1);
356 y1 = tmp[j + 3] - (b0 * y2 + b1 * y3 + b2 * y0);
357 o[0] = (float)(alpha_fourth * y0); //clamp01(alpha_fourth*y0);
358 o += stride;
359 o[0] = (float)(alpha_fourth * y3); //clamp01(alpha_fourth*y3);
360 o += stride;
361 o[0] = (float)(alpha_fourth * y2); //clamp01(alpha_fourth*y2);
362 o += stride;
363 o[0] = (float)(alpha_fourth * y1); //clamp01(alpha_fourth*y1);
364 o += stride;
365 }
366
367 for(int j = h - rh; j < h; ++j, o += stride)
368 {
369 double y0 = tmp[j] - (b0 * y1 + b1 * y2 + b2 * y3);
370 o[0] = (float)(alpha_fourth * y0); //clamp01(alpha_fourth*y0);
371 y3 = y2;
372 y2 = y1;
373 y1 = y0;
374 }
375 }
376 }
377
378 };