Zhang-Suen thinning algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|Kotlin}}: Added attribution)
Line 418: Line 418:


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.2 :
{{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.


#subject charmatrix = matrixof(char).
type charmatrix = matrix<CharValue>.


const image = (
const image = (
Line 452: Line 453:
(0, 4, 6))).
(0, 4, 6))).


extension(charmatrix) zhangsuenOp
charmatrix extension zhangsuenOp
{
{
$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 += { x = c. y = r. }.
toWhite append:{ x = c. y = r. }.
^ true.
^ true.
Line 479: Line 480:
int count := 0.
int count := 0.
0 till:(nbrs length - 1) &doEach:(:i)
0 till(nbrs length - 1) do(:i)
[
[
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 := count + 1. ].
].
].
Line 492: Line 493:
int count := 0.
int count := 0.
0 till:(nbrs length - 1) &doEach:(:i)
0 till(nbrs length - 1) do(:i)
[
[
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.
Line 509: Line 510:
[
[
int count := 0.
int count := 0.
var group := nbrGroups@step.
var group := nbrGroups[step].
0 till:2 &doEach:(:i)
0 till:2 do(:i)
[
[
0 till:(group@i length) &seekEach:(:j)
0 till(group[i] length) seek(:j)
[
[
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.
Line 537: Line 538:
firstStep := firstStep not.
firstStep := firstStep not.


1 till:(self rows - 1) &doEach:(:r)
1 till(self rows - 1) do(:r)
[
[
1 till:(self columns - 1) &doEach:(:c)
1 till(self columns - 1) do(:c)
[
[
if (self $proceed:r:c:toWhite:firstStep)
if(self~zhangsuenOp $proceed(r,c,toWhite,firstStep))
[ hasChanged := true ].
[ hasChanged := true ].
].
].
].
].
toWhite run &each:(:p)[ self@(p y)@(p x) := #32. ].
toWhite forEach(:p)[ self[p y][p x] := $32. ].
toWhite clear.
toWhite clear.
].
].
Line 555: Line 556:
var it := self enumerator.
var it := self enumerator.
it run &each:(:ch) [ console writeLiteral:ch:" ". ].
it forEach(:ch) [ console print(ch," ") ].
while (it next)
while (it next)
[
[
console writeLine.
console writeLine.
it run &each:(:ch) [ console writeLiteral:ch:" ". ].
it forEach(:ch) [ console print(ch," ") ].
].
].
]
]
Line 567: Line 568:
program =
program =
[
[
charmatrix grid := MatrixSpace
charmatrix grid := MatrixSpace::
{
{
rows = image length.
rows = image length.
columns = image@0 length.
columns = image[0] length.
getAt &int:i &int:j
getAt int:i int:j
= image@i@j.
= image[i][j].
}.
}.