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.

// from https://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm#Java

// image is _ for bg, anything else for fg
srecord noeq ZhangSuenDemo(S image) {
  static char empty = '_';
  
  static final int[][] nbrs = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1},
        {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}};
 
  static final int[][][] nbrGroups = {{{0, 2, 4}, {2, 4, 6}}, {{0, 2, 6},
        {0, 4, 6}}};
 
    List<Point> toWhite = new ArrayList<>();
    char[][] grid;
 
    S get() {
      var image = toStringArray(lines(this.image));
      grid = new char[image.length][];
      for (int r = 0; r < image.length; r++)
          grid[r] = image[r].toCharArray();

      thinImage();
      
      ret charGridToString(grid);
    }
 
    void thinImage() {
        boolean firstStep = false;
        boolean hasChanged;
 
        do {
            hasChanged = false;
            firstStep = !firstStep;
 
            for (int r = 1; r < grid.length - 1; r++) {
                for (int c = 1; c < grid[0].length - 1; c++) {
 
                    if (grid[r][c] == empty)
                        continue;
 
                    int nn = numNeighbors(r, c);
                    if (nn < 2 || nn > 6)
                        continue;
 
                    if (numTransitions(r, c) != 1)
                        continue;
 
                    if (!atLeastOneIsWhite(r, c, firstStep ? 0 : 1))
                        continue;
 
                    toWhite.add(new Point(c, r));
                    print("Clearing " + c + "/" + r);
                    hasChanged = true;
                }
            }
 
            for (Point p : toWhite)
                grid[p.y][p.x] = empty;
            toWhite.clear();
 
        } while (firstStep || hasChanged);
        
    }
 
    int numNeighbors(int r, int c) {
        int count = 0;
        for (int i = 0; i < nbrs.length - 1; i++)
            if (grid[r + nbrs[i][1]][c + nbrs[i][0]] != empty)
                count++;
        return count;
    }
 
    int numTransitions(int r, int c) {
        int count = 0;
        for (int i = 0; i < nbrs.length - 1; i++)
            if (grid[r + nbrs[i][1]][c + nbrs[i][0]] == empty) {
                if (grid[r + nbrs[i + 1][1]][c + nbrs[i + 1][0]] != empty)
                    count++;
            }
        return count;
    }
 
    boolean atLeastOneIsWhite(int r, int c, int step) {
        int count = 0;
        int[][] group = nbrGroups[step];
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < group[i].length; j++) {
                int[] nbr = nbrs[group[i][j]];
                if (grid[r + nbr[1]][c + nbr[0]] == empty) {
                    count++;
                    break;
                }
            }
        return count > 1;
    }
}

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: 191 / 293
Version history: 5 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)