Debian Codegraph @

cvd/canny.h

raw symbols SCIP
1 #ifndef CVD_INCLUDE_EDGE_HPP
2 #define CVD_INCLUDE_EDGE_HPP
3
4 #include <TooN/TooN.h>
5 #include <cmath>
6 #include <cvd/convolution.h>
7 #include <cvd/image.h>
8 #include <queue>
9
10 namespace CVD
11 {
12
13 template <class T>
14 void canny_gradient(const BasicImage<T>& im, BasicImage<TooN::Vector<2>>& grad, const double sigma = 1.0)
15 {
16 Image<T> out(im.size());
17 convolveGaussian(im, out, sigma);
18
19 for(int y = 1; y < im.size().y - 1; y++)
20 {
21 for(int x = 1; x < im.size().x - 1; x++)
22 {
23 const double xgrad = ((out[y - 1][x - 1] + 2 * out[y][x - 1] + out[y + 1][x - 1]) - (out[y - 1][x + 1] + 2 * out[y][x + 1] + out[y + 1][x + 1]));
24 const double ygrad = ((out[y - 1][x - 1] + 2 * out[y - 1][x] + out[y - 1][x + 1]) - (out[y + 1][x - 1] + 2 * out[y + 1][x] + out[y + 1][x + 1]));
25 grad[y][x][0] = xgrad;
26 grad[y][x][1] = ygrad;
27 }
28 }
29 }
30
31 template <class T>
32 void canny(const BasicImage<T>& im, BasicImage<T>& out, const double sigma = 1.0, const double lower_threshold = 0.1, const double upper_threshold = 0.2)
33 {
34 //GRANTA_ASSERT(im.size() == out.size());
35 const int STRONG_EDGE = 4;
36 const int WEAK_EDGE = 5;
37 const int REJECT_EDGE = 6;
38 const double pi = std::atan(1.0) * 4;
39 Image<double> mags(out.size());
40 Image<byte> bins(out.size());
41 convolveGaussian(im, out, sigma);
42 /*
43 Compute the gradients, the gradient directions, and bin each directions into 4 discrete bins:
44
45 * horizontal (id=0): angles in [-22.5, 22.5) (midangle 0) and [157.5, 202.5) (midangle 180)
46 * diagonal (id=1): angles in [22.5, 67.5) (midangle 45)
47 * vertical (id=2): angles in [67.5, 112.5) (midangle 90)
48 * anti-diagonal (id=3): angles in [112.5, 157.5) (midangle 135)
49
50 For each pixel location, the bin id of its pixel's discretized direction is stored in bins[y][x].
51 Additionally, the magnitude is stored in mags[y][x] for the non-maximal suppression step.
52 */
53 for(int y = 1; y < im.size().y - 1; y++)
54 {
55 for(int x = 1; x < im.size().x - 1; x++)
56 {
57 const double ygrad = ((out[y - 1][x - 1] + 2 * out[y - 1][x] + out[y - 1][x + 1]) - (out[y + 1][x - 1] + 2 * out[y + 1][x] + out[y + 1][x + 1]));
58 const double xgrad = ((out[y - 1][x - 1] + 2 * out[y][x - 1] + out[y + 1][x - 1]) - (out[y - 1][x + 1] + 2 * out[y][x + 1] + out[y + 1][x + 1]));
59 const double mag = std::sqrt(xgrad * xgrad + ygrad * ygrad);
60 mags[y][x] = mag;
61 if(mag >= lower_threshold)
62 {
63 double dir = std::atan(ygrad / xgrad);
64 if(dir < 0)
65 {
66 dir += pi;
67 }
68 dir = (180.0 * dir) / pi;
69 int dirbin = static_cast<int>((dir + 22.5) / 45.0) % 4;
70 bins[y][x] = dirbin;
71 }
72 else
73 {
74 bins[y][x] = REJECT_EDGE;
75 }
76 }
77 }
78 //cout << "Total time gradient: " << timer.get_time() << endl;
79
80 /*
81 Perform non-maximal suppression, rejecting any pixel with a gradient magnitude less than its
82 neighbors (along the gradient direction).
83 */
84 std::queue<ImageRef> edge_locations;
85
86 /* Perform non-maximal suppression along the gradient direction and also perform
87 hysterisis thresholding. */
88 for(int y = 2; y < im.size().y - 2; y++)
89 {
90 for(int x = 2; x < im.size().x - 2; x++)
91 {
92 out[y][x] = 0.0;
93 const double mag = mags[y][x];
94
95 /* Skip rejected pixels. */
96 if(bins[y][x] == REJECT_EDGE)
97 {
98 continue;
99 }
100 bool edge = false;
101 /* If a pixel is an edge then a pixel has a higher gradient
102 magnitude than its neighbors along the gradient direction
103 (not an if and only if). Reject those pixels that are not
104 local maxima along the gradient direction. */
105 switch(bins[y][x])
106 {
107 case 0: /* horizontal gradient direction */
108 if(mag > mags[y][x - 1] && mag > mags[y][x + 1])
109 {
110 edge = true;
111 }
112 break;
113 case 1: /* diagonal gradient direction */
114 if(mag > mags[y - 1][x - 1] && mag > mags[y + 1][x + 1])
115 {
116 edge = true;
117 }
118 break;
119 case 2: /* vertical gradient direction */
120 if(mag > mags[y - 1][x] && mag > mags[y + 1][x])
121 {
122 edge = true;
123 }
124 break;
125 case 3: /* anti-diagonal gradient direction */
126 if(mag > mags[y + 1][x - 1] && mag > mags[y - 1][x + 1])
127 {
128 edge = true;
129 }
130 break;
131 }
132 /*
133 If the magnitude is greater than the upper threshold, push it onto the queue so it
134 can be traced later.
135 */
136 if(edge && mag > upper_threshold)
137 {
138 bins[y][x] = STRONG_EDGE; // strong edge
139 edge_locations.push(ImageRef(x, y));
140 }
141 /*
142 Otherwise, if it's above the lower threshold, mark it as weak.
143 */
144 else if(edge && mag > lower_threshold)
145 {
146 bins[y][x] = WEAK_EDGE; // weak edge
147 }
148 /*
149 Since we've already rejected weak edges, we shouldn't get here.
150 */
151 else
152 {
153 bins[y][x] = REJECT_EDGE; // reject edge
154 }
155 }
156 }
157 //cout << "total time hysteris: " << timer.get_time() << endl;
158
159 /* While there are still unvisited accepted (strong) edge locations... */
160 while(edge_locations.size() > 0)
161 {
162 /* Pop the next strong edge. */
163 ImageRef loc(edge_locations.front());
164 edge_locations.pop();
165
166 /* Record its magnitude. */
167 out[loc] = mags[loc];
168
169 /* Mark any weak edges connected to it as strong and push them onto the stack.
170 These edges will never be pushed twice since only weak edges can be pushed
171 onto the stack at this point.*/
172 if(bins[loc.y][loc.x] == STRONG_EDGE)
173 {
174 if(bins[loc.y - 1][loc.x - 1] == WEAK_EDGE)
175 {
176 bins[loc.y - 1][loc.x - 1] = STRONG_EDGE;
177 edge_locations.push(ImageRef(loc.x - 1, loc.y - 1));
178 }
179 if(bins[loc.y - 1][loc.x] == WEAK_EDGE)
180 {
181 bins[loc.y - 1][loc.x] = STRONG_EDGE;
182 edge_locations.push(ImageRef(loc.x, loc.y - 1));
183 }
184 if(bins[loc.y - 1][loc.x + 1] == WEAK_EDGE)
185 {
186 bins[loc.y - 1][loc.x + 1] = STRONG_EDGE;
187 edge_locations.push(ImageRef(loc.x + 1, loc.y - 1));
188 }
189 if(bins[loc.y][loc.x - 1] == WEAK_EDGE)
190 {
191 bins[loc.y][loc.x - 1] = STRONG_EDGE;
192 edge_locations.push(ImageRef(loc.x - 1, loc.y));
193 }
194 if(bins[loc.y][loc.x] == WEAK_EDGE)
195 {
196 bins[loc.y][loc.x] = STRONG_EDGE;
197 edge_locations.push(ImageRef(loc.x, loc.y));
198 }
199 if(bins[loc.y][loc.x + 1] == WEAK_EDGE)
200 {
201 bins[loc.y][loc.x + 1] = STRONG_EDGE;
202 edge_locations.push(ImageRef(loc.x + 1, loc.y));
203 }
204 if(bins[loc.y + 1][loc.x - 1] == WEAK_EDGE)
205 {
206 bins[loc.y + 1][loc.x - 1] = STRONG_EDGE;
207 edge_locations.push(ImageRef(loc.x - 1, loc.y + 1));
208 }
209 if(bins[loc.y + 1][loc.x] == WEAK_EDGE)
210 {
211 bins[loc.y + 1][loc.x] = STRONG_EDGE;
212 edge_locations.push(ImageRef(loc.x, loc.y + 1));
213 }
214 if(bins[loc.y + 1][loc.x + 1] == WEAK_EDGE)
215 {
216 bins[loc.y + 1][loc.x + 1] = STRONG_EDGE;
217 edge_locations.push(ImageRef(loc.x + 1, loc.y + 1));
218 }
219 }
220 }
221 //cout << "total time tracing: " << timer.get_time() << endl;
222 }
223
224 template <class T>
225 void canny2(const BasicImage<T>& im, BasicImage<T>& out, double sigma = 1.0, double lower_threshold = 0.1, double upper_threshold = 0.2)
226 {
227 //GRANTA_ASSERT(im.size() == out.size());
228 const int STRONG_EDGE = 4;
229 const int WEAK_EDGE = 5;
230 const int REJECT_EDGE = 6;
231 Image<double> mags(out.size());
232 Image<byte> bins(out.size());
233 convolveGaussian(im, out, sigma);
234 lower_threshold = lower_threshold * lower_threshold;
235 upper_threshold = upper_threshold * upper_threshold;
236 /*
237 Compute the gradients, the gradient directions, and bin each directions into 4 discrete bins:
238
239 * horizontal (id=0): angles in [-22.5, 22.5) (midangle 0) and [157.5, 202.5) (midangle 180)
240 * diagonal (id=1): angles in [22.5, 67.5) (midangle 45)
241 * vertical (id=2): angles in [67.5, 112.5) (midangle 90)
242 * anti-diagonal (id=3): angles in [112.5, 157.5) (midangle 135)
243
244 For each pixel location, the bin id of its pixel's discretized direction is stored in bins[y][x].
245 Additionally, the magnitude is stored in mags[y][x] for the non-maximal suppression step.
246 */
247 for(int y = 1; y < im.size().y - 1; y++)
248 {
249 for(int x = 1; x < im.size().x - 1; x++)
250 {
251 const double ygrad = ((out[y - 1][x - 1] + 2 * out[y - 1][x] + out[y - 1][x + 1]) - (out[y + 1][x - 1] + 2 * out[y + 1][x] + out[y + 1][x + 1]));
252 const double xgrad = ((out[y - 1][x - 1] + 2 * out[y][x - 1] + out[y + 1][x - 1]) - (out[y - 1][x + 1] + 2 * out[y][x + 1] + out[y + 1][x + 1]));
253 const double axgrad = std::abs(xgrad);
254 const double aygrad = std::abs(ygrad);
255 //const double mag = std::sqrt(xgrad * xgrad + ygrad * ygrad);
256 const double mag = xgrad * xgrad + ygrad * ygrad;
257 mags[y][x] = mag;
258 if(mag >= lower_threshold)
259 {
260 const bool xgrad_pos = xgrad >= 0;
261 const bool ygrad_pos = ygrad >= 0;
262 const bool xgrad_neg = xgrad <= 0;
263 const bool ygrad_neg = ygrad <= 0;
264 const bool axgrad_sup = axgrad >= aygrad;
265 const bool aygrad_sup = axgrad <= aygrad;
266 if((xgrad_pos && ygrad_pos && axgrad_sup)
267 || (xgrad_neg && ygrad_neg && axgrad_sup))
268 {
269 bins[y][x] = 0;
270 }
271 else if((xgrad_pos && ygrad_pos && aygrad_sup)
272 || (xgrad_neg && ygrad_neg && aygrad_sup))
273 {
274 bins[y][x] = 1;
275 }
276 else if((xgrad_neg && ygrad_pos && aygrad_sup)
277 || (xgrad_pos && ygrad_neg && aygrad_sup))
278 {
279 bins[y][x] = 2;
280 }
281 else if((xgrad_neg && ygrad_pos && axgrad_sup)
282 || (xgrad_pos && ygrad_neg && axgrad_sup))
283 {
284 bins[y][x] = 3;
285 }
286 }
287 else
288 {
289 bins[y][x] = REJECT_EDGE;
290 }
291 }
292 }
293 //cout << "Total time gradient: " << timer.get_time() << endl;
294
295 /*
296 Perform non-maximal suppression, rejecting any pixel with a gradient magnitude less than its
297 neighbors (along the gradient direction).
298 */
299 std::queue<ImageRef> edge_locations;
300
301 for(int y = 2; y < im.size().y - 2; y++)
302 {
303 out[y][0] = 0;
304 out[y][1] = 0;
305 out[y][im.size().x - 2] = 0;
306 out[y][im.size().x - 1] = 0;
307 }
308
309 for(int x = 1; x < im.size().x - 1; x++)
310 {
311 out[0][x] = 0;
312 out[1][x] = 0;
313 out[im.size().y - 2][x] = 0;
314 out[im.size().y - 1][x] = 0;
315 }
316
317 /* Perform non-maximal suppression along the gradient direction and also perform
318 hysterisis thresholding. */
319 for(int y = 2; y < im.size().y - 2; y++)
320 {
321 for(int x = 2; x < im.size().x - 2; x++)
322 {
323 out[y][x] = 0.0;
324 const double mag = mags[y][x];
325 /* Skip rejected pixels. */
326 if(bins[y][x] == REJECT_EDGE)
327 {
328 continue;
329 }
330 bool edge = false;
331 /* If a pixel is an edge then a pixel has a higher gradient
332 magnitude than its neighbors along the gradient direction
333 (not an if and only if). Reject those pixels that are not
334 local maxima along the gradient direction. */
335 switch(bins[y][x])
336 {
337 case 0: /* horizontal gradient direction */
338 if(mag > mags[y][x - 1] && mag > mags[y][x + 1])
339 {
340 edge = true;
341 }
342 break;
343 case 1: /* diagonal gradient direction */
344 if(mag > mags[y - 1][x - 1] && mag > mags[y + 1][x + 1])
345 {
346 edge = true;
347 }
348 break;
349 case 2: /* vertical gradient direction */
350 if(mag > mags[y - 1][x] && mag > mags[y + 1][x])
351 {
352 edge = true;
353 }
354 break;
355 case 3: /* anti-diagonal gradient direction */
356 if(mag > mags[y + 1][x - 1] && mag > mags[y - 1][x + 1])
357 {
358 edge = true;
359 }
360 break;
361 }
362 /*
363 If the magnitude is greater than the upper threshold, push it onto the queue so it
364 can be traced later.
365 */
366 if(edge && mag > upper_threshold)
367 {
368 bins[y][x] = STRONG_EDGE; // strong edge
369 edge_locations.push(ImageRef(x, y));
370 }
371 /*
372 Otherwise, if it's above the lower threshold, mark it as weak.
373 */
374 else if(edge && mag > lower_threshold)
375 {
376 bins[y][x] = WEAK_EDGE; // weak edge
377 }
378 /*
379 Since we've already rejected weak edges, we shouldn't get here.
380 */
381 else
382 {
383 bins[y][x] = REJECT_EDGE; // reject edge
384 }
385 }
386 }
387 //cout << "total time hysteris: " << timer.get_time() << endl;
388
389 /* While there are still unvisited accepted (strong) edge locations... */
390 while(edge_locations.size() > 0)
391 {
392 /* Pop the next strong edge. */
393 ImageRef loc(edge_locations.front());
394 edge_locations.pop();
395
396 /* Record its magnitude. */
397 out[loc] = mags[loc];
398
399 /* Mark any weak edges connected to it as strong and push them onto the stack.
400 These edges will never be pushed twice since only weak edges can be pushed
401 onto the stack at this point.*/
402 if(bins[loc.y][loc.x] == STRONG_EDGE)
403 {
404 if(bins[loc.y - 1][loc.x - 1] == WEAK_EDGE)
405 {
406 bins[loc.y - 1][loc.x - 1] = STRONG_EDGE;
407 edge_locations.push(ImageRef(loc.x - 1, loc.y - 1));
408 }
409 if(bins[loc.y - 1][loc.x] == WEAK_EDGE)
410 {
411 bins[loc.y - 1][loc.x] = STRONG_EDGE;
412 edge_locations.push(ImageRef(loc.x, loc.y - 1));
413 }
414 if(bins[loc.y - 1][loc.x + 1] == WEAK_EDGE)
415 {
416 bins[loc.y - 1][loc.x + 1] = STRONG_EDGE;
417 edge_locations.push(ImageRef(loc.x + 1, loc.y - 1));
418 }
419 if(bins[loc.y][loc.x - 1] == WEAK_EDGE)
420 {
421 bins[loc.y][loc.x - 1] = STRONG_EDGE;
422 edge_locations.push(ImageRef(loc.x - 1, loc.y));
423 }
424 if(bins[loc.y][loc.x] == WEAK_EDGE)
425 {
426 bins[loc.y][loc.x] = STRONG_EDGE;
427 edge_locations.push(ImageRef(loc.x, loc.y));
428 }
429 if(bins[loc.y][loc.x + 1] == WEAK_EDGE)
430 {
431 bins[loc.y][loc.x + 1] = STRONG_EDGE;
432 edge_locations.push(ImageRef(loc.x + 1, loc.y));
433 }
434 if(bins[loc.y + 1][loc.x - 1] == WEAK_EDGE)
435 {
436 bins[loc.y + 1][loc.x - 1] = STRONG_EDGE;
437 edge_locations.push(ImageRef(loc.x - 1, loc.y + 1));
438 }
439 if(bins[loc.y + 1][loc.x] == WEAK_EDGE)
440 {
441 bins[loc.y + 1][loc.x] = STRONG_EDGE;
442 edge_locations.push(ImageRef(loc.x, loc.y + 1));
443 }
444 if(bins[loc.y + 1][loc.x + 1] == WEAK_EDGE)
445 {
446 bins[loc.y + 1][loc.x + 1] = STRONG_EDGE;
447 edge_locations.push(ImageRef(loc.x + 1, loc.y + 1));
448 }
449 }
450 }
451 //cout << "total time tracing: " << timer.get_time() << endl;
452 }
453
454 /*
455 Takes in an arbitray edge map (in) and a mask. For each pixel,
456 if the mask is above a threshold (threshold), it computes the
457 gradient direction for that pixel, storing the result in grad_dir_map.
458 */
459 template <class T>
460 void compute_gradient_direction_at(const BasicImage<T>& in, const BasicImage<T>& mask, const double threshold,
461 BasicImage<T>& grad_dir_map)
462 {
463 zero_border(grad_dir_map);
464 for(int y = 1; y < in.size().y - 1; y++)
465 {
466 for(int x = 1; x < in.size().x - 1; x++)
467 {
468 if(mask[y][x] > threshold)
469 {
470 const double ygrad = ((in[y - 1][x - 1] + 2 * in[y - 1][x] + in[y - 1][x + 1]) - (in[y + 1][x - 1] + 2 * in[y + 1][x] + in[y + 1][x + 1]));
471 const double xgrad = ((in[y - 1][x - 1] + 2 * in[y][x - 1] + in[y + 1][x - 1]) - (in[y - 1][x + 1] + 2 * in[y][x + 1] + in[y + 1][x + 1]));
472 double dir = std::atan2(xgrad, ygrad);
473 grad_dir_map[y][x] = dir;
474 }
475 else
476 {
477 grad_dir_map[y][x] = 0.0;
478 }
479 }
480 }
481 }
482 }
483 #endif