| 6 |
| obtaining a copy of this software and associated documentation |
| 7 | files (the "Software"), to deal in the Software without |
| 8 | restriction, including without limitation the rights to use, |
| 9 | copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the |
| 11 | Software is furnished to do so, subject to the following |
| 12 | conditions: |
| 13 |
| 14 | The above copyright notice and this permission notice shall be |
| 15 | included in all copies or substantial portions of the Software. |
| 16 |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 | OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <math.h> |
| 31 | #include <time.h> |
| 32 | #include "fast-edge.h" |
| 33 |
| 34 | #define LOW_THRESHOLD_PERCENTAGE 0.8 // percentage of the high threshold value that the low threshold shall be set at |
| 35 | #define PI 3.14159265 |
| 36 | #define HIGH_THRESHOLD_PERCENTAGE 0.10 // percentage of pixels that meet the high threshold - for example 0.15 will ensure that at least 15% of edge pixels are considered to meet the high threshold |
| 37 |
| 38 | #define min(X,Y) ((X) < (Y) ? (X) : (Y)) |
| 39 | #define max(X,Y) ((X) < (Y) ? (Y) : (X)) |
| 40 |
| 41 | namespace ocr{ |
| 42 | /* |
| 43 | CANNY EDGE DETECT |
| 44 | DOES NOT PERFORM NOISE REDUCTION - PERFORM NOISE REDUCTION PRIOR TO USE |
| 45 | Noise reduction omitted, as some applications benefit from morphological operations such as opening or closing as opposed to Gaussian noise reduction |
| 46 | If your application always takes the same size input image, uncomment the definitions of WIDTH and HEIGHT in the header file and define them to the size of your input image, |
| 47 | otherwise the required intermediate arrays will be dynamically allocated. |
| 48 | If WIDTH and HEIGHT are defined, the arrays will be allocated in the compiler directive that follows: |
| 49 | */ |
| 50 | #ifdef WIDTH |
| 51 | int g[WIDTH * HEIGHT], dir[WIDTH * HEIGHT] = {0}; |
| 52 | unsigned char img_scratch_data[WIDTH * HEIGHT] = {0}; |
| 53 | #endif |
| 54 | void canny_edge_detect(struct image * img_in, struct image * img_out) { |
| 55 | struct image img_scratch; |
| 56 | int high, low; |
| 57 | #ifndef WIDTH |
| 58 | int * g = (int*)calloc(static_cast<size_t>(img_in->width*img_in->height), sizeof(int)); |
| 59 | int * dir = (int*)calloc(static_cast<size_t>(img_in->width*img_in->height), sizeof(int)); |
| 60 | unsigned char * img_scratch_data = (unsigned char*)calloc(static_cast<size_t>(img_in->width*img_in->height), sizeof(char)); |
| 61 | #endif |
| 62 | img_scratch.width = img_in->width; |
| 63 | img_scratch.height = img_in->height; |
| 64 | img_scratch.pixel_data = img_scratch_data; |
| 65 | calc_gradient_sobel(img_in, g, dir); |
| 66 | //printf("*** performing non-maximum suppression ***\n"); |
| 67 | non_max_suppression(&img_scratch, g, dir); |
| 68 | estimate_threshold(&img_scratch, &high, &low); |
| 69 | hysteresis(high, low, &img_scratch, img_out); |
| 70 | #ifndef WIDTH |
| 71 | free(g); |
| 72 | free(dir); |
| 73 | free(img_scratch_data); |
| 74 | #endif |
| 75 | } |
| 76 |
| 77 | /* |
| 78 | GAUSSIAN_NOISE_ REDUCE |
| 79 | apply 5x5 Gaussian convolution filter, shrinks the image by 4 pixels in each direction, using Gaussian filter found here: |
| 80 | http://en.wikipedia.org/wiki/Canny_edge_detector |
| 81 | */ |
| 82 | void gaussian_noise_reduce(struct image * img_in, struct image * img_out) |
| 83 | { |
| 84 | #ifdef CLOCK |
| 85 | clock_t start = clock(); |
| 86 | #endif |
| 87 | int w, h, x, y, max_x, max_y; |
| 88 | w = img_in->width; |
| 89 | h = img_in->height; |
| 90 | img_out->width = w; |
| 91 | img_out->height = h; |
| 92 | max_x = w - 2; |
| 93 | max_y = w * (h - 2); |
| 94 | for (y = w * 2; y < max_y; y += w) { |
| 95 | for (x = 2; x < max_x; x++) { |
| 96 | img_out->pixel_data[x + y] = (2 * img_in->pixel_data[x + y - 2 - w - w] + |
| 97 | 4 * img_in->pixel_data[x + y - 1 - w - w] + |
| 98 | 5 * img_in->pixel_data[x + y - w - w] + |
| 99 | 4 * img_in->pixel_data[x + y + 1 - w - w] + |
| 100 | 2 * img_in->pixel_data[x + y + 2 - w - w] + |
| 101 | 4 * img_in->pixel_data[x + y - 2 - w] + |
| 102 | 9 * img_in->pixel_data[x + y - 1 - w] + |
| 103 | 12 * img_in->pixel_data[x + y - w] + |
| 104 | 9 * img_in->pixel_data[x + y + 1 - w] + |
| 105 | 4 * img_in->pixel_data[x + y + 2 - w] + |
| 106 | 5 * img_in->pixel_data[x + y - 2] + |
| 107 | 12 * img_in->pixel_data[x + y - 1] + |
| 108 | 15 * img_in->pixel_data[x + y] + |
| 109 | 12 * img_in->pixel_data[x + y + 1] + |
| 110 | 5 * img_in->pixel_data[x + y + 2] + |
| 111 | 4 * img_in->pixel_data[x + y - 2 + w] + |
| 112 | 9 * img_in->pixel_data[x + y - 1 + w] + |
| 113 | 12 * img_in->pixel_data[x + y + w] + |
| 114 | 9 * img_in->pixel_data[x + y + 1 + w] + |
| 115 | 4 * img_in->pixel_data[x + y + 2 + w] + |
| 116 | 2 * img_in->pixel_data[x + y - 2 + w + w] + |
| 117 | 4 * img_in->pixel_data[x + y - 1 + w + w] + |
| 118 | 5 * img_in->pixel_data[x + y + w + w] + |
| 119 | 4 * img_in->pixel_data[x + y + 1 + w + w] + |
| 120 | 2 * img_in->pixel_data[x + y + 2 + w + w]) / 159; |
| 121 | } |
| 122 | } |
| 123 | #ifdef CLOCK |
| 124 | printf("Gaussian noise reduction - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 125 | #endif |
| 126 | } |
| 127 |
| 128 | /* |
| 129 | CALC_GRADIENT_SOBEL |
| 130 | calculates the result of the Sobel operator - http://en.wikipedia.org/wiki/Sobel_operator - and estimates edge direction angle |
| 131 | */ |
| 132 | /*void calc_gradient_sobel(struct image * img_in, int g_x[], int g_y[], int g[], int dir[]) {//float theta[]) {*/ |
| 133 | void calc_gradient_sobel(struct image * img_in, int g[], int dir[]) { |
| 134 | #ifdef CLOCK |
| 135 | clock_t start = clock(); |
| 136 | #endif |
| 137 | int w, h, x, y, max_x, max_y, g_x, g_y; |
| 138 | float g_div; |
| 139 | w = img_in->width; |
| 140 | h = img_in->height; |
| 141 | max_x = w - 3; |
| 142 | max_y = w * (h - 3); |
| 143 | for (y = w * 3; y < max_y; y += w) { |
| 144 | for (x = 3; x < max_x; x++) { |
| 145 | g_x = (2 * img_in->pixel_data[x + y + 1] |
| 146 | + img_in->pixel_data[x + y - w + 1] |
| 147 | + img_in->pixel_data[x + y + w + 1] |
| 148 | - 2 * img_in->pixel_data[x + y - 1] |
| 149 | - img_in->pixel_data[x + y - w - 1] |
| 150 | - img_in->pixel_data[x + y + w - 1]); |
| 151 | g_y = 2 * img_in->pixel_data[x + y - w] |
| 152 | + img_in->pixel_data[x + y - w + 1] |
| 153 | + img_in->pixel_data[x + y - w - 1] |
| 154 | - 2 * img_in->pixel_data[x + y + w] |
| 155 | - img_in->pixel_data[x + y + w + 1] |
| 156 | - img_in->pixel_data[x + y + w - 1]; |
| 157 | #ifndef ABS_APPROX |
| 158 | g[x + y] = sqrt(g_x * g_x + g_y * g_y); |
| 159 | #endif |
| 160 | #ifdef ABS_APPROX |
| 161 | g[x + y] = abs(g_x[x + y]) + abs(g_y[x + y]); |
| 162 | #endif |
| 163 | if (g_x == 0) { |
| 164 | dir[x + y] = 2; |
| 165 | } else { |
| 166 | g_div = g_y / (float) g_x; |
| 167 | /* the following commented-out code is slightly faster than the code that follows, but is a slightly worse approximation for determining the edge direction angle |
| 168 | if (g_div < 0) { |
| 169 | if (g_div < -1) { |
| 170 | dir[n] = 0; |
| 171 | } else { |
| 172 | dir[n] = 1; |
| 173 | } |
| 174 | } else { |
| 175 | if (g_div > 1) { |
| 176 | dir[n] = 0; |
| 177 | } else { |
| 178 | dir[n] = 3; |
| 179 | } |
| 180 | } |
| 181 | */ |
| 182 | if (g_div < 0) { |
| 183 | if (g_div < -2.41421356237) { |
| 184 | dir[x + y] = 0; |
| 185 | } else { |
| 186 | if (g_div < -0.414213562373) { |
| 187 | dir[x + y] = 1; |
| 188 | } else { |
| 189 | dir[x + y] = 2; |
| 190 | } |
| 191 | } |
| 192 | } else { |
| 193 | if (g_div > 2.41421356237) { |
| 194 | dir[x + y] = 0; |
| 195 | } else { |
| 196 | if (g_div > 0.414213562373) { |
| 197 | dir[x + y] = 3; |
| 198 | } else { |
| 199 | dir[x + y] = 2; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 |
| 206 | } |
| 207 | #ifdef CLOCK |
| 208 | printf("Calculate gradient Sobel - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 209 | #endif |
| 210 | } |
| 211 |
| 212 | /* |
| 213 | CALC_GRADIENT_SCHARR |
| 214 | calculates the result of the Scharr version of the Sobel operator - http://en.wikipedia.org/wiki/Sobel_operator - and estimates edge direction angle |
| 215 | may have better rotational symmetry |
| 216 | */ |
| 217 | void calc_gradient_scharr(struct image * img_in, int g_x[], int g_y[], int g[], int dir[]) {//float theta[]) { |
| 218 | #ifdef CLOCK |
| 219 | clock_t start = clock(); |
| 220 | #endif |
| 221 | int w, h, x, y, max_x, max_y, n; |
| 222 | float g_div; |
| 223 | w = img_in->width; |
| 224 | h = img_in->height; |
| 225 | max_x = w - 1; |
| 226 | max_y = w * (h - 1); |
| 227 | n = 0; |
| 228 | for (y = w; y < max_y; y += w) { |
| 229 | for (x = 1; x < max_x; x++) { |
| 230 | g_x[n] = (10 * img_in->pixel_data[x + y + 1] |
| 231 | + 3 * img_in->pixel_data[x + y - w + 1] |
| 232 | + 3 * img_in->pixel_data[x + y + w + 1] |
| 233 | - 10 * img_in->pixel_data[x + y - 1] |
| 234 | - 3 * img_in->pixel_data[x + y - w - 1] |
| 235 | - 3 * img_in->pixel_data[x + y + w - 1]); |
| 236 | g_y[n] = 10 * img_in->pixel_data[x + y - w] |
| 237 | + 3 * img_in->pixel_data[x + y - w + 1] |
| 238 | + 3 * img_in->pixel_data[x + y - w - 1] |
| 239 | - 10 * img_in->pixel_data[x + y + w] |
| 240 | - 3 * img_in->pixel_data[x + y + w + 1] |
| 241 | - 3 * img_in->pixel_data[x + y + w - 1]; |
| 242 | #ifndef ABS_APPROX |
| 243 | g[n] = sqrt(g_x[n] * g_x[n] + g_y[n] * g_y[n]); |
| 244 | #endif |
| 245 | #ifdef ABS_APPROX |
| 246 | g[n] = abs(g_x[n]) + abs(g_y[n]); |
| 247 | #endif |
| 248 | if (g_x[n] == 0) { |
| 249 | dir[n] = 2; |
| 250 | } else { |
| 251 | g_div = g_y[n] / (float) g_x[n]; |
| 252 | if (g_div < 0) { |
| 253 | if (g_div < -2.41421356237) { |
| 254 | dir[n] = 0; |
| 255 | } else { |
| 256 | if (g_div < -0.414213562373) { |
| 257 | dir[n] = 1; |
| 258 | } else { |
| 259 | dir[n] = 2; |
| 260 | } |
| 261 | } |
| 262 | } else { |
| 263 | if (g_div > 2.41421356237) { |
| 264 | dir[n] = 0; |
| 265 | } else { |
| 266 | if (g_div > 0.414213562373) { |
| 267 | dir[n] = 3; |
| 268 | } else { |
| 269 | dir[n] = 2; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | n++; |
| 275 | } |
| 276 | } |
| 277 | #ifdef CLOCK |
| 278 | printf("Calculate gradient Scharr - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 279 | #endif |
| 280 | } |
| 281 | /* |
| 282 | NON_MAX_SUPPRESSION |
| 283 | using the estimates of the Gx and Gy image gradients and the edge direction angle determines whether the magnitude of the gradient assumes a local maximum in the gradient direction |
| 284 | if the rounded edge direction angle is 0 degrees, checks the north and south directions |
| 285 | if the rounded edge direction angle is 45 degrees, checks the northwest and southeast directions |
| 286 | if the rounded edge direction angle is 90 degrees, checks the east and west directions |
| 287 | if the rounded edge direction angle is 135 degrees, checks the northeast and southwest directions |
| 288 | */ |
| 289 | void non_max_suppression(struct image * img, int g[], int dir[]) {//float theta[]) { |
| 290 | #ifdef CLOCK |
| 291 | clock_t start = clock(); |
| 292 | #endif |
| 293 | int w, h, x, y, max_x, max_y; |
| 294 | w = img->width; |
| 295 | h = img->height; |
| 296 | max_x = w; |
| 297 | max_y = w * h; |
| 298 | for (y = 0; y < max_y; y += w) { |
| 299 | for (x = 0; x < max_x; x++) { |
| 300 | switch (dir[x + y]) { |
| 301 | case 0: |
| 302 | if(x+y-w-1<0){ |
| 303 | continue; |
| 304 | } |
| 305 | if (g[x + y] > g[x + y - w] && g[x + y] > g[x + y + w]) { |
| 306 | if (g[x + y] > 255) { |
| 307 | img->pixel_data[x + y] = 0xFF; |
| 308 | } else { |
| 309 | img->pixel_data[x + y] = g[x + y]; |
| 310 | } |
| 311 | } else { |
| 312 | img->pixel_data[x + y] = 0x00; |
| 313 | } |
| 314 | break; |
| 315 | case 1: |
| 316 | if(x+y-w-1<0){ |
| 317 | continue; |
| 318 | } |
| 319 | if (g[x + y] > g[x + y - w - 1] && g[x + y] > g[x + y + w + 1]) { |
| 320 | if (g[x + y] > 255) { |
| 321 | img->pixel_data[x + y] = 0xFF; |
| 322 | } else { |
| 323 | img->pixel_data[x + y] = g[x + y]; |
| 324 | } |
| 325 | } else { |
| 326 | img->pixel_data[x + y] = 0x00; |
| 327 | } |
| 328 | break; |
| 329 | case 2: |
| 330 | if (g[x + y] > g[x + y - 1] && g[x + y] > g[x + y + 1]) { |
| 331 | if (g[x + y] > 255) { |
| 332 | img->pixel_data[x + y] = 0xFF; |
| 333 | } else { |
| 334 | img->pixel_data[x + y] = g[x + y]; |
| 335 | } |
| 336 | } else { |
| 337 | img->pixel_data[x + y] = 0x00; |
| 338 | } |
| 339 | break; |
| 340 | case 3: |
| 341 | if(x+y-w-1<0){ |
| 342 | continue; |
| 343 | } |
| 344 | if (g[x + y] > g[x + y - w + 1] && g[x + y] > g[x + y + w - 1]) { |
| 345 | if (g[x + y] > 255) { |
| 346 | img->pixel_data[x + y] = 0xFF; |
| 347 | } else { |
| 348 | img->pixel_data[x + y] = g[x + y]; |
| 349 | } |
| 350 | } else { |
| 351 | img->pixel_data[x + y] = 0x00; |
| 352 | } |
| 353 | break; |
| 354 | default: |
| 355 | printf("ERROR - direction outside range 0 to 3"); |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | #ifdef CLOCK |
| 361 | printf("Non-maximum suppression - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 362 | #endif |
| 363 | } |
| 364 | /* |
| 365 | ESTIMATE_THRESHOLD |
| 366 | estimates hysteresis threshold, assuming that the top X% (as defined by the HIGH_THRESHOLD_PERCENTAGE) of edge pixels with the greatest intesity are true edges |
| 367 | and that the low threshold is equal to the quantity of the high threshold plus the total number of 0s at the low end of the histogram divided by 2 |
| 368 | */ |
| 369 | void estimate_threshold(struct image * img, int * high, int * low) { |
| 370 | #ifdef CLOCK |
| 371 | clock_t start = clock(); |
| 372 | #endif |
| 373 | int i, max, pixels, high_cutoff; |
| 374 | int histogram[256]; |
| 375 | max = img->width * img->height; |
| 376 | for (i = 0; i < 256; i++) { |
| 377 | histogram[i] = 0; |
| 378 | } |
| 379 | for (i = 0; i < max; i++) { |
| 380 | histogram[img->pixel_data[i]]++; |
| 381 | } |
| 382 | pixels = (max - histogram[0]) * HIGH_THRESHOLD_PERCENTAGE; |
| 383 | high_cutoff = 0; |
| 384 | i = 255; |
| 385 | while (high_cutoff < pixels) { |
| 386 | high_cutoff += histogram[i]; |
| 387 | i--; |
| 388 | } |
| 389 | *high = i; |
| 390 | i = 1; |
| 391 | while (histogram[i] == 0) { |
| 392 | i++; |
| 393 | } |
| 394 | *low = (*high + i) * LOW_THRESHOLD_PERCENTAGE; |
| 395 | #ifdef PRINT_HISTOGRAM |
| 396 | for (i = 0; i < 256; i++) { |
| 397 | printf("i %d count %d\n", i, histogram[i]); |
| 398 | } |
| 399 | #endif |
| 400 |
| 401 | #ifdef CLOCK |
| 402 | printf("Estimate threshold - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 403 | #endif |
| 404 | } |
| 405 |
| 406 | void hysteresis (int high, int low, struct image * img_in, struct image * img_out) |
| 407 | { |
| 408 | #ifdef CLOCK |
| 409 | clock_t start = clock(); |
| 410 | #endif |
| 411 | int x, y, n, max; |
| 412 | max = img_in->width * img_in->height; |
| 413 | for (n = 0; n < max; n++) { |
| 414 | img_out->pixel_data[n] = 0x00; |
| 415 | } |
| 416 | for (y=0; y < img_out->height; y++) { |
| 417 | for (x=0; x < img_out->width; x++) { |
| 418 | if (img_in->pixel_data[y * img_out->width + x] >= high) { |
| 419 | trace (x, y, low, img_in, img_out); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | #ifdef CLOCK |
| 424 | printf("Hysteresis - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 425 | #endif |
| 426 | } |
| 427 |
| 428 | int trace(int x, int y, int low, struct image * img_in, struct image * img_out) |
| 429 | { |
| 430 | int y_off, x_off;//, flag; |
| 431 | if (img_out->pixel_data[y * img_out->width + x] == 0) |
| 432 | { |
| 433 | img_out->pixel_data[y * img_out->width + x] = 0xFF; |
| 434 | for (y_off = -1; y_off <=1; y_off++) |
| 435 | { |
| 436 | for(x_off = -1; x_off <= 1; x_off++) |
| 437 | { |
| 438 | if (!(y == 0 && x_off == 0) && range(img_in, x + x_off, y + y_off) && img_in->pixel_data[(y + y_off) * img_out->width + x + x_off] >= low) { |
| 439 | if (trace(x + x_off, y + y_off, low, img_in, img_out)) |
| 440 | { |
| 441 | return(1); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | return(1); |
| 447 | } |
| 448 | return(0); |
| 449 | } |
| 450 |
| 451 | int range(struct image * img, int x, int y) |
| 452 | { |
| 453 | if ((x < 0) || (x >= img->width)) { |
| 454 | return(0); |
| 455 | } |
| 456 | if ((y < 0) || (y >= img->height)) { |
| 457 | return(0); |
| 458 | } |
| 459 | return(1); |
| 460 | } |
| 461 |
| 462 | void dilate_1d_h(struct image * img, struct image * img_out) { |
| 463 | int x, y, offset, y_max; |
| 464 | y_max = img->height * (img->width - 2); |
| 465 | for (y = 2 * img->width; y < y_max; y += img->width) { |
| 466 | for (x = 2; x < img->width - 2; x++) { |
| 467 | offset = x + y; |
| 468 | img_out->pixel_data[offset] = max(max(max(max(img->pixel_data[offset-2], img->pixel_data[offset-1]), img->pixel_data[offset]), img->pixel_data[offset+1]), img->pixel_data[offset+2]); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 |
| 473 | void dilate_1d_v(struct image * img, struct image * img_out) { |
| 474 | int x, y, offset, y_max; |
| 475 | y_max = img->height * (img->width - 2); |
| 476 | for (y = 2 * img->width; y < y_max; y += img->width) { |
| 477 | for (x = 2; x < img->width - 2; x++) { |
| 478 | offset = x + y; |
| 479 | img_out->pixel_data[offset] = max(max(max(max(img->pixel_data[offset-2 * img->width], img->pixel_data[offset-img->width]), img->pixel_data[offset]), img->pixel_data[offset+img->width]), img->pixel_data[offset+2*img->width]); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 |
| 484 | void erode_1d_h(struct image * img, struct image * img_out) { |
| 485 | int x, y, offset, y_max; |
| 486 | y_max = img->height * (img->width - 2); |
| 487 | for (y = 2 * img->width; y < y_max; y += img->width) { |
| 488 | for (x = 2; x < img->width - 2; x++) { |
| 489 | offset = x + y; |
| 490 | img_out->pixel_data[offset] = min(min(min(min(img->pixel_data[offset-2], img->pixel_data[offset-1]), img->pixel_data[offset]), img->pixel_data[offset+1]), img->pixel_data[offset+2]); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 |
| 495 | void erode_1d_v(struct image * img, struct image * img_out) { |
| 496 | int x, y, offset, y_max; |
| 497 | y_max = img->height * (img->width - 2); |
| 498 | for (y = 2 * img->width; y < y_max; y += img->width) { |
| 499 | for (x = 2; x < img->width - 2; x++) { |
| 500 | offset = x + y; |
| 501 | img_out->pixel_data[offset] = min(min(min(min(img->pixel_data[offset-2 * img->width], img->pixel_data[offset-img->width]), img->pixel_data[offset]), img->pixel_data[offset+img->width]), img->pixel_data[offset+2*img->width]); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 |
| 506 | void erode(struct image * img_in, struct image * img_scratch, struct image * img_out) { |
| 507 | #ifdef CLOCK |
| 508 | clock_t start = clock(); |
| 509 | #endif |
| 510 | erode_1d_h(img_in, img_scratch); |
| 511 | erode_1d_v(img_scratch, img_out); |
| 512 | #ifdef CLOCK |
| 513 | printf("Erosion - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 514 | #endif |
| 515 | } |
| 516 |
| 517 | void dilate(struct image * img_in, struct image * img_scratch, struct image * img_out) { |
| 518 | #ifdef CLOCK |
| 519 | clock_t start = clock(); |
| 520 | #endif |
| 521 | dilate_1d_h(img_in, img_scratch); |
| 522 | dilate_1d_v(img_scratch, img_out); |
| 523 | #ifdef CLOCK |
| 524 | printf("Dilation - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 525 | #endif |
| 526 | } |
| 527 |
| 528 | void morph_open(struct image * img_in, struct image * img_scratch, struct image * img_scratch2, struct image * img_out) { |
| 529 | #ifdef CLOCK |
| 530 | clock_t start = clock(); |
| 531 | #endif |
| 532 | erode(img_in, img_scratch, img_scratch2); |
| 533 | dilate(img_scratch2, img_scratch, img_out); |
| 534 | #ifdef CLOCK |
| 535 | printf("Morphological opening - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 536 | #endif |
| 537 | } |
| 538 |
| 539 | void morph_close(struct image * img_in, struct image * img_scratch, struct image * img_scratch2, struct image * img_out) { |
| 540 | #ifdef CLOCK |
| 541 | clock_t start = clock(); |
| 542 | #endif |
| 543 | dilate(img_in, img_scratch, img_scratch2); |
| 544 | erode(img_scratch2, img_scratch, img_out); |
| 545 | #ifdef CLOCK |
| 546 | printf("Morphological closing - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); |
| 547 | #endif |
| 548 | } |
| 549 |
| 550 | } |
| 551 |