edtaa3func.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * edtaa3()
  3. *
  4. * Sweep-and-update Euclidean distance transform of an
  5. * image. Positive pixels are treated as object pixels,
  6. * zero or negative pixels are treated as background.
  7. * An attempt is made to treat antialiased edges correctly.
  8. * The input image must have pixels in the range [0,1],
  9. * and the antialiased image should be a box-filter
  10. * sampling of the ideal, crisp edge.
  11. * If the antialias region is more than 1 pixel wide,
  12. * the result from this transform will be inaccurate.
  13. *
  14. * By Stefan Gustavson (stefan.gustavson@gmail.com).
  15. *
  16. * Originally written in 1994, based on a verbal
  17. * description of Per-Erik Danielsson's SSED8 algorithm
  18. * as presented in the PhD dissertation of Ingemar
  19. * Ragnemalm. This is Per-Erik Danielsson's scanline
  20. * scheme from 1979 - I only implemented it in C.
  21. *
  22. * Updated in 2004 to treat border pixels correctly,
  23. * and cleaned up the code to improve readability.
  24. *
  25. * Updated in 2009 to handle anti-aliased edges,
  26. * as published in the article "Anti-aliased Euclidean
  27. * distance transform" by Stefan Gustavson and Robin Strand,
  28. * Pattern Recognition Letters 32 (2011) 252¨C257.
  29. *
  30. * Updated in 2011 to avoid a corner case causing an
  31. * infinite loop for some input data.
  32. *
  33. */
  34. /*
  35. Copyright (C) 2009 Stefan Gustavson (stefan.gustavson@gmail.com)
  36. This software is distributed under the permissive "MIT License":
  37. Permission is hereby granted, free of charge, to any person obtaining a copy
  38. of this software and associated documentation files (the "Software"), to deal
  39. in the Software without restriction, including without limitation the rights
  40. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  41. copies of the Software, and to permit persons to whom the Software is
  42. furnished to do so, subject to the following conditions:
  43. The above copyright notice and this permission notice shall be included in
  44. all copies or substantial portions of the Software.
  45. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  46. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  47. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  48. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  49. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  50. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  51. THE SOFTWARE.
  52. */
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #include <math.h>
  57. /*
  58. * Compute the local gradient at edge pixels using convolution filters.
  59. * The gradient is computed only at edge pixels. At other places in the
  60. * image, it is never used, and it's mostly zero anyway.
  61. */
  62. void computegradient(double *img, int w, int h, double *gx, double *gy)
  63. {
  64. int i,j,k;
  65. double glength;
  66. #define SQRT2 1.4142136
  67. for(i = 1; i < h-1; i++) { // Avoid edges where the kernels would spill over
  68. for(j = 1; j < w-1; j++) {
  69. k = i*w + j;
  70. if((img[k]>0.0) && (img[k]<1.0)) { // Compute gradient for edge pixels only
  71. gx[k] = -img[k-w-1] - SQRT2*img[k-1] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+1] + img[k+w+1];
  72. gy[k] = -img[k-w-1] - SQRT2*img[k-w] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+w] + img[k+w+1];
  73. glength = gx[k]*gx[k] + gy[k]*gy[k];
  74. if(glength > 0.0) { // Avoid division by zero
  75. glength = sqrt(glength);
  76. gx[k]=gx[k]/glength;
  77. gy[k]=gy[k]/glength;
  78. }
  79. }
  80. }
  81. }
  82. // TODO: Compute reasonable values for gx, gy also around the image edges.
  83. // (These are zero now, which reduces the accuracy for a 1-pixel wide region
  84. // around the image edge.) 2x2 kernels would be suitable for this.
  85. }
  86. /*
  87. * A somewhat tricky function to approximate the distance to an edge in a
  88. * certain pixel, with consideration to either the local gradient (gx,gy)
  89. * or the direction to the pixel (dx,dy) and the pixel greyscale value a.
  90. * The latter alternative, using (dx,dy), is the metric used by edtaa2().
  91. * Using a local estimate of the edge gradient (gx,gy) yields much better
  92. * accuracy at and near edges, and reduces the error even at distant pixels
  93. * provided that the gradient direction is accurately estimated.
  94. */
  95. double edgedf(double gx, double gy, double a)
  96. {
  97. double df, glength, temp, a1;
  98. if ((gx == 0) || (gy == 0)) { // Either A) gu or gv are zero, or B) both
  99. df = 0.5-a; // Linear approximation is A) correct or B) a fair guess
  100. } else {
  101. glength = sqrt(gx*gx + gy*gy);
  102. if(glength>0) {
  103. gx = gx/glength;
  104. gy = gy/glength;
  105. }
  106. /* Everything is symmetric wrt sign and transposition,
  107. * so move to first octant (gx>=0, gy>=0, gx>=gy) to
  108. * avoid handling all possible edge directions.
  109. */
  110. gx = fabs(gx);
  111. gy = fabs(gy);
  112. if(gx<gy) {
  113. temp = gx;
  114. gx = gy;
  115. gy = temp;
  116. }
  117. a1 = 0.5*gy/gx;
  118. if (a < a1) { // 0 <= a < a1
  119. df = 0.5*(gx + gy) - sqrt(2.0*gx*gy*a);
  120. } else if (a < (1.0-a1)) { // a1 <= a <= 1-a1
  121. df = (0.5-a)*gx;
  122. } else { // 1-a1 < a <= 1
  123. df = -0.5*(gx + gy) + sqrt(2.0*gx*gy*(1.0-a));
  124. }
  125. }
  126. return df;
  127. }
  128. double distaa3(double *img, double *gximg, double *gyimg, int w, int c, int xc, int yc, int xi, int yi)
  129. {
  130. double di, df, dx, dy, gx, gy, a;
  131. int closest;
  132. closest = c-xc-yc*w; // Index to the edge pixel pointed to from c
  133. a = img[closest]; // Grayscale value at the edge pixel
  134. gx = gximg[closest]; // X gradient component at the edge pixel
  135. gy = gyimg[closest]; // Y gradient component at the edge pixel
  136. if(a > 1.0) a = 1.0;
  137. if(a < 0.0) a = 0.0; // Clip grayscale values outside the range [0,1]
  138. if(a == 0.0) return 1000000.0; // Not an object pixel, return "very far" ("don't know yet")
  139. dx = (double)xi;
  140. dy = (double)yi;
  141. di = sqrt(dx*dx + dy*dy); // Length of integer vector, like a traditional EDT
  142. if(di==0) { // Use local gradient only at edges
  143. // Estimate based on local gradient only
  144. df = edgedf(gx, gy, a);
  145. } else {
  146. // Estimate gradient based on direction to edge (accurate for large di)
  147. df = edgedf(dx, dy, a);
  148. }
  149. return di + df; // Same metric as edtaa2, except at edges (where di=0)
  150. }
  151. // Shorthand macro: add ubiquitous parameters img, gx, gy and w and call distaa3()
  152. #define DISTAA(c,xc,yc,xi,yi) (distaa3(img, gx, gy, w, c, xc, yc, xi, yi))
  153. void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist)
  154. {
  155. int x, y, i, c;
  156. int offset_u, offset_ur, offset_r, offset_rd,
  157. offset_d, offset_dl, offset_l, offset_lu;
  158. double olddist, newdist;
  159. int cdistx, cdisty, newdistx, newdisty;
  160. int changed;
  161. double epsilon = 1e-3; // Safeguard against errors due to limited precision
  162. /* Initialize index offsets for the current image width */
  163. offset_u = -w;
  164. offset_ur = -w+1;
  165. offset_r = 1;
  166. offset_rd = w+1;
  167. offset_d = w;
  168. offset_dl = w-1;
  169. offset_l = -1;
  170. offset_lu = -w-1;
  171. /* Initialize the distance images */
  172. for(i=0; i<w*h; i++) {
  173. distx[i] = 0; // At first, all pixels point to
  174. disty[i] = 0; // themselves as the closest known.
  175. if(img[i] <= 0.0)
  176. {
  177. dist[i]= 1000000.0; // Big value, means "not set yet"
  178. }
  179. else if (img[i]<1.0) {
  180. dist[i] = edgedf(gx[i], gy[i], img[i]); // Gradient-assisted estimate
  181. }
  182. else {
  183. dist[i]= 0.0; // Inside the object
  184. }
  185. }
  186. /* Perform the transformation */
  187. do
  188. {
  189. changed = 0;
  190. /* Scan rows, except first row */
  191. for(y=1; y<h; y++)
  192. {
  193. /* move index to leftmost pixel of current row */
  194. i = y*w;
  195. /* scan right, propagate distances from above & left */
  196. /* Leftmost pixel is special, has no left neighbors */
  197. olddist = dist[i];
  198. if(olddist > 0) // If non-zero distance or not set yet
  199. {
  200. c = i + offset_u; // Index of candidate for testing
  201. cdistx = distx[c];
  202. cdisty = disty[c];
  203. newdistx = cdistx;
  204. newdisty = cdisty+1;
  205. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  206. if(newdist < olddist-epsilon)
  207. {
  208. distx[i]=newdistx;
  209. disty[i]=newdisty;
  210. dist[i]=newdist;
  211. olddist=newdist;
  212. changed = 1;
  213. }
  214. c = i+offset_ur;
  215. cdistx = distx[c];
  216. cdisty = disty[c];
  217. newdistx = cdistx-1;
  218. newdisty = cdisty+1;
  219. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  220. if(newdist < olddist-epsilon)
  221. {
  222. distx[i]=newdistx;
  223. disty[i]=newdisty;
  224. dist[i]=newdist;
  225. changed = 1;
  226. }
  227. }
  228. i++;
  229. /* Middle pixels have all neighbors */
  230. for(x=1; x<w-1; x++, i++)
  231. {
  232. olddist = dist[i];
  233. if(olddist <= 0) continue; // No need to update further
  234. c = i+offset_l;
  235. cdistx = distx[c];
  236. cdisty = disty[c];
  237. newdistx = cdistx+1;
  238. newdisty = cdisty;
  239. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  240. if(newdist < olddist-epsilon)
  241. {
  242. distx[i]=newdistx;
  243. disty[i]=newdisty;
  244. dist[i]=newdist;
  245. olddist=newdist;
  246. changed = 1;
  247. }
  248. c = i+offset_lu;
  249. cdistx = distx[c];
  250. cdisty = disty[c];
  251. newdistx = cdistx+1;
  252. newdisty = cdisty+1;
  253. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  254. if(newdist < olddist-epsilon)
  255. {
  256. distx[i]=newdistx;
  257. disty[i]=newdisty;
  258. dist[i]=newdist;
  259. olddist=newdist;
  260. changed = 1;
  261. }
  262. c = i+offset_u;
  263. cdistx = distx[c];
  264. cdisty = disty[c];
  265. newdistx = cdistx;
  266. newdisty = cdisty+1;
  267. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  268. if(newdist < olddist-epsilon)
  269. {
  270. distx[i]=newdistx;
  271. disty[i]=newdisty;
  272. dist[i]=newdist;
  273. olddist=newdist;
  274. changed = 1;
  275. }
  276. c = i+offset_ur;
  277. cdistx = distx[c];
  278. cdisty = disty[c];
  279. newdistx = cdistx-1;
  280. newdisty = cdisty+1;
  281. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  282. if(newdist < olddist-epsilon)
  283. {
  284. distx[i]=newdistx;
  285. disty[i]=newdisty;
  286. dist[i]=newdist;
  287. changed = 1;
  288. }
  289. }
  290. /* Rightmost pixel of row is special, has no right neighbors */
  291. olddist = dist[i];
  292. if(olddist > 0) // If not already zero distance
  293. {
  294. c = i+offset_l;
  295. cdistx = distx[c];
  296. cdisty = disty[c];
  297. newdistx = cdistx+1;
  298. newdisty = cdisty;
  299. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  300. if(newdist < olddist-epsilon)
  301. {
  302. distx[i]=newdistx;
  303. disty[i]=newdisty;
  304. dist[i]=newdist;
  305. olddist=newdist;
  306. changed = 1;
  307. }
  308. c = i+offset_lu;
  309. cdistx = distx[c];
  310. cdisty = disty[c];
  311. newdistx = cdistx+1;
  312. newdisty = cdisty+1;
  313. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  314. if(newdist < olddist-epsilon)
  315. {
  316. distx[i]=newdistx;
  317. disty[i]=newdisty;
  318. dist[i]=newdist;
  319. olddist=newdist;
  320. changed = 1;
  321. }
  322. c = i+offset_u;
  323. cdistx = distx[c];
  324. cdisty = disty[c];
  325. newdistx = cdistx;
  326. newdisty = cdisty+1;
  327. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  328. if(newdist < olddist-epsilon)
  329. {
  330. distx[i]=newdistx;
  331. disty[i]=newdisty;
  332. dist[i]=newdist;
  333. changed = 1;
  334. }
  335. }
  336. /* Move index to second rightmost pixel of current row. */
  337. /* Rightmost pixel is skipped, it has no right neighbor. */
  338. i = y*w + w-2;
  339. /* scan left, propagate distance from right */
  340. for(x=w-2; x>=0; x--, i--)
  341. {
  342. olddist = dist[i];
  343. if(olddist <= 0) continue; // Already zero distance
  344. c = i+offset_r;
  345. cdistx = distx[c];
  346. cdisty = disty[c];
  347. newdistx = cdistx-1;
  348. newdisty = cdisty;
  349. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  350. if(newdist < olddist-epsilon)
  351. {
  352. distx[i]=newdistx;
  353. disty[i]=newdisty;
  354. dist[i]=newdist;
  355. changed = 1;
  356. }
  357. }
  358. }
  359. /* Scan rows in reverse order, except last row */
  360. for(y=h-2; y>=0; y--)
  361. {
  362. /* move index to rightmost pixel of current row */
  363. i = y*w + w-1;
  364. /* Scan left, propagate distances from below & right */
  365. /* Rightmost pixel is special, has no right neighbors */
  366. olddist = dist[i];
  367. if(olddist > 0) // If not already zero distance
  368. {
  369. c = i+offset_d;
  370. cdistx = distx[c];
  371. cdisty = disty[c];
  372. newdistx = cdistx;
  373. newdisty = cdisty-1;
  374. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  375. if(newdist < olddist-epsilon)
  376. {
  377. distx[i]=newdistx;
  378. disty[i]=newdisty;
  379. dist[i]=newdist;
  380. olddist=newdist;
  381. changed = 1;
  382. }
  383. c = i+offset_dl;
  384. cdistx = distx[c];
  385. cdisty = disty[c];
  386. newdistx = cdistx+1;
  387. newdisty = cdisty-1;
  388. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  389. if(newdist < olddist-epsilon)
  390. {
  391. distx[i]=newdistx;
  392. disty[i]=newdisty;
  393. dist[i]=newdist;
  394. changed = 1;
  395. }
  396. }
  397. i--;
  398. /* Middle pixels have all neighbors */
  399. for(x=w-2; x>0; x--, i--)
  400. {
  401. olddist = dist[i];
  402. if(olddist <= 0) continue; // Already zero distance
  403. c = i+offset_r;
  404. cdistx = distx[c];
  405. cdisty = disty[c];
  406. newdistx = cdistx-1;
  407. newdisty = cdisty;
  408. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  409. if(newdist < olddist-epsilon)
  410. {
  411. distx[i]=newdistx;
  412. disty[i]=newdisty;
  413. dist[i]=newdist;
  414. olddist=newdist;
  415. changed = 1;
  416. }
  417. c = i+offset_rd;
  418. cdistx = distx[c];
  419. cdisty = disty[c];
  420. newdistx = cdistx-1;
  421. newdisty = cdisty-1;
  422. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  423. if(newdist < olddist-epsilon)
  424. {
  425. distx[i]=newdistx;
  426. disty[i]=newdisty;
  427. dist[i]=newdist;
  428. olddist=newdist;
  429. changed = 1;
  430. }
  431. c = i+offset_d;
  432. cdistx = distx[c];
  433. cdisty = disty[c];
  434. newdistx = cdistx;
  435. newdisty = cdisty-1;
  436. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  437. if(newdist < olddist-epsilon)
  438. {
  439. distx[i]=newdistx;
  440. disty[i]=newdisty;
  441. dist[i]=newdist;
  442. olddist=newdist;
  443. changed = 1;
  444. }
  445. c = i+offset_dl;
  446. cdistx = distx[c];
  447. cdisty = disty[c];
  448. newdistx = cdistx+1;
  449. newdisty = cdisty-1;
  450. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  451. if(newdist < olddist-epsilon)
  452. {
  453. distx[i]=newdistx;
  454. disty[i]=newdisty;
  455. dist[i]=newdist;
  456. changed = 1;
  457. }
  458. }
  459. /* Leftmost pixel is special, has no left neighbors */
  460. olddist = dist[i];
  461. if(olddist > 0) // If not already zero distance
  462. {
  463. c = i+offset_r;
  464. cdistx = distx[c];
  465. cdisty = disty[c];
  466. newdistx = cdistx-1;
  467. newdisty = cdisty;
  468. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  469. if(newdist < olddist-epsilon)
  470. {
  471. distx[i]=newdistx;
  472. disty[i]=newdisty;
  473. dist[i]=newdist;
  474. olddist=newdist;
  475. changed = 1;
  476. }
  477. c = i+offset_rd;
  478. cdistx = distx[c];
  479. cdisty = disty[c];
  480. newdistx = cdistx-1;
  481. newdisty = cdisty-1;
  482. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  483. if(newdist < olddist-epsilon)
  484. {
  485. distx[i]=newdistx;
  486. disty[i]=newdisty;
  487. dist[i]=newdist;
  488. olddist=newdist;
  489. changed = 1;
  490. }
  491. c = i+offset_d;
  492. cdistx = distx[c];
  493. cdisty = disty[c];
  494. newdistx = cdistx;
  495. newdisty = cdisty-1;
  496. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  497. if(newdist < olddist-epsilon)
  498. {
  499. distx[i]=newdistx;
  500. disty[i]=newdisty;
  501. dist[i]=newdist;
  502. changed = 1;
  503. }
  504. }
  505. /* Move index to second leftmost pixel of current row. */
  506. /* Leftmost pixel is skipped, it has no left neighbor. */
  507. i = y*w + 1;
  508. for(x=1; x<w; x++, i++)
  509. {
  510. /* scan right, propagate distance from left */
  511. olddist = dist[i];
  512. if(olddist <= 0) continue; // Already zero distance
  513. c = i+offset_l;
  514. cdistx = distx[c];
  515. cdisty = disty[c];
  516. newdistx = cdistx+1;
  517. newdisty = cdisty;
  518. newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
  519. if(newdist < olddist-epsilon)
  520. {
  521. distx[i]=newdistx;
  522. disty[i]=newdisty;
  523. dist[i]=newdist;
  524. changed = 1;
  525. }
  526. }
  527. }
  528. }
  529. while(changed); // Sweep until no more updates are made
  530. /* The transformation is completed. */
  531. }
  532. #ifdef __cplusplus
  533. }
  534. #endif