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

96
LINES

< > BotCompany Repo | #1034928 // ZhangSuenDemo

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

Transpiled version (5705L) is out of date.

1  
// from https://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm#Java
2  
3  
// image is _ for bg, anything else for fg
4  
srecord noeq ZhangSuenDemo(S image) {
5  
  static char empty = '_';
6  
  
7  
  static final int[][] nbrs = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1},
8  
        {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}};
9  
 
10  
  static final int[][][] nbrGroups = {{{0, 2, 4}, {2, 4, 6}}, {{0, 2, 6},
11  
        {0, 4, 6}}};
12  
 
13  
    List<Point> toWhite = new ArrayList<>();
14  
    char[][] grid;
15  
 
16  
    S get() {
17  
      var image = toStringArray(lines(this.image));
18  
      grid = new char[image.length][];
19  
      for (int r = 0; r < image.length; r++)
20  
          grid[r] = image[r].toCharArray();
21  
22  
      thinImage();
23  
      
24  
      ret charGridToString(grid);
25  
    }
26  
 
27  
    void thinImage() {
28  
        boolean firstStep = false;
29  
        boolean hasChanged;
30  
 
31  
        do {
32  
            hasChanged = false;
33  
            firstStep = !firstStep;
34  
 
35  
            for (int r = 1; r < grid.length - 1; r++) {
36  
                for (int c = 1; c < grid[0].length - 1; c++) {
37  
 
38  
                    if (grid[r][c] == empty)
39  
                        continue;
40  
 
41  
                    int nn = numNeighbors(r, c);
42  
                    if (nn < 2 || nn > 6)
43  
                        continue;
44  
 
45  
                    if (numTransitions(r, c) != 1)
46  
                        continue;
47  
 
48  
                    if (!atLeastOneIsWhite(r, c, firstStep ? 0 : 1))
49  
                        continue;
50  
 
51  
                    toWhite.add(new Point(c, r));
52  
                    print("Clearing " + c + "/" + r);
53  
                    hasChanged = true;
54  
                }
55  
            }
56  
 
57  
            for (Point p : toWhite)
58  
                grid[p.y][p.x] = empty;
59  
            toWhite.clear();
60  
 
61  
        } while (firstStep || hasChanged);
62  
        
63  
    }
64  
 
65  
    int numNeighbors(int r, int c) {
66  
        int count = 0;
67  
        for (int i = 0; i < nbrs.length - 1; i++)
68  
            if (grid[r + nbrs[i][1]][c + nbrs[i][0]] != empty)
69  
                count++;
70  
        return count;
71  
    }
72  
 
73  
    int numTransitions(int r, int c) {
74  
        int count = 0;
75  
        for (int i = 0; i < nbrs.length - 1; i++)
76  
            if (grid[r + nbrs[i][1]][c + nbrs[i][0]] == empty) {
77  
                if (grid[r + nbrs[i + 1][1]][c + nbrs[i + 1][0]] != empty)
78  
                    count++;
79  
            }
80  
        return count;
81  
    }
82  
 
83  
    boolean atLeastOneIsWhite(int r, int c, int step) {
84  
        int count = 0;
85  
        int[][] group = nbrGroups[step];
86  
        for (int i = 0; i < 2; i++)
87  
            for (int j = 0; j < group[i].length; j++) {
88  
                int[] nbr = nbrs[group[i][j]];
89  
                if (grid[r + nbr[1]][c + nbr[0]] == empty) {
90  
                    count++;
91  
                    break;
92  
                }
93  
            }
94  
        return count > 1;
95  
    }
96  
}

Author comment

Began life as a copy of #1034923

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034928
Snippet name: ZhangSuenDemo
Eternal ID of this version: #1034928/6
Text MD5: 8722de64cfbbcb74cc6ec45c9f7dba2e
Author: stefan
Category: javax / image recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-15 23:18:21
Source code size: 2856 bytes / 96 lines
Pitched / IR pitched: No / No
Views / Downloads: 192 / 295
Version history: 5 change(s)
Referenced in: [show references]