Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

74
LINES

< > BotCompany Repo | #1032489 // DistanceTransform [Felz/Hutt algorithm, to test]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5108L/29K).

1  
// Ported from http://cs.brown.edu/people/pfelzens/dt/
2  
sclass DistanceTransform {
3  
  static float INF = 1E20f;
4  
5  
  // dt of 1d function using squared distance
6  
  float[] dt(float[] f, int n) {
7  
    float[] d = new float[n], z = new float[n+1];
8  
    int[] v = new int[n];
9  
    int k = 0;
10  
    z[0] = -INF;
11  
    z[1] = INF;
12  
    for (int q = 1; q <= n-1; q++) {
13  
      float s = ((f[q]+sqr(q))-(f[v[k]]+sqr(v[k])))/(2*q-2*v[k]);
14  
      while (s <= z[k]) {
15  
        k--;
16  
        s = ((f[q]+sqr(q))-(f[v[k]]+sqr(v[k])))/(2*q-2*v[k]);
17  
      }
18  
      k++;
19  
      v[k] = q;
20  
      z[k] = s;
21  
      z[k+1] = INF;
22  
    }
23  
  
24  
    k = 0;
25  
    for (int q = 0; q <= n-1; q++) {
26  
      while (z[k+1] < q)
27  
        k++;
28  
      d[q] = sqr(q-v[k]) + f[v[k]];
29  
    }
30  
  
31  
    ret d;
32  
  }
33  
  
34  
  // dt of 2d function using squared distance
35  
  void dt(FloatBWImage im) {
36  
    int width = im.getWidth();
37  
    int height = im.getHeight();
38  
    float[] f = new[max(width,height)];
39  
  
40  
    // transform along columns
41  
    for x to width: {
42  
      for y to height:
43  
        f[y] = im.getPixel(x, y);
44  
      float[] d = dt(f, height);
45  
      for y to height:
46  
        im.setPixel(x, y, d[y]);
47  
    }
48  
  
49  
    // transform along rows
50  
    for y to height: {
51  
      for x to width:
52  
        f[x] = im.getPixel(x, y);
53  
      float[] d = dt(f, width);
54  
      for x to width:
55  
        im.setPixel(x, y, d[x]);
56  
    }
57  
  }
58  
  
59  
  /* dt of binary image using squared distance */
60  
  FloatBWImage dt(BWImage im, float threshold default .5f, bool brightIsOn) {
61  
    int width = im.getWidth();
62  
    int height = im.getHeight();
63  
  
64  
    var out = new FloatBWImage(width, height);
65  
    for y to height:
66  
      for x to width:
67  
        out.setPixel(x, y,
68  
          (im.getFloatPixel(x, y) >= threshold == brightIsOn)
69  
            ? 0 : INF);
70  
71  
    dt(out);
72  
    ret out;
73  
  }
74  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032489
Snippet name: DistanceTransform [Felz/Hutt algorithm, to test]
Eternal ID of this version: #1032489/12
Text MD5: dece8b329e3a3c14e0175749d045c95a
Transpilation MD5: 3e8d2e75b563dcd07624edeaf2820e5d
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-10 08:07:50
Source code size: 1847 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 123 / 213
Version history: 11 change(s)
Referenced in: [show references]