Zhang-Suen thinning algorithm: Difference between revisions
Content added Content deleted
Line 982: | Line 982: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA |
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), |
|||
{ |
|||
⚫ | |||
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[][][] |
|||
⚫ | |||
{ |
|||
new int[][]{new int[]{0, 2, 4}, new int[]{2, 4, 6}}, |
|||
new int[][]{new int[]{0, 2, 6}, new int[]{0, 4, 6}} |
|||
}; |
|||
⚫ | |||
{ |
{ |
||
proceed(r, c, toWhite, firstStep) |
proceed(r, c, toWhite, firstStep) |
||
{ |
|||
if (self[r][c] != $35) |
if (self[r][c] != $35) |
||
{ ^ false }; |
|||
int nn := self |
int nn := self.numNeighbors(r,c); |
||
if ((nn < 2) || (nn > 6)) |
if ((nn < 2) || (nn > 6)) |
||
{ ^ false }; |
|||
if(self |
if(self.numTransitions(r,c) != 1) |
||
{ ^ false }; |
|||
ifnot (self |
ifnot (self.atLeastOneIsWhite(r,c,firstStep.iif(0,1))) |
||
{ ^ false }; |
|||
toWhite |
toWhite.append(new { x = c; y = r; }); |
||
^ true |
^ true |
||
} |
|||
numNeighbors(r,c) |
numNeighbors(r,c) |
||
{ |
|||
int count := 0 |
int count := 0; |
||
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 += 1 } |
|||
}; |
|||
^ count |
^ count; |
||
} |
|||
numTransitions(r,c) |
numTransitions(r,c) |
||
{ |
|||
int count := 0 |
int count := 0; |
||
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]; |
||
for(int i := 0, i < 3, i += 1) |
|||
{ |
|||
0 |
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 }; |
|||
^ false |
^ false |
||
} |
|||
}; |
|||
^ count > 1 |
^ count > 1 |
||
} |
|||
thinImage |
thinImage() |
||
{ |
|||
bool firstStep := false |
bool firstStep := false; |
||
bool hasChanged := true |
bool hasChanged := true; |
||
var toWhite := |
var toWhite := new List(); |
||
while (hasChanged || firstStep) |
while (hasChanged || firstStep) |
||
{ |
|||
hasChanged := false |
hasChanged := false; |
||
firstStep := firstStep |
firstStep := firstStep.Inverted; |
||
for(int r := 1, r < self.Rows, r += 1) |
|||
{ |
|||
for(int c := 1, c < self.Columns, c += 1) |
|||
{ |
|||
if(self |
if(self.proceed(r,c,toWhite,firstStep)) |
||
{ hasChanged := true } |
|||
} |
|||
}; |
|||
toWhite |
toWhite.forEach:(p){ self[p.y][p.x] := $32 }; |
||
toWhite |
toWhite.clear() |
||
} |
|||
} |
|||
print |
print() |
||
{ |
|||
var it := self |
var it := self.enumerator(); |
||
⚫ | |||
while (it next) |
|||
⚫ | |||
⚫ | |||
it.forEach:(ch){ console.print(ch," ") }; |
|||
while (it.next()) |
|||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
public program |
public program() |
||
{ |
|||
[ |
|||
Matrix<CharValue> grid := |
Matrix<CharValue> grid := class Matrix<CharValue>.load(new |
||
{ |
{ |
||
int |
int Rows = image.Length; |
||
int |
int Columns = image[0].Length; |
||
at(int i, int j) |
|||
= image[i][j] |
= image[i][j]; |
||
⚫ | |||
⚫ | |||
setAt(int i, int j, object o) |
|||
⚫ | |||
image[i][j] := o. |
|||
] |
|||
⚫ | |||
⚫ | |||
grid |
grid.print(); |
||
console |
console.readChar() |
||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |