/src/libvips/libvips/convolution/canny.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Canny edge detector |
2 | | */ |
3 | | |
4 | | /* |
5 | | |
6 | | This file is part of VIPS. |
7 | | |
8 | | VIPS is free software; you can redistribute it and/or modify |
9 | | it under the terms of the GNU Lesser General Public License as published by |
10 | | the Free Software Foundation; either version 2 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU Lesser General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU Lesser General Public License |
19 | | along with this program; if not, write to the Free Software |
20 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | | 02110-1301 USA |
22 | | |
23 | | */ |
24 | | |
25 | | /* |
26 | | |
27 | | These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
28 | | |
29 | | */ |
30 | | |
31 | | /* |
32 | | #define DEBUG |
33 | | */ |
34 | | |
35 | | #ifdef HAVE_CONFIG_H |
36 | | #include <config.h> |
37 | | #endif /*HAVE_CONFIG_H*/ |
38 | | #include <glib/gi18n-lib.h> |
39 | | |
40 | | #include <stdio.h> |
41 | | #include <stdlib.h> |
42 | | #include <math.h> |
43 | | |
44 | | #include <vips/vips.h> |
45 | | |
46 | | typedef struct _VipsCanny { |
47 | | VipsOperation parent_instance; |
48 | | |
49 | | VipsImage *in; |
50 | | VipsImage *out; |
51 | | |
52 | | double sigma; |
53 | | VipsPrecision precision; |
54 | | |
55 | | /* Need an image vector for start_many. |
56 | | */ |
57 | | VipsImage *args[3]; |
58 | | } VipsCanny; |
59 | | |
60 | | typedef VipsOperationClass VipsCannyClass; |
61 | | |
62 | | G_DEFINE_TYPE(VipsCanny, vips_canny, VIPS_TYPE_OPERATION); |
63 | | |
64 | | /* Simple 2x2 -1/+1 difference. For uchar, we try to hit the vector path and |
65 | | * use an offset rather than -ves. Otherwise it's float or double output. |
66 | | */ |
67 | | static int |
68 | | vips_canny_gradient(VipsImage *in, VipsImage **Gx, VipsImage **Gy) |
69 | 0 | { |
70 | 0 | VipsImage *scope; |
71 | 0 | VipsImage **t; |
72 | 0 | VipsPrecision precision; |
73 | |
|
74 | 0 | scope = vips_image_new(); |
75 | 0 | t = (VipsImage **) vips_object_local_array((VipsObject *) scope, 2); |
76 | |
|
77 | 0 | t[0] = vips_image_new_matrixv(2, 2, |
78 | 0 | -1.0, 1.0, |
79 | 0 | -1.0, 1.0); |
80 | |
|
81 | 0 | if (in->BandFmt == VIPS_FORMAT_UCHAR) { |
82 | 0 | precision = VIPS_PRECISION_INTEGER; |
83 | 0 | vips_image_set_double(t[0], "offset", 128.0); |
84 | 0 | } |
85 | 0 | else |
86 | 0 | precision = VIPS_PRECISION_FLOAT; |
87 | |
|
88 | 0 | if (vips_conv(in, Gx, t[0], "precision", precision, NULL) || |
89 | 0 | vips_rot90(t[0], &t[1], NULL) || |
90 | 0 | vips_conv(in, Gy, t[1], "precision", precision, NULL)) { |
91 | 0 | g_object_unref(scope); |
92 | 0 | return -1; |
93 | 0 | } |
94 | | |
95 | 0 | g_object_unref(scope); |
96 | |
|
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | | /* LUT for calculating atan2() with +/- 4 bits of precision in each axis. |
101 | | */ |
102 | | static VipsPel vips_canny_polar_atan2[256]; |
103 | | |
104 | | /* For the uchar path, gx/gy are -128 to +127, and we need -8 to +7 for the |
105 | | * atan2 LUT. |
106 | | * |
107 | | * For G, we should calculate sqrt(gx * gx + gy * gy), however we are only |
108 | | * interested in relative magnitude (max of sqrt), so we can skip the sqrt |
109 | | * itself. We need a result that will fit in 0 - 255, so shift down. |
110 | | */ |
111 | | #define POLAR_UCHAR \ |
112 | 0 | { \ |
113 | 0 | for (x = 0; x < r->width; x++) { \ |
114 | 0 | for (band = 0; band < Gx->Bands; band++) { \ |
115 | 0 | int gx = p1[band] - 128; \ |
116 | 0 | int gy = p2[band] - 128; \ |
117 | 0 | \ |
118 | 0 | int i = ((gx >> 4) & 0xf) | (gy & 0xf0); \ |
119 | 0 | \ |
120 | 0 | q[0] = (gx * gx + gy * gy + 256) >> 9; \ |
121 | 0 | q[1] = vips_canny_polar_atan2[i]; \ |
122 | 0 | \ |
123 | 0 | q += 2; \ |
124 | 0 | } \ |
125 | 0 | \ |
126 | 0 | p1 += Gx->Bands; \ |
127 | 0 | p2 += Gx->Bands; \ |
128 | 0 | } \ |
129 | 0 | } |
130 | | |
131 | | /* Float/double path. We keep the same ranges as the uchar path to reduce |
132 | | * confusion. |
133 | | */ |
134 | | #define POLAR(TYPE) \ |
135 | 0 | { \ |
136 | 0 | TYPE *tp1 = (TYPE *) p1; \ |
137 | 0 | TYPE *tp2 = (TYPE *) p2; \ |
138 | 0 | TYPE *tq = (TYPE *) q; \ |
139 | 0 | \ |
140 | 0 | for (x = 0; x < r->width; x++) { \ |
141 | 0 | for (band = 0; band < Gx->Bands; band++) { \ |
142 | 0 | double gx = tp1[band]; \ |
143 | 0 | double gy = tp2[band]; \ |
144 | 0 | double theta = VIPS_DEG(atan2(gx, gy)); \ |
145 | 0 | \ |
146 | 0 | tq[0] = (gx * gx + gy * gy + 256.0) / 512.0; \ |
147 | 0 | tq[1] = 256.0 * fmod(theta + 360.0, 360.0) / 360.0; \ |
148 | 0 | \ |
149 | 0 | tq += 2; \ |
150 | 0 | } \ |
151 | 0 | \ |
152 | 0 | tp1 += Gx->Bands; \ |
153 | 0 | tp2 += Gx->Bands; \ |
154 | 0 | } \ |
155 | 0 | } |
156 | | |
157 | | static int |
158 | | vips_canny_polar_generate(VipsRegion *out_region, |
159 | | void *vseq, void *a, void *b, gboolean *stop) |
160 | 0 | { |
161 | 0 | VipsRegion **in = (VipsRegion **) vseq; |
162 | 0 | VipsRect *r = &out_region->valid; |
163 | 0 | VipsImage *Gx = in[0]->im; |
164 | |
|
165 | 0 | int x, y, band; |
166 | |
|
167 | 0 | if (vips_reorder_prepare_many(out_region->im, in, r)) |
168 | 0 | return -1; |
169 | | |
170 | 0 | for (y = 0; y < r->height; y++) { |
171 | 0 | VipsPel *p1 = (VipsPel *restrict) |
172 | 0 | VIPS_REGION_ADDR(in[0], r->left, r->top + y); |
173 | 0 | VipsPel *p2 = (VipsPel *restrict) |
174 | 0 | VIPS_REGION_ADDR(in[1], r->left, r->top + y); |
175 | 0 | VipsPel *q = (VipsPel *restrict) |
176 | 0 | VIPS_REGION_ADDR(out_region, r->left, r->top + y); |
177 | |
|
178 | 0 | switch (Gx->BandFmt) { |
179 | 0 | case VIPS_FORMAT_UCHAR: |
180 | 0 | POLAR_UCHAR; |
181 | 0 | break; |
182 | | |
183 | 0 | case VIPS_FORMAT_FLOAT: |
184 | 0 | POLAR(float); |
185 | 0 | break; |
186 | | |
187 | 0 | case VIPS_FORMAT_DOUBLE: |
188 | 0 | POLAR(double); |
189 | 0 | break; |
190 | | |
191 | 0 | default: |
192 | 0 | g_assert(FALSE); |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | return 0; |
197 | 0 | } |
198 | | |
199 | | static void * |
200 | | vips_atan2_init(void *null) |
201 | 0 | { |
202 | 0 | int i; |
203 | |
|
204 | 0 | for (i = 0; i < 256; i++) { |
205 | | /* Use the bottom 4 bits for x, the top 4 for y. The double |
206 | | * shift does sign extension, assuming 2s complement. |
207 | | */ |
208 | 0 | int bits = sizeof(int) * 8 - 4; |
209 | 0 | int x = ((i & 0xf) << bits) >> bits; |
210 | 0 | int y = ((i >> 4) & 0x0f) << bits >> bits; |
211 | 0 | double theta = VIPS_DEG(atan2(x, y)) + 360; |
212 | |
|
213 | 0 | vips_canny_polar_atan2[i] = 256 * theta / 360; |
214 | 0 | } |
215 | |
|
216 | 0 | return NULL; |
217 | 0 | } |
218 | | |
219 | | /* Calculate G/theta from Gx/Gy. We code theta as 0-256 for 0-360 |
220 | | * and skip the sqrt on G. |
221 | | * |
222 | | * For a white disc on a black background, theta is 0 at the top, 64 on the |
223 | | * left, 128 on the right and 192 on the right edge. |
224 | | */ |
225 | | static int |
226 | | vips_canny_polar(VipsImage **args, VipsImage **out) |
227 | 0 | { |
228 | 0 | static GOnce once = G_ONCE_INIT; |
229 | |
|
230 | 0 | g_once(&once, vips_atan2_init, NULL); |
231 | |
|
232 | 0 | *out = vips_image_new(); |
233 | 0 | if (vips_image_pipeline_array(*out, |
234 | 0 | VIPS_DEMAND_STYLE_THINSTRIP, args)) |
235 | 0 | return -1; |
236 | 0 | (*out)->Bands *= 2; |
237 | |
|
238 | 0 | if (vips_image_generate(*out, |
239 | 0 | vips_start_many, vips_canny_polar_generate, vips_stop_many, |
240 | 0 | args, NULL)) |
241 | 0 | return -1; |
242 | | |
243 | 0 | return 0; |
244 | 0 | } |
245 | | |
246 | | #define THIN(TYPE) \ |
247 | 0 | { \ |
248 | 0 | TYPE *tp = (TYPE *) p; \ |
249 | 0 | TYPE *tq = (TYPE *) q; \ |
250 | 0 | \ |
251 | 0 | for (x = 0; x < r->width; x++) { \ |
252 | 0 | for (band = 0; band < out_bands; band++) { \ |
253 | 0 | TYPE G = tp[lsk + psk]; \ |
254 | 0 | TYPE theta = tp[lsk + psk + 1]; \ |
255 | 0 | int low_theta = ((int) (theta / 32)) & 0x7; \ |
256 | 0 | int high_theta = (low_theta + 1) & 0x7; \ |
257 | 0 | TYPE residual = theta - low_theta * 32; \ |
258 | 0 | TYPE lowa = tp[offset[low_theta]]; \ |
259 | 0 | TYPE lowb = tp[offset[high_theta]]; \ |
260 | 0 | TYPE low = \ |
261 | 0 | (lowa * (32 - residual) + lowb * residual) / 32; \ |
262 | 0 | TYPE higha = tp[offset[(low_theta + 4) & 0x7]]; \ |
263 | 0 | TYPE highb = tp[offset[(high_theta + 4) & 0x7]]; \ |
264 | 0 | TYPE high = \ |
265 | 0 | (higha * (32 - residual) + highb * residual) / 32; \ |
266 | 0 | \ |
267 | 0 | if (G <= low || \ |
268 | 0 | G < high) \ |
269 | 0 | G = 0; \ |
270 | 0 | \ |
271 | 0 | tq[band] = G; \ |
272 | 0 | \ |
273 | 0 | tp += 2; \ |
274 | 0 | } \ |
275 | 0 | \ |
276 | 0 | tq += out_bands; \ |
277 | 0 | } \ |
278 | 0 | } |
279 | | |
280 | | static int |
281 | | vips_canny_thin_generate(VipsRegion *out_region, |
282 | | void *vseq, void *a, void *b, gboolean *stop) |
283 | 0 | { |
284 | 0 | VipsRegion *in = (VipsRegion *) vseq; |
285 | 0 | VipsRect *r = &out_region->valid; |
286 | 0 | VipsImage *im = in->im; |
287 | 0 | int out_bands = out_region->im->Bands; |
288 | |
|
289 | 0 | VipsRect rect; |
290 | 0 | int x, y, band; |
291 | 0 | int lsk; |
292 | 0 | int psk; |
293 | |
|
294 | 0 | int offset[8]; |
295 | |
|
296 | 0 | rect = *r; |
297 | 0 | rect.width += 2; |
298 | 0 | rect.height += 2; |
299 | 0 | if (vips_region_prepare(in, &rect)) |
300 | 0 | return -1; |
301 | | |
302 | | /* These are in typed units. |
303 | | */ |
304 | 0 | lsk = VIPS_REGION_LSKIP(in) / VIPS_IMAGE_SIZEOF_ELEMENT(im); |
305 | 0 | psk = VIPS_IMAGE_SIZEOF_PEL(im) / VIPS_IMAGE_SIZEOF_ELEMENT(im); |
306 | | |
307 | | /* For each of the 8 directions, the offset to get to that pixel from |
308 | | * the top-left of the 3x3. |
309 | | * |
310 | | * 1 | 0 | 7 |
311 | | * --+---+-- |
312 | | * 2 | X | 6 |
313 | | * --+---+-- |
314 | | * 3 | 4 | 5 |
315 | | */ |
316 | 0 | offset[0] = psk; |
317 | 0 | offset[1] = 0; |
318 | 0 | offset[2] = lsk; |
319 | 0 | offset[3] = 2 * lsk; |
320 | 0 | offset[4] = 2 * lsk + psk; |
321 | 0 | offset[5] = 2 * lsk + 2 * psk; |
322 | 0 | offset[6] = lsk + 2 * psk; |
323 | 0 | offset[7] = 2 * psk; |
324 | |
|
325 | 0 | for (y = 0; y < r->height; y++) { |
326 | 0 | VipsPel *p = (VipsPel *restrict) |
327 | 0 | VIPS_REGION_ADDR(in, r->left, r->top + y); |
328 | 0 | VipsPel *q = (VipsPel *restrict) |
329 | 0 | VIPS_REGION_ADDR(out_region, r->left, r->top + y); |
330 | |
|
331 | 0 | switch (im->BandFmt) { |
332 | 0 | case VIPS_FORMAT_UCHAR: |
333 | 0 | THIN(unsigned char); |
334 | 0 | break; |
335 | | |
336 | 0 | case VIPS_FORMAT_FLOAT: |
337 | 0 | THIN(float); |
338 | 0 | break; |
339 | | |
340 | 0 | case VIPS_FORMAT_DOUBLE: |
341 | 0 | THIN(double); |
342 | 0 | break; |
343 | | |
344 | 0 | default: |
345 | 0 | g_assert(FALSE); |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | 0 | return 0; |
350 | 0 | } |
351 | | |
352 | | /* Remove non-maximal edges. At each point, compare the G to the G in either |
353 | | * direction and 0 it if it's not the largest. |
354 | | */ |
355 | | static int |
356 | | vips_canny_thin(VipsImage *in, VipsImage **out) |
357 | 0 | { |
358 | 0 | *out = vips_image_new(); |
359 | 0 | if (vips_image_pipelinev(*out, |
360 | 0 | VIPS_DEMAND_STYLE_THINSTRIP, in, NULL)) |
361 | 0 | return -1; |
362 | 0 | (*out)->Bands /= 2; |
363 | 0 | (*out)->Xsize -= 2; |
364 | 0 | (*out)->Ysize -= 2; |
365 | |
|
366 | 0 | if (vips_image_generate(*out, |
367 | 0 | vips_start_one, vips_canny_thin_generate, vips_stop_one, |
368 | 0 | in, NULL)) |
369 | 0 | return -1; |
370 | | |
371 | 0 | return 0; |
372 | 0 | } |
373 | | |
374 | | static int |
375 | | vips_canny_build(VipsObject *object) |
376 | 0 | { |
377 | 0 | VipsCanny *canny = (VipsCanny *) object; |
378 | 0 | VipsImage **t = (VipsImage **) vips_object_local_array(object, 6); |
379 | |
|
380 | 0 | VipsImage *in; |
381 | |
|
382 | 0 | if (VIPS_OBJECT_CLASS(vips_canny_parent_class)->build(object)) |
383 | 0 | return -1; |
384 | | |
385 | 0 | in = canny->in; |
386 | |
|
387 | 0 | if (vips_gaussblur(in, &t[0], canny->sigma, |
388 | 0 | "precision", canny->precision, |
389 | 0 | NULL)) |
390 | 0 | return -1; |
391 | 0 | in = t[0]; |
392 | |
|
393 | 0 | if (vips_canny_gradient(in, &t[1], &t[2])) |
394 | 0 | return -1; |
395 | | |
396 | | /* Form (G, theta). |
397 | | */ |
398 | 0 | canny->args[0] = t[1]; |
399 | 0 | canny->args[1] = t[2]; |
400 | 0 | canny->args[2] = NULL; |
401 | 0 | if (vips_canny_polar(canny->args, &t[3])) |
402 | 0 | return -1; |
403 | 0 | in = t[3]; |
404 | | |
405 | | /* Expand by two pixels all around, then thin in the direction of the |
406 | | * gradient. |
407 | | */ |
408 | 0 | if (vips_embed(in, &t[4], 1, 1, in->Xsize + 2, in->Ysize + 2, |
409 | 0 | "extend", VIPS_EXTEND_COPY, |
410 | 0 | NULL)) |
411 | 0 | return -1; |
412 | | |
413 | 0 | if (vips_canny_thin(t[4], &t[5])) |
414 | 0 | return -1; |
415 | 0 | in = t[5]; |
416 | |
|
417 | 0 | g_object_set(object, "out", vips_image_new(), NULL); |
418 | |
|
419 | 0 | if (vips_image_write(in, canny->out)) |
420 | 0 | return -1; |
421 | | |
422 | 0 | return 0; |
423 | 0 | } |
424 | | |
425 | | static void |
426 | | vips_canny_class_init(VipsCannyClass *class) |
427 | 17 | { |
428 | 17 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
429 | 17 | VipsObjectClass *object_class = (VipsObjectClass *) class; |
430 | 17 | VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); |
431 | | |
432 | 17 | gobject_class->set_property = vips_object_set_property; |
433 | 17 | gobject_class->get_property = vips_object_get_property; |
434 | | |
435 | 17 | object_class->nickname = "canny"; |
436 | 17 | object_class->description = _("Canny edge detector"); |
437 | 17 | object_class->build = vips_canny_build; |
438 | | |
439 | 17 | operation_class->flags = VIPS_OPERATION_SEQUENTIAL; |
440 | | |
441 | 17 | VIPS_ARG_IMAGE(class, "in", 1, |
442 | 17 | _("Input"), |
443 | 17 | _("Input image"), |
444 | 17 | VIPS_ARGUMENT_REQUIRED_INPUT, |
445 | 17 | G_STRUCT_OFFSET(VipsCanny, in)); |
446 | | |
447 | 17 | VIPS_ARG_IMAGE(class, "out", 2, |
448 | 17 | _("Output"), |
449 | 17 | _("Output image"), |
450 | 17 | VIPS_ARGUMENT_REQUIRED_OUTPUT, |
451 | 17 | G_STRUCT_OFFSET(VipsCanny, out)); |
452 | | |
453 | 17 | VIPS_ARG_DOUBLE(class, "sigma", 10, |
454 | 17 | _("Sigma"), |
455 | 17 | _("Sigma of Gaussian"), |
456 | 17 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
457 | 17 | G_STRUCT_OFFSET(VipsCanny, sigma), |
458 | 17 | 0.01, 1000, 1.4); |
459 | | |
460 | 17 | VIPS_ARG_ENUM(class, "precision", 103, |
461 | 17 | _("Precision"), |
462 | 17 | _("Convolve with this precision"), |
463 | 17 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
464 | 17 | G_STRUCT_OFFSET(VipsCanny, precision), |
465 | 17 | VIPS_TYPE_PRECISION, VIPS_PRECISION_FLOAT); |
466 | 17 | } |
467 | | |
468 | | static void |
469 | | vips_canny_init(VipsCanny *canny) |
470 | 0 | { |
471 | 0 | canny->sigma = 1.4; |
472 | 0 | canny->precision = VIPS_PRECISION_FLOAT; |
473 | 0 | } |
474 | | |
475 | | /** |
476 | | * vips_canny: (method) |
477 | | * @in: input image |
478 | | * @out: (out): output image |
479 | | * @...: `NULL`-terminated list of optional named arguments |
480 | | * |
481 | | * Find edges by Canny's method: The maximum of the derivative of the gradient |
482 | | * in the direction of the gradient. Output is float, except for uchar input, |
483 | | * where output is uchar, and double input, where output is double. Non-complex |
484 | | * images only. |
485 | | * |
486 | | * Use @sigma to control the scale over which gradient is measured. 1.4 is |
487 | | * usually a good value. |
488 | | * |
489 | | * Use @precision to set the precision of edge detection. For uchar images, |
490 | | * setting this to [enum@Vips.Precision.INTEGER] will make edge detection much |
491 | | * faster, but sacrifice some sensitivity. |
492 | | * |
493 | | * You will probably need to process the output further to eliminate weak |
494 | | * edges. |
495 | | * |
496 | | * ::: tip "Optional arguments" |
497 | | * * @sigma: `gdouble`, sigma for gaussian blur |
498 | | * * @precision: [enum@Precision], calculation accuracy |
499 | | * |
500 | | * ::: seealso |
501 | | * [method@Image.sobel]. |
502 | | * |
503 | | * Returns: 0 on success, -1 on error. |
504 | | */ |
505 | | int |
506 | | vips_canny(VipsImage *in, VipsImage **out, ...) |
507 | 0 | { |
508 | 0 | va_list ap; |
509 | 0 | int result; |
510 | |
|
511 | 0 | va_start(ap, out); |
512 | 0 | result = vips_call_split("canny", ap, in, out); |
513 | 0 | va_end(ap); |
514 | |
|
515 | 0 | return result; |
516 | 0 | } |