Zhang-Suen thinning algorithm: Difference between revisions

Content added Content deleted
m (Changed "thinned" from "proc" to "func".)
(Added 11l)
Line 107: Line 107:
* "Character Recognition Systems: A Guide for Students and Practitioners" By Mohamed Cheriet, Nawwaf Kharma, Cheng-Lin Liu, Ching Suen
* "Character Recognition Systems: A Guide for Students and Practitioners" By Mohamed Cheriet, Nawwaf Kharma, Cheng-Lin Liu, Ching Suen
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Python}}

<lang 11l>V beforeTxt =
‘1100111
1100111
1100111
1100111
1100110
1100110
1100110
1100110
1100110
1100110
1100110
1100110
1111110
0000000’

V smallrc01 =
‘00000000000000000000000000000000
01111111110000000111111110000000
01110001111000001111001111000000
01110000111000001110000111000000
01110001111000001110000000000000
01111111110000001110000000000000
01110111100000001110000111000000
01110011110011101111001111011100
01110001111011100111111110011100
00000000000000000000000000000000’

V rc01 =
‘00000000000000000000000000000000000000000000000000000000000
01111111111111111100000000000000000001111111111111000000000
01111111111111111110000000000000001111111111111111000000000
01111111111111111111000000000000111111111111111111000000000
01111111100000111111100000000001111111111111111111000000000
00011111100000111111100000000011111110000000111111000000000
00011111100000111111100000000111111100000000000000000000000
00011111111111111111000000000111111100000000000000000000000
00011111111111111110000000000111111100000000000000000000000
00011111111111111111000000000111111100000000000000000000000
00011111100000111111100000000111111100000000000000000000000
00011111100000111111100000000111111100000000000000000000000
00011111100000111111100000000011111110000000111111000000000
01111111100000111111100000000001111111111111111111000000000
01111111100000111111101111110000111111111111111111011111100
01111111100000111111101111110000001111111111111111011111100
01111111100000111111101111110000000001111111111111011111100
00000000000000000000000000000000000000000000000000000000000’

F intarray(binstring)
‘Change a 2D matrix of 01 chars into a list of lists of ints’
R binstring.split("\n").map(line -> line.map(ch -> (I ch == ‘1’ {1} E 0)))

F chararray(intmatrix)
‘Change a 2d list of lists of 1/0 ints into lines of 1/0 chars’
R intmatrix.map(row -> row.map(p -> String(p)).join(‘’)).join("\n")

F toTxt(intmatrix)
‘Change a 2d list of lists of 1/0 ints into lines of '#' and '.' chars’
R intmatrix.map(row -> row.map(p -> (I p {‘#’} E ‘.’)).join(‘’)).join("\n")

F neighbours_list(x, y, image)
‘Return 8-neighbours of point p1 of picture, in order’
V i = image
V (x1, y1, x_1, y_1) = (x + 1, y - 1, x - 1, y + 1)
R [i[y1][x], i[y1][x1], i[y][x1], i[y_1][x1], i[y_1][x], i[y_1][x_1], i[y][x_1], i[y1][x_1]]

F neighbours_tuple(x, y, image)
‘Return 8-neighbours of point p1 of picture, in order’
V i = image
V (x1, y1, x_1, y_1) = (x + 1, y - 1, x - 1, y + 1)
R (i[y1][x], i[y1][x1], i[y][x1], i[y_1][x1], i[y_1][x], i[y_1][x_1], i[y][x_1], i[y1][x_1])

F transitions(neighbours)
V s = 0
L(i) 7
s += Int((neighbours[i], neighbours[i + 1]) == (0, 1))
R s + Int((neighbours[7], neighbours[0]) == (0, 1))

F zhangSuen(&image)
V changing1 = [(-1, -1)]
V changing2 = [(-1, -1)]
L !changing1.empty | !changing2.empty
changing1.drop()
L(y) 1 .< image.len - 1
L(x) 1 .< image[0].len - 1
V n = neighbours_list(x, y, image)
V (P2, P3, P4, P5, P6, P7, P8, P9) = neighbours_tuple(x, y, image)
I (image[y][x] == 1 & P4 * P6 * P8 == 0 & P2 * P4 * P6 == 0 & transitions(n) == 1 & sum(n) C 2..6)
changing1.append((x, y))
L(x, y) changing1
image[y][x] = 0
changing2.drop()
L(y) 1 .< image.len - 1
L(x) 1 .< image[0].len - 1
V n = neighbours_list(x, y, image)
V (P2, P3, P4, P5, P6, P7, P8, P9) = neighbours_tuple(x, y, image)
I (image[y][x] == 1 & P2 * P6 * P8 == 0 & P2 * P4 * P8 == 0 & transitions(n) == 1 & sum(n) C 2..6)
changing2.append((x, y))
L(x, y) changing2
image[y][x] = 0
R image

L(picture) (beforeTxt, smallrc01, rc01)
V image = intarray(picture)
print("\nFrom:\n#.".format(toTxt(image)))
V after = zhangSuen(&image)
print("\nTo thinned:\n#.".format(toTxt(after)))</lang>

{{out}}
Just the example asked for in the task:
<pre>
From:
...........................................................
.#################...................#############.........
.##################...............################.........
.###################............##################.........
.########.....#######..........###################.........
...######.....#######.........#######.......######.........
...######.....#######........#######.......................
...#################.........#######.......................
...################..........#######.......................
...#################.........#######.......................
...######.....#######........#######.......................
...######.....#######........#######.......................
...######.....#######.........#######.......######.........
.########.....#######..........###################.........
.########.....#######.######....##################.######..
.########.....#######.######......################.######..
.########.....#######.######.........#############.######..
...........................................................

To thinned:
...........................................................
...........................................................
....#.##########.......................#######.............
.....##........#...................####.......#............
.....#..........#.................##.......................
.....#..........#................#.........................
.....#..........#................#.........................
.....#..........#................#.........................
.....############...............#..........................
.....#..........#...............#..........................
.....#..........#................#.........................
.....#..........#................#.........................
.....#..........#................#.........................
.....#............................##.......................
.....#.............................############............
.......................###..........................###....
...........................................................
...........................................................
</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==