edtaa3func.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2009 Stefan Gustavson ([email protected])
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY STEFAN GUSTAVSON ''AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  18. * EVENT SHALL STEFAN GUSTAVSON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * The views and conclusions contained in the software and documentation are
  27. * those of the authors and should not be interpreted as representing official
  28. * policies, either expressed or implied, of Stefan Gustavson.
  29. *
  30. *
  31. * edtaa3()
  32. *
  33. * Sweep-and-update Euclidean distance transform of an
  34. * image. Positive pixels are treated as object pixels,
  35. * zero or negative pixels are treated as background.
  36. * An attempt is made to treat antialiased edges correctly.
  37. * The input image must have pixels in the range [0,1],
  38. * and the antialiased image should be a box-filter
  39. * sampling of the ideal, crisp edge.
  40. * If the antialias region is more than 1 pixel wide,
  41. * the result from this transform will be inaccurate.
  42. *
  43. * By Stefan Gustavson ([email protected]).
  44. *
  45. * Originally written in 1994, based on a verbal
  46. * description of the SSED8 algorithm published in the
  47. * PhD dissertation of Ingemar Ragnemalm. This is his
  48. * algorithm, I only implemented it in C.
  49. *
  50. * Updated in 2004 to treat border pixels correctly,
  51. * and cleaned up the code to improve readability.
  52. *
  53. * Updated in 2009 to handle anti-aliased edges.
  54. *
  55. * Updated in 2011 to avoid a corner case infinite loop.
  56. *
  57. */
  58. #ifndef __EDTAA3FUNC_H__
  59. #define __EDTAA3FUNC_H__
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #include <math.h>
  64. /*
  65. * Compute the local gradient at edge pixels using convolution filters.
  66. * The gradient is computed only at edge pixels. At other places in the
  67. * image, it is never used, and it's mostly zero anyway.
  68. */
  69. void computegradient(double *img, int w, int h, double *gx, double *gy);
  70. /*
  71. * A somewhat tricky function to approximate the distance to an edge in a
  72. * certain pixel, with consideration to either the local gradient (gx,gy)
  73. * or the direction to the pixel (dx,dy) and the pixel greyscale value a.
  74. * The latter alternative, using (dx,dy), is the metric used by edtaa2().
  75. * Using a local estimate of the edge gradient (gx,gy) yields much better
  76. * accuracy at and near edges, and reduces the error even at distant pixels
  77. * provided that the gradient direction is accurately estimated.
  78. */
  79. double edgedf(double gx, double gy, double a);
  80. double distaa3(double *img, double *gximg, double *gyimg, int w, int c, int xc, int yc, int xi, int yi);
  81. // Shorthand macro: add ubiquitous parameters dist, gx, gy, img and w and call distaa3()
  82. #define DISTAA(c,xc,yc,xi,yi) (distaa3(img, gx, gy, w, c, xc, yc, xi, yi))
  83. void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif // __EDTAA3FUNC_H__