-
Notifications
You must be signed in to change notification settings - Fork 6
/
canny.c
454 lines (397 loc) · 13.5 KB
/
canny.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <stdlib.h>
#include <string.h>
#include <math.h>
/**@file
The code detects edges in greyscale images. The method was developed
by John F Canny and is generally considered to be the best edge
detection method currently available.
C version. Based on Java code by
* Tom Gibara
*/
#define ffabs(x) ( (x) >= 0 ? (x) : -(x) )
#define GAUSSIAN_CUT_OFF 0.005f
#define MAGNITUDE_SCALE 100.0f
#define MAGNITUDE_LIMIT 1000.0f
#define MAGNITUDE_MAX ((int) (MAGNITUDE_SCALE * MAGNITUDE_LIMIT))
typedef struct
{
unsigned char *data; /**< Input image .*/
int width; /**< Image width. */
int height; /**< Image height. */
int *idata; /**< Output for edges. */
int *magnitude; /**< Edge magnitude as detected by Gaussians. */
float *xConv; /**< Temporary for convolution in x direction. */
float *yConv; /**< Temporary for convolution in y direction. */
float *xGradient; /**< Gradients in x direction, as detected by Gaussians. */
float *yGradient; /**< Gradients in x direction, as detected by Gaussians. */
} CANNY;
unsigned char *canny(unsigned char *grey, int width, int height);
unsigned char *cannyparam(unsigned char *grey, int width, int height,
float lowThreshold, float highthreshold,
float gaussiankernelradius, int gaussiankernelwidth,
int contrastnormalised);
static CANNY *allocatebuffers(unsigned char *grey, int width, int height);
static void killbuffers(CANNY *can);
static int computeGradients(CANNY *can, float kernelRadius, int kernelWidth);
static void performHysteresis(CANNY *can, int low, int high);
static void follow(CANNY *can, int x1, int y1, int i1, int threshold);
static void normalizeContrast(unsigned char *data, int width, int height);
static float hypotenuse(float x, float y);
static float gaussian(float x, float sigma);
/**
Canny edge detection with default parameters
@param grey - the greyscale image
@param width - image width
@param height - image height
@returns Binary image with edges as set pixels.
@image html maggie.jpg Margaret Thatcher (1925-2013) greyscale photograph
@image html maggiecanny.gif Mrs Thatcher edge detected
*/
unsigned char *canny(unsigned char *grey, int width, int height)
{
return cannyparam(grey, width, height, 2.5f, 7.5f, 2.0f, 16, 0);
}
/**
Canny edge detection with parameters passed in by user.
@param grey - the greyscale image
@param width - image width
@param height - image height
@param lowthreshold - default 2.5
@param highthreshold - default 7.5
@param gaussiankernelradius - radius of edge detection Gaussian, in standard deviations
(default 2.0)
@param gaussiankernelwidth - width of Gaussian kernel, in pixels (default 16)
@param contrastnormalised - flag to normalise image before edge detection (defualt 0)
@returns: Binary image with set pixels as edges.
*/
unsigned char *cannyparam(unsigned char *grey, int width, int height,
float lowthreshold, float highthreshold,
float gaussiankernelradius, int gaussiankernelwidth,
int contrastnormalised)
{
CANNY *can = 0;
unsigned char *answer = 0;
int low, high;
int err;
int i;
answer = malloc(width * height);
if(!answer)
goto error_exit;
can = allocatebuffers(grey, width, height);
if(!can)
goto error_exit;
if (contrastnormalised)
normalizeContrast(can->data, width, height);
err = computeGradients(can, gaussiankernelradius, gaussiankernelwidth);
if(err < 0)
goto error_exit;
low = (int) (lowthreshold * MAGNITUDE_SCALE + 0.5f);
high = (int) ( highthreshold * MAGNITUDE_SCALE + 0.5f);
performHysteresis(can, low, high);
for(i=0;i<width*height;i++)
answer[i] = can->idata[i] > 0 ? 1 : 0;
killbuffers(can);
return answer;
error_exit:
free(answer);
killbuffers(can);
return 0;
}
/*
buffer allocation
*/
static CANNY *allocatebuffers(unsigned char *grey, int width, int height)
{
CANNY *answer;
answer = malloc(sizeof(CANNY));
if(!answer)
goto error_exit;
answer->data = malloc(width * height);
answer->idata = malloc(width * height * sizeof(int));
answer->magnitude = malloc(width * height * sizeof(int));
answer->xConv = malloc(width * height * sizeof(float));
answer->yConv = malloc(width * height * sizeof(float));
answer->xGradient = malloc(width * height * sizeof(float));
answer->yGradient = malloc(width * height * sizeof(float));
if(!answer->data || !answer->idata || !answer->magnitude ||
!answer->xConv || !answer->yConv ||
!answer->xGradient || !answer->yGradient)
goto error_exit;
memcpy(answer->data, grey, width * height);
answer->width = width;
answer->height = height;
return answer;
error_exit:
killbuffers(answer);
return 0;
}
/*
buffers destructor
*/
static void killbuffers(CANNY *can)
{
if(can)
{
free(can->data);
free(can->idata);
free(can->magnitude);
free(can->xConv);
free(can->yConv);
free(can->xGradient);
free(can->yGradient);
}
}
/* NOTE: The elements of the method below (specifically the technique for
non-maximal suppression and the technique for gradient computation)
are derived from an implementation posted in the following forum (with the
clear intent of others using the code):
http://forum.java.sun.com/thread.jspa?threadID=546211&start=45&tstart=0
My code effectively mimics the algorithm exhibited above.
Since I don't know the providence of the code that was posted it is a
possibility (though I think a very remote one) that this code violates
someone's intellectual property rights. If this concerns you feel free to
contact me for an alternative, though less efficient, implementation.
*/
static int computeGradients(CANNY *can, float kernelRadius, int kernelWidth)
{
float *kernel;
float *diffKernel;
int kwidth;
int width, height;
int initX;
int maxX;
int initY;
int maxY;
int x, y;
int i;
int flag;
width = can->width;
height = can->height;
kernel = malloc(kernelWidth * sizeof(float));
diffKernel = malloc(kernelWidth * sizeof(float));
if(!kernel || !diffKernel)
goto error_exit;
/* initialise the Gaussian kernel */
for (kwidth = 0; kwidth < kernelWidth; kwidth++)
{
float g1, g2, g3;
g1 = gaussian((float) kwidth, kernelRadius);
if (g1 <= GAUSSIAN_CUT_OFF && kwidth >= 2)
break;
g2 = gaussian(kwidth - 0.5f, kernelRadius);
g3 = gaussian(kwidth + 0.5f, kernelRadius);
kernel[kwidth] = (g1 + g2 + g3) / 3.0f / (2.0f * (float) 3.14 * kernelRadius * kernelRadius);
diffKernel[kwidth] = g3 - g2;
}
initX = kwidth - 1;
maxX = width - (kwidth - 1);
initY = width * (kwidth - 1);
maxY = width * (height - (kwidth - 1));
/* perform convolution in x and y directions */
for(x = initX; x < maxX; x++)
{
for(y = initY; y < maxY; y += width)
{
int index = x + y;
float sumX = can->data[index] * kernel[0];
float sumY = sumX;
int xOffset = 1;
int yOffset = width;
while(xOffset < kwidth)
{
sumY += kernel[xOffset] * (can->data[index - yOffset] + can->data[index + yOffset]);
sumX += kernel[xOffset] * (can->data[index - xOffset] + can->data[index + xOffset]);
yOffset += width;
xOffset++;
}
can->yConv[index] = sumY;
can->xConv[index] = sumX;
}
}
for (x = initX; x < maxX; x++)
{
for (y = initY; y < maxY; y += width)
{
float sum = 0.0f;
int index = x + y;
for (i = 1; i < kwidth; i++)
sum += diffKernel[i] * (can->yConv[index - i] - can->yConv[index + i]);
can->xGradient[index] = sum;
}
}
for(x = kwidth; x < width - kwidth; x++)
{
for (y = initY; y < maxY; y += width)
{
float sum = 0.0f;
int index = x + y;
int yOffset = width;
for (i = 1; i < kwidth; i++)
{
sum += diffKernel[i] * (can->xConv[index - yOffset] - can->xConv[index + yOffset]);
yOffset += width;
}
can->yGradient[index] = sum;
}
}
initX = kwidth;
maxX = width - kwidth;
initY = width * kwidth;
maxY = width * (height - kwidth);
for(x = initX; x < maxX; x++)
{
for(y = initY; y < maxY; y += width)
{
int index = x + y;
int indexN = index - width;
int indexS = index + width;
int indexW = index - 1;
int indexE = index + 1;
int indexNW = indexN - 1;
int indexNE = indexN + 1;
int indexSW = indexS - 1;
int indexSE = indexS + 1;
float xGrad = can->xGradient[index];
float yGrad = can->yGradient[index];
float gradMag = hypotenuse(xGrad, yGrad);
/* perform non-maximal supression */
float nMag = hypotenuse(can->xGradient[indexN], can->yGradient[indexN]);
float sMag = hypotenuse(can->xGradient[indexS], can->yGradient[indexS]);
float wMag = hypotenuse(can->xGradient[indexW], can->yGradient[indexW]);
float eMag = hypotenuse(can->xGradient[indexE], can->yGradient[indexE]);
float neMag = hypotenuse(can->xGradient[indexNE], can->yGradient[indexNE]);
float seMag = hypotenuse(can->xGradient[indexSE], can->yGradient[indexSE]);
float swMag = hypotenuse(can->xGradient[indexSW], can->yGradient[indexSW]);
float nwMag = hypotenuse(can->xGradient[indexNW], can->yGradient[indexNW]);
float tmp;
/*
* An explanation of what's happening here, for those who want
* to understand the source: This performs the "non-maximal
* supression" phase of the Canny edge detection in which we
* need to compare the gradient magnitude to that in the
* direction of the gradient; only if the value is a local
* maximum do we consider the point as an edge candidate.
*
* We need to break the comparison into a number of different
* cases depending on the gradient direction so that the
* appropriate values can be used. To avoid computing the
* gradient direction, we use two simple comparisons: first we
* check that the partial derivatives have the same sign (1)
* and then we check which is larger (2). As a consequence, we
* have reduced the problem to one of four identical cases that
* each test the central gradient magnitude against the values at
* two points with 'identical support'; what this means is that
* the geometry required to accurately interpolate the magnitude
* of gradient function at those points has an identical
* geometry (upto right-angled-rotation/reflection).
*
* When comparing the central gradient to the two interpolated
* values, we avoid performing any divisions by multiplying both
* sides of each inequality by the greater of the two partial
* derivatives. The common comparand is stored in a temporary
* variable (3) and reused in the mirror case (4).
*
*/
flag = ( (xGrad * yGrad <= 0.0f) /*(1)*/
? ffabs(xGrad) >= ffabs(yGrad) /*(2)*/
? (tmp = ffabs(xGrad * gradMag)) >= ffabs(yGrad * neMag - (xGrad + yGrad) * eMag) /*(3)*/
&& tmp > fabs(yGrad * swMag - (xGrad + yGrad) * wMag) /*(4)*/
: (tmp = ffabs(yGrad * gradMag)) >= ffabs(xGrad * neMag - (yGrad + xGrad) * nMag) /*(3)*/
&& tmp > ffabs(xGrad * swMag - (yGrad + xGrad) * sMag) /*(4)*/
: ffabs(xGrad) >= ffabs(yGrad) /*(2)*/
? (tmp = ffabs(xGrad * gradMag)) >= ffabs(yGrad * seMag + (xGrad - yGrad) * eMag) /*(3)*/
&& tmp > ffabs(yGrad * nwMag + (xGrad - yGrad) * wMag) /*(4)*/
: (tmp = ffabs(yGrad * gradMag)) >= ffabs(xGrad * seMag + (yGrad - xGrad) * sMag) /*(3)*/
&& tmp > ffabs(xGrad * nwMag + (yGrad - xGrad) * nMag) /*(4)*/
);
if(flag)
{
can->magnitude[index] = (gradMag >= MAGNITUDE_LIMIT) ? MAGNITUDE_MAX : (int) (MAGNITUDE_SCALE * gradMag);
/*NOTE: The orientation of the edge is not employed by this
implementation. It is a simple matter to compute it at
this point as: Math.atan2(yGrad, xGrad); */
}
else
{
can->magnitude[index] = 0;
}
}
}
free(kernel);
free(diffKernel);
return 0;
error_exit:
free(kernel);
free(diffKernel);
return -1;
}
/*
we follow edges. high gives the parameter for starting an edge,
how the parameter for continuing it.
*/
static void performHysteresis(CANNY *can, int low, int high)
{
int offset = 0;
int x, y;
memset(can->idata, 0, can->width * can->height * sizeof(int));
for(y = 0; y < can->height; y++)
{
for(x = 0; x < can->width; x++)
{
if(can->idata[offset] == 0 && can->magnitude[offset] >= high)
follow(can, x, y, offset, low);
offset++;
}
}
}
/*
recursive portion of edge follower
*/
static void follow(CANNY *can, int x1, int y1, int i1, int threshold)
{
int x, y;
int x0 = x1 == 0 ? x1 : x1 - 1;
int x2 = x1 == can->width - 1 ? x1 : x1 + 1;
int y0 = y1 == 0 ? y1 : y1 - 1;
int y2 = y1 == can->height -1 ? y1 : y1 + 1;
can->idata[i1] = can->magnitude[i1];
for (x = x0; x <= x2; x++)
{
for (y = y0; y <= y2; y++)
{
int i2 = x + y * can->width;
if ((y != y1 || x != x1) && can->idata[i2] == 0 && can->magnitude[i2] >= threshold)
follow(can, x, y, i2, threshold);
}
}
}
static void normalizeContrast(unsigned char *data, int width, int height)
{
int histogram[256] = {0};
int remap[256];
int sum = 0;
int j = 0;
int k;
int target;
int i;
for (i = 0; i < width * height; i++)
histogram[data[i]]++;
for (i = 0; i < 256; i++)
{
sum += histogram[i];
target = (sum*255)/(width * height);
for (k = j+1; k <= target; k++)
remap[k] = i;
j = target;
}
for (i = 0; i < width * height; i++)
data[i] = remap[data[i]];
}
static float hypotenuse(float x, float y)
{
return (float) sqrt(x*x +y*y);
}
static float gaussian(float x, float sigma)
{
return (float) exp(-(x * x) / (2.0f * sigma * sigma));
}