Zhang-Suen thinning algorithm: Difference between revisions
Content added Content deleted
m (changed table to be not so compacted, added other whitespace.) |
|||
Line 982: | Line 982: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 3. |
ELENA 3.4 : |
||
{{trans|Java}} |
{{trans|Java}} |
||
<lang elena>import system'collections. |
<lang elena>import system'collections. |
||
Line 988: | Line 988: | ||
import extensions. |
import extensions. |
||
import extensions'routines. |
import extensions'routines. |
||
type charmatrix = matrix<CharValue>. |
|||
const image = ( |
const image = ( |
||
" ", |
" ", |
||
Line 1,010: | Line 1,008: | ||
" ######## ####### ###### ############# ###### ", |
" ######## ####### ###### ############# ###### ", |
||
" "). |
" "). |
||
nbrs = ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1), |
nbrs = ((0, -1), (1, -1), (1, 0), (1, 1), (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), |
nbrGroups = (((0, 2, 4), (2, 4, 6)), ((0, 2, 6), |
||
(0, 4, 6))). |
(0, 4, 6))). |
||
extension<Matrix<CharValue>> zhangsuenOp |
|||
{ |
{ |
||
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:{ x = c. y = r. }. |
||
^ true. |
^ true. |
||
] |
] |
||
numNeighbors |
numNeighbors(r,c) |
||
[ |
[ |
||
int count := 0. |
int count := 0. |
||
0 till(nbrs length - 1) do(:i) |
0 till(nbrs length - 1) do(:i) |
||
[ |
[ |
||
Line 1,049: | Line 1,047: | ||
[ count := count + 1. ]. |
[ count := count + 1. ]. |
||
]. |
]. |
||
⚫ | |||
^ count. |
^ count. |
||
] |
] |
||
numTransitions |
numTransitions(r,c) |
||
[ |
[ |
||
int count := 0. |
int count := 0. |
||
0 till(nbrs length - 1) do(:i) |
0 till(nbrs length - 1) do(:i) |
||
[ |
[ |
||
Line 1,067: | Line 1,065: | ||
]. |
]. |
||
]. |
]. |
||
^ count. |
^ count. |
||
] |
] |
||
atLeastOneIsWhite |
atLeastOneIsWhite(r, c, step) |
||
[ |
[ |
||
int count := 0. |
int count := 0. |
||
Line 1,080: | Line 1,078: | ||
[ |
[ |
||
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 |
||
[ |
[ |
||
Line 1,096: | Line 1,094: | ||
bool hasChanged := true. |
bool hasChanged := true. |
||
var toWhite := List new. |
var toWhite := List new. |
||
while (hasChanged || firstStep) |
while (hasChanged || firstStep) |
||
[ |
[ |
||
hasChanged := false. |
hasChanged := false. |
||
firstStep := firstStep |
firstStep := firstStep inverted. |
||
1 till(self rows - 1) do(:r) |
1 till(self rows - 1) do(:r) |
||
[ |
[ |
||
1 till(self columns - 1) do(:c) |
1 till(self columns - 1) do(:c) |
||
[ |
[ |
||
if(self~zhangsuenOp |
if(self~zhangsuenOp 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," ") ]. |
it forEach(:ch) [ console print(ch," ") ]. |
||
while (it next) |
while (it next) |
||
[ |
[ |
||
console writeLine. |
console writeLine. |
||
it forEach(:ch) [ console print(ch," ") ]. |
it forEach(:ch) [ console print(ch," ") ]. |
||
]. |
]. |
||
] |
] |
||
} |
} |
||
public program |
|||
[ |
[ |
||
Matrix<CharValue> grid := MatrixSpace:: |
|||
{ |
{ |
||
rows = image length. |
int rows = image length. |
||
columns = image[0] length. |
int columns = image[0] length. |
||
getAt |
getAt(int i, int j) |
||
= image[i][j]. |
= image[i][j]. |
||
⚫ | |||
setAt(int i, int j, object o) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
}. |
}. |
||
grid thinImage. |
grid thinImage. |
||
grid print. |
grid print. |
||
console readChar |
console readChar |
||
] |
]</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |