edtaa3func.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright (C) 2009 Stefan Gustavson (stefan.gustavson@gmail.com)
  3. This software is distributed under the permissive "MIT License":
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /*
  21. * edtaa3()
  22. *
  23. * Sweep-and-update Euclidean distance transform of an
  24. * image. Positive pixels are treated as object pixels,
  25. * zero or negative pixels are treated as background.
  26. * An attempt is made to treat antialiased edges correctly.
  27. * The input image must have pixels in the range [0,1],
  28. * and the antialiased image should be a box-filter
  29. * sampling of the ideal, crisp edge.
  30. * If the antialias region is more than 1 pixel wide,
  31. * the result from this transform will be inaccurate.
  32. *
  33. * By Stefan Gustavson (stefan.gustavson@gmail.com).
  34. *
  35. * Originally written in 1994, based on a verbal
  36. * description of Per-Erik Danielsson's SSED8 algorithm
  37. * as presented in the PhD dissertation of Ingemar
  38. * Ragnemalm. This is Per-Erik Danielsson's scanline
  39. * scheme from 1979 - I only implemented it in C.
  40. *
  41. * Updated in 2004 to treat border pixels correctly,
  42. * and cleaned up the code to improve readability.
  43. *
  44. * Updated in 2009 to handle anti-aliased edges,
  45. * as published in the article "Anti-aliased Euclidean
  46. * distance transform" by Stefan Gustavson and Robin Strand,
  47. * Pattern Recognition Letters 32 (2011) 252¨C257.
  48. *
  49. * Updated in 2011 to avoid a corner case causing an
  50. * infinite loop for some input data.
  51. *
  52. */
  53. #ifndef __EDTAA3FUNC_H__
  54. #define __EDTAA3FUNC_H__
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #include <math.h>
  59. /*
  60. * Compute the local gradient at edge pixels using convolution filters.
  61. * The gradient is computed only at edge pixels. At other places in the
  62. * image, it is never used, and it's mostly zero anyway.
  63. */
  64. void computegradient(double *img, int w, int h, double *gx, double *gy);
  65. /*
  66. * A somewhat tricky function to approximate the distance to an edge in a
  67. * certain pixel, with consideration to either the local gradient (gx,gy)
  68. * or the direction to the pixel (dx,dy) and the pixel greyscale value a.
  69. * The latter alternative, using (dx,dy), is the metric used by edtaa2().
  70. * Using a local estimate of the edge gradient (gx,gy) yields much better
  71. * accuracy at and near edges, and reduces the error even at distant pixels
  72. * provided that the gradient direction is accurately estimated.
  73. */
  74. double edgedf(double gx, double gy, double a);
  75. double distaa3(double *img, double *gximg, double *gyimg, int w, int c, int xc, int yc, int xi, int yi);
  76. // Shorthand macro: add ubiquitous parameters dist, gx, gy, img and w and call distaa3()
  77. #define DISTAA(c,xc,yc,xi,yi) (distaa3(img, gx, gy, w, c, xc, yc, xi, yi))
  78. void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif // __EDTAA3FUNC_H__