Zhang-Suen thinning algorithm: Difference between revisions

Content added Content deleted
Line 982: Line 982:


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.4 :
ELENA 5.0 :
{{trans|Java}}
{{trans|Java}}
<lang elena>import system'collections.
<lang elena>import system'collections;
import system'routines.
import system'routines;
import extensions.
import extensions;
import extensions'routines.
import extensions'routines;

const image = (
const string[] image = new string[]{
" ",
" ",
" ################# ############# ",
" ################# ############# ",
Line 1,007: 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>
{
{
proceed(r, c, toWhite, firstStep)
proceed(r, c, toWhite, firstStep)
[
{
if (self[r][c] != $35)
if (self[r][c] != $35)
[ ^ false ].
{ ^ false };
int nn := self numNeighbors(r,c).
int nn := self.numNeighbors(r,c);
if ((nn < 2) || (nn > 6))
if ((nn < 2) || (nn > 6))
[ ^ false ].
{ ^ false };
if(self numTransitions(r,c) != 1)
if(self.numTransitions(r,c) != 1)
[ ^ false ].
{ ^ false };
ifnot (self atLeastOneIsWhite(r,c,firstStep iif(0,1)))
ifnot (self.atLeastOneIsWhite(r,c,firstStep.iif(0,1)))
[ ^ false ].
{ ^ false };
toWhite append:{ x = c. y = r. }.
toWhite.append(new { x = c; y = r; });
^ true.
^ true
]
}
numNeighbors(r,c)
numNeighbors(r,c)
[
{
int count := 0.
int count := 0;
0 till(nbrs length - 1) do(:i)
for (int i := 0, i < nbrs.Length, i += 1)
[
{
if (self[r + nbrs[i][1]][c + nbrs[i][0]] == $35)
if (self[r + nbrs[i][1]][c + nbrs[i][0]] == $35)
[ count := count + 1. ].
{ count += 1 }
].
};
^ count.
^ count;
]
}
numTransitions(r,c)
numTransitions(r,c)
[
{
int count := 0.
int count := 0;
0 till(nbrs length - 1) do(:i)
for (int i := 0, i < nbrs.Length, i += 1)
[
{
if (self[r + nbrs[i][1]][c + nbrs[i][0]] == $32)
if (self[r + nbrs[i][1]][c + nbrs[i][0]] == $32)
[
{
if (self[r + nbrs[i + 1][1]][c + nbrs[i + 1][0]] == $35)
if (self[r + nbrs[i + 1][1]][c + nbrs[i + 1][0]] == $35)
[
{
count := count + 1.
count := count + 1
].
}
].
}
].
};
^ count.
^ count
]
}
atLeastOneIsWhite(r, c, step)
atLeastOneIsWhite(r, c, step)
[
{
int count := 0.
int count := 0;
var group := nbrGroups[step].
var group := nbrGroups[step];
0 till:2 do(:i)
for(int i := 0, i < 3, i += 1)
[
{
0 till(group[i] length) seek(:j)
for(int j := 0, j < group[i].Length, j += 1)
[
{
var nbr := nbrs[group[i][j]].
var nbr := nbrs[group[i][j]];
if (self[r + nbr[1]][c + nbr[0]] == $32)
if (self[r + nbr[1]][c + nbr[0]] == $32)
[ count := count + 1. ^ true ].
{ count := count + 1; ^ true };
^ false.
^ false
].
}
].
};
^ count > 1.
^ count > 1
]
}
thinImage
thinImage()
[
{
bool firstStep := false.
bool firstStep := false;
bool hasChanged := true.
bool hasChanged := true;
var toWhite := List new.
var toWhite := new List();
while (hasChanged || firstStep)
while (hasChanged || firstStep)
[
{
hasChanged := false.
hasChanged := false;
firstStep := firstStep inverted.
firstStep := firstStep.Inverted;
1 till(self rows - 1) do(:r)
for(int r := 1, r < self.Rows, r += 1)
[
{
1 till(self columns - 1) do(:c)
for(int c := 1, c < self.Columns, c += 1)
[
{
if(self proceed(r,c,toWhite,firstStep))
if(self.proceed(r,c,toWhite,firstStep))
[ hasChanged := true ].
{ hasChanged := true }
].
}
].
};
toWhite forEach(:p)[ self[p y][p x] := $32. ].
toWhite.forEach:(p){ self[p.y][p.x] := $32 };
toWhite clear.
toWhite.clear()
].
}
]
}
print
print()
[
{
var it := self enumerator.
var it := self.enumerator();
it forEach(:ch) [ console print(ch," ") ].
while (it next)
[
console writeLine.
it forEach(:ch) [ console print(ch," ") ].
it.forEach:(ch){ console.print(ch," ") };
].
while (it.next())
]
{
console.writeLine();

it.forEach:(ch){ console.print(ch," ") }
}
}
}
}
public program
public program()
{
[
Matrix<CharValue> grid := MatrixSpace::
Matrix<CharValue> grid := class Matrix<CharValue>.load(new
{
{
int rows = image length.
int Rows = image.Length;
int columns = image[0] length.
int Columns = image[0].Length;
getAt(int i, int j)
at(int i, int j)
= image[i][j].
= image[i][j];
});
grid.thinImage();
setAt(int i, int j, object o)
[
image[i][j] := o.
]
}.

grid thinImage.
grid print.
grid.print();
console readChar
console.readChar()
]</lang>
}</lang>
{{out}}
{{out}}
<pre>
<pre>