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

83
LINES

< > BotCompany Repo | #1034927 // ZhangSuenThinner_byteArray

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

Libraryless. Click here for Pure Java version (5635L/31K).

1  
// from https://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm#Java
2  
3  
// each array element is one pixel (0 = background, anything else = foreground)
4  
srecord noeq ZhangSuenThinner_byteArray(int w, int h, byte[] pixels) {
5  
  static final int[][] nbrs = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1},
6  
    {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}};
7  
 
8  
  static final int[][][] nbrGroups = {
9  
    {{0, 2, 4}, {2, 4, 6}},
10  
    {{0, 2, 6}, {0, 4, 6}}};
11  
 
12  
  new PtBuffer toWhite;
13  
14  
  run {
15  
    bool firstStep;
16  
17  
    while ping (true) {
18  
      firstStep = !firstStep;
19  
20  
      for (int r = 1; r < h - 1; r++)
21  
        for (int c = 1; c < w - 1; c++) {
22  
          if (!getPixel(c, r))
23  
            continue;
24  
25  
          int nn = numNeighbors(r, c);
26  
          if (nn < 2 || nn > 6)
27  
            continue;
28  
29  
          if (numTransitions(r, c) != 1)
30  
            continue;
31  
32  
          if (!atLeastOneIsWhite(r, c, firstStep ? 0 : 1))
33  
            continue;
34  
35  
          toWhite.add(c, r);
36  
        }
37  
38  
      if (!firstStep && empty(toWhite)) break;
39  
      for (Pt p : toWhite)
40  
        clearPixel(p.x, p.y);
41  
      toWhite.clear();
42  
    }
43  
  }
44  
45  
  int numNeighbors(int r, int c) {
46  
    int count = 0;
47  
    for (int i = 0; i < nbrs.length - 1; i++)
48  
      if (getPixel(c + nbrs[i][0], r + nbrs[i][1]))
49  
        count++;
50  
    ret count;
51  
  }
52  
53  
  int numTransitions(int r, int c) {
54  
    int count = 0;
55  
    for (int i = 0; i < nbrs.length - 1; i++)
56  
      if (!getPixel(c + nbrs[i][0], r + nbrs[i][1])
57  
        && getPixel(c + nbrs[i+1][0], r + nbrs[i+1][1]))
58  
          count++;
59  
    ret count;
60  
  }
61  
62  
  bool atLeastOneIsWhite(int r, int c, int step) {
63  
    int count = 0;
64  
    int[][] group = nbrGroups[step];
65  
    for (int i = 0; i < 2; i++)
66  
      for (int j = 0; j < group[i].length; j++) {
67  
        int[] nbr = nbrs[group[i][j]];
68  
        if (!getPixel(c + nbr[0], r + nbr[1])) {
69  
          count++;
70  
          break;
71  
        }
72  
      }
73  
    ret count > 1;
74  
  }
75  
  
76  
  bool getPixel(int x, int y) {
77  
    ret pixels[y*w+x] != 0;
78  
  }
79  
  
80  
  void clearPixel(int x, int y) {
81  
    pixels[y*w+x] = 0;
82  
  }
83  
}

Author comment

Began life as a copy of #1034925

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034927
Snippet name: ZhangSuenThinner_byteArray
Eternal ID of this version: #1034927/4
Text MD5: a305bc2cf79b9f765ee8f4c2f09c0306
Transpilation MD5: 8d18fef3ed5ef2c9b0e93c83cadbdca3
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-15 20:49:11
Source code size: 2107 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 179 / 243
Version history: 3 change(s)
Referenced in: [show references]