Zhang-Suen thinning algorithm: Difference between revisions

m
Line 982:
 
=={{header|Elena}}==
ELENA 35.40 :
{{trans|Java}}
<lang elena>import system'collections.;
import system'routines.;
import extensions.;
import extensions'routines.;
 
const string[] image = (new string[]{
" ",
" ################# ############# ",
Line 1,007:
" ######## ####### ###### ################ ###### ",
" ######## ####### ###### ############# ###### ",
" ").};
 
int[][] nbrs = new int[][]
nbrs = ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1),
{
(-1, 1), (-1, 0), (-1, -1), (0, -1)).
new int[]{0, -1}, new int[]{1, -1}, new int[]{1, 0}, new int[]{1, 1}, new int[]{0, 1},
new (int[]{-1, 1)}, (new int[]{-1, 0)}, (new int[]{-1, -1)}, (new int[]{0, -1)).}
nbrGroups = (((0, 2, 4), (2, 4, 6)), ((0, 2, 6),
};
(0, 4, 6))).
 
int[][][] nbrGroups = new int[][][]
extension<Matrix<CharValue>> zhangsuenOp
{
new int[][]{new int[]{0, 2, 4}, new int[]{2, 4, 6}},
new int[][]{new int[]{0, 2, 6}, new int[]{0, 4, 6}}
};
 
extension< zhangsuenOp : 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(new append:{ x = c.; y = r.; }.);
^ true.
]}
numNeighbors(r,c)
[{
int count := 0.;
0for till(nbrsint lengthi -:= 1)0, do(:i < nbrs.Length, i += 1)
[{
if (self[r + nbrs[i][1]][c + nbrs[i][0]] == $35)
[{ count :+= count + 1. ].}
].};
^ count.;
]}
numTransitions(r,c)
[{
int count := 0.;
0for till(nbrsint lengthi -:= 1)0, do(:i < nbrs.Length, i += 1)
[{
if (self[r + nbrs[i][1]][c + nbrs[i][0]] == $32)
[{
if (self[r + nbrs[i + 1][1]][c + nbrs[i + 1][0]] == $35)
[{
count := count + 1.
].}
].}
].};
^ count.
]}
atLeastOneIsWhite(r, c, step)
[{
int count := 0.;
var group := nbrGroups[step].;
0for(int till:2i do(:= 0, i < 3, i += 1)
[{
for(int j := 0, j < till(group[i].Length, length) seek(:j += 1)
[{
var nbr := nbrs[group[i][j]].;
if (self[r + nbr[1]][c + nbr[0]] == $32)
[{ count := count + 1.; ^ true ].};
^ false.
].}
].};
^ count > 1.
]}
thinImage()
[{
bool firstStep := false.;
bool hasChanged := true.;
var toWhite := List new. List();
while (hasChanged || firstStep)
[{
hasChanged := false.;
firstStep := firstStep inverted.Inverted;
1 tillfor(selfint rowsr -:= 1), do(:r) < self.Rows, r += 1)
[{
1 tillfor(selfint columnsc -:= 1), do(:c < self.Columns, c += 1)
[{
if(self .proceed(r,c,toWhite,firstStep))
[{ hasChanged := true ].}
].}
].};
toWhite .forEach:(:p)[{ self[p .y][p .x] := $32. ].};
toWhite clear.clear()
].}
]}
print()
[{
var it := self enumerator.enumerator();
it forEach(:ch) [ console print(ch," ") ].
while (it next)
[
console writeLine.
it .forEach:(:ch) [{ console .print(ch," ") ].};
]while (it.next())
] {
console writeLine.writeLine();
 
it it.forEach(:(ch) [{ console .print(ch," ") ].}
[}
}.
}
public program()
{
[
Matrix<CharValue> grid := MatrixSpace::class Matrix<CharValue>.load(new
{
int rowsRows = image length.Length;
int columnsColumns = image[0] length.Length;
getAtat(int i, int j)
= image[i][j].;
[});
grid thinImage.thinImage();
setAt(int i, int j, object o)
[
image[i][j] := o.
]
}.
 
grid thinImage.
grid print.print();
console .readChar()
]}</lang>
{{out}}
<pre>
Anonymous user