Zhang-Suen thinning algorithm: Difference between revisions

Line 334:
...........................................................</pre>
 
=={{header|Perl 6}}==
Takes the original image from a file that may be based on any characters whose low bits are 0 or 1 (which conveniently includes . and #).
<lang perl6>my $DEBUG = 1;
 
my @lines = ([.ords X+& 1] for lines); # The low bits Just Work.
my \v = +@lines;
my \h = +@lines[0];
my @black = @lines.map: *.values; # Flatten to 1-dimensional.
 
my \p8 = [-h-1, -h+0, -h+1, # Flatland distances to 8 neighbors.
0-1, 0+1,
h-1, h+0, h+1].[1,2,4,7,6,5,3,0]; # (in cycle order)
 
my @has8 = gather for 1..v-2 X 1..h-2 -> \y,\x { take y*h + x }
 
repeat while my @goners1 or my @goners2 {
@goners1 = @goners2 = ();
 
sub cycles (@neighbors) { [+] @neighbors Z< @neighbors[].rotate }
sub blacks (@neighbors) { [+] @neighbors }
 
for @has8 -> \p {
next unless @black[p];
my \n = @black[p8 X+ p];
next unless cycles(n) == 1;
next unless 2 <= blacks(n) <= 6;
next if all n[0,2,4] or all n[2,4,6];
@goners1.push: p;
}
@black[@goners1] = 0 xx *;
say "Ping: {[+] @black} remaining after removing ", @goners1 if $DEBUG;
 
for @has8 -> \p {
next unless @black[p];
my \n = @black[p8 X+ p];
next unless cycles(n) == 1;
next unless 2 <= blacks(n) <= 6;
next if all n[0,2,6] or all n[0,4,6];
@goners2.push: p;
}
@black[@goners2] = 0 xx *;
say "Pong: {[+] @black} remaining after removing ", @goners2 if $DEBUG;
}
 
say @black.splice(0,h).join.trans('01' => '.#') while @black;</lang>
{{out}}
<pre>Ping: 66 remaining after removing 33 41 49 56 67 71 74 80 83 86 89 99 106 114 119 120 121 131 135 138 146 169 178 195 197 210 215 217 227 230 233 236 238 240 243 246 249 251 253 257 258 259 263 264 266 268 269 270 273 274 279 280 283 284 285
Pong: 47 remaining after removing 65 73 88 97 104 112 129 137 144 161 167 176 193 198 208 216 225 226 231
Ping: 45 remaining after removing 87 194
Pong: 45 remaining after removing
Ping: 45 remaining after removing
Pong: 45 remaining after removing
................................
..#######.........######........
..#.....#........##.............
..#......#.......#..............
..#.....#........#..............
..#####.#........#..............
.......##........#..............
........#....#...##....##...#...
.........#.........####.........
................................</pre>
=={{header|Python}}==
Several input images are converted.
Anonymous user