Zhang-Suen thinning algorithm: Difference between revisions

Content added Content deleted
m (changed table to be not so compacted, added other whitespace.)
Line 982:
 
=={{header|Elena}}==
ELENA 3.24 :
{{trans|Java}}
<lang elena>import system'collections.
Line 988:
import extensions.
import extensions'routines.
 
type charmatrix = matrix<CharValue>.
 
const image = (
" ",
Line 1,010 ⟶ 1,008:
" ######## ####### ###### ############# ###### ",
" ").
 
nbrs = ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1),
(-1, 1), (-1, 0), (-1, -1), (0, -1)).
 
nbrGroups = (((0, 2, 4), (2, 4, 6)), ((0, 2, 6),
(0, 4, 6))).
 
charmatrix extension<Matrix<CharValue>> zhangsuenOp
{
$proceed : (r :, c :, toWhite :, firstStep)
[
if (self[r][c] != $35)
[ ^ false ].
int nn := self numNeighbors(r,c).
if ((nn < 2) || (nn > 6))
[ ^ false ].
if(self numTransitions(r,c) != 1)
[ ^ false ].
 
ifnot (self atLeastOneIsWhite(r,c,firstStep iif(0,1)))
[ ^ false ].
 
toWhite append:{ x = c. y = r. }.
^ true.
]
numNeighbors :(r : ,c)
[
int count := 0.
0 till(nbrs length - 1) do(:i)
[
Line 1,049 ⟶ 1,047:
[ count := count + 1. ].
].
^ count.
]
 
numTransitions : (r : ,c)
[
int count := 0.
0 till(nbrs length - 1) do(:i)
[
Line 1,067 ⟶ 1,065:
].
].
^ count.
]
atLeastOneIsWhite : (r :, c :, step)
[
int count := 0.
Line 1,080 ⟶ 1,078:
[
var nbr := nbrs[group[i][j]].
if (self[r + nbr[1]][c + nbr[0]] == $32)
[ count := count + 1. ^ true ].
^ false.
].
].
^ count > 1.
]
thinImage
[
Line 1,096 ⟶ 1,094:
bool hasChanged := true.
var toWhite := List new.
 
while (hasChanged || firstStep)
[
hasChanged := false.
firstStep := firstStep notinverted.
 
1 till(self rows - 1) do(:r)
[
1 till(self columns - 1) do(:c)
[
if(self~zhangsuenOp $proceed(r,c,toWhite,firstStep))
[ hasChanged := true ].
].
].
toWhite forEach(:p)[ self[p y][p x] := $32. ].
toWhite clear.
].
]
print
[
var it := self enumerator.
it forEach(:ch) [ console print(ch," ") ].
while (it next)
[
console writeLine.
it forEach(:ch) [ console print(ch," ") ].
].
]
}
 
programpublic =program
[
charmatrixMatrix<CharValue> grid := MatrixSpace::
{
int rows = image length.
int columns = image[0] length.
getAt (int: i, int: j)
= image[i][j].
setAt(int i, int j, object o)
[
image[i][j] := o.
]
}.
 
grid thinImage.
grid print.
console readChar.
].</lang>
{{out}}
<pre>