Uses 1113K of libraries. Click here for Pure Java version (4515L/22K).
1 | !7 |
2 | |
3 | // from https://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm#Java |
4 | |
5 | cprint ZhangSuen { |
6 | |
7 | final String[] image = { |
8 | " ", |
9 | " ################# ############# ", |
10 | " ################## ################ ", |
11 | " ################### ################## ", |
12 | " ######## ####### ################### ", |
13 | " ###### ####### ####### ###### ", |
14 | " ###### ####### ####### ", |
15 | " ################# ####### ", |
16 | " ################ ####### ", |
17 | " ################# ####### ", |
18 | " ###### ####### ####### ", |
19 | " ###### ####### ####### ", |
20 | " ###### ####### ####### ###### ", |
21 | " ######## ####### ################### ", |
22 | " ######## ####### ###### ################## ###### ", |
23 | " ######## ####### ###### ################ ###### ", |
24 | " ######## ####### ###### ############# ###### ", |
25 | " "}; |
26 | |
27 | final int[][] nbrs = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, |
28 | {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}}; |
29 | |
30 | final int[][][] nbrGroups = {{{0, 2, 4}, {2, 4, 6}}, {{0, 2, 6}, |
31 | {0, 4, 6}}}; |
32 | |
33 | List<Point> toWhite = new ArrayList<>(); |
34 | char[][] grid; |
35 | |
36 | start-thread { |
37 | grid = new char[image.length][]; |
38 | for (int r = 0; r < image.length; r++) |
39 | grid[r] = image[r].toCharArray(); |
40 | |
41 | thinImage(); |
42 | } |
43 | |
44 | void thinImage() { |
45 | boolean firstStep = false; |
46 | boolean hasChanged; |
47 | |
48 | do { |
49 | hasChanged = false; |
50 | firstStep = !firstStep; |
51 | |
52 | for (int r = 1; r < grid.length - 1; r++) { |
53 | for (int c = 1; c < grid[0].length - 1; c++) { |
54 | |
55 | if (grid[r][c] != '#') |
56 | continue; |
57 | |
58 | int nn = numNeighbors(r, c); |
59 | if (nn < 2 || nn > 6) |
60 | continue; |
61 | |
62 | if (numTransitions(r, c) != 1) |
63 | continue; |
64 | |
65 | if (!atLeastOneIsWhite(r, c, firstStep ? 0 : 1)) |
66 | continue; |
67 | |
68 | toWhite.add(new Point(c, r)); |
69 | hasChanged = true; |
70 | } |
71 | } |
72 | |
73 | for (Point p : toWhite) |
74 | grid[p.y][p.x] = ' '; |
75 | toWhite.clear(); |
76 | |
77 | } while (firstStep || hasChanged); |
78 | |
79 | printResult(); |
80 | } |
81 | |
82 | int numNeighbors(int r, int c) { |
83 | int count = 0; |
84 | for (int i = 0; i < nbrs.length - 1; i++) |
85 | if (grid[r + nbrs[i][1]][c + nbrs[i][0]] == '#') |
86 | count++; |
87 | return count; |
88 | } |
89 | |
90 | int numTransitions(int r, int c) { |
91 | int count = 0; |
92 | for (int i = 0; i < nbrs.length - 1; i++) |
93 | if (grid[r + nbrs[i][1]][c + nbrs[i][0]] == ' ') { |
94 | if (grid[r + nbrs[i + 1][1]][c + nbrs[i + 1][0]] == '#') |
95 | count++; |
96 | } |
97 | return count; |
98 | } |
99 | |
100 | boolean atLeastOneIsWhite(int r, int c, int step) { |
101 | int count = 0; |
102 | int[][] group = nbrGroups[step]; |
103 | for (int i = 0; i < 2; i++) |
104 | for (int j = 0; j < group[i].length; j++) { |
105 | int[] nbr = nbrs[group[i][j]]; |
106 | if (grid[r + nbr[1]][c + nbr[0]] == ' ') { |
107 | count++; |
108 | break; |
109 | } |
110 | } |
111 | return count > 1; |
112 | } |
113 | |
114 | void printResult() { |
115 | for (char[] row : grid) |
116 | print(str(row)); |
117 | } |
118 | } |
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034923 |
Snippet name: | Zhang-Suen thinning algorithm demo |
Eternal ID of this version: | #1034923/3 |
Text MD5: | 77802290e281b9c5c4bf651c2f2bdf7f |
Transpilation MD5: | 360a2d711f806ce54f11e04fc76729c3 |
Author: | stefan |
Category: | javax / image recognition |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-15 19:54:58 |
Source code size: | 4033 bytes / 118 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 159 / 325 |
Version history: | 2 change(s) |
Referenced in: | [show references] |