Skip to content

Commit de32fd0

Browse files
authored
make Grow/Erode use same neighbour amount; fix Erode double-peel per iteration (#549)
Fixes #548
1 parent 90395fa commit de32fd0

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

Source/simba.vartype_pointarray.pas

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,11 +1111,12 @@ function TPointArrayHelper.Area: Double;
11111111
function TPointArrayHelper.Erode(Iterations: Integer): TPointArray;
11121112
var
11131113
I, J, X, Y: Integer;
1114-
Matrix: TBooleanMatrix;
1114+
Matrix: TByteMatrix; // 0 = removed, 1 = filled, 2 = enqueued
11151115
QueueA, QueueB: TSimbaPointBuffer;
11161116
face: TPointArray;
11171117
pt: TPoint;
11181118
B: TBox;
1119+
Edges: TPointArray;
11191120
begin
11201121
Result := Default(TPointArray);
11211122
if (Length(Self) = 0) or (Iterations = 0) then
@@ -1129,10 +1130,21 @@ function TPointArrayHelper.Erode(Iterations: Integer): TPointArray;
11291130

11301131
Matrix.SetSize(B.X2, B.Y2);
11311132
for I:=0 to High(Self) do
1132-
Matrix[Self[I].Y - B.Y1][Self[I].X - B.X1] := True;
1133+
Matrix[Self[I].Y - B.Y1][Self[I].X - B.X1] := 1;
1134+
1135+
Edges := Self.Edges().Offset(-B.X1, -B.Y1);
1136+
QueueA.Init();
1137+
for I:=0 to High(Edges) do
1138+
begin
1139+
pt := Edges[I];
1140+
if Matrix[pt.y][pt.x] = 1 then
1141+
begin
1142+
Matrix[pt.y][pt.x] := 2;
1143+
QueueA.Add(pt);
1144+
end;
1145+
end;
11331146

1134-
SetLength(face, 4);
1135-
QueueA.InitWith(Self.Edges().Offset(-B.X1, -B.Y1));
1147+
SetLength(face, 8);
11361148
QueueB.Init();
11371149
J := 0;
11381150
repeat
@@ -1141,31 +1153,31 @@ function TPointArrayHelper.Erode(Iterations: Integer): TPointArray;
11411153
while (QueueA.Count > 0) do
11421154
begin
11431155
pt := QueueA.Pop;
1144-
Matrix[pt.y][pt.x] := False;
1145-
GetAdjacent4(face, pt);
1146-
for I:=0 to 3 do
1156+
Matrix[pt.y][pt.x] := 0;
1157+
1158+
GetAdjacent8(face, pt);
1159+
for I:=0 to 7 do
11471160
begin
11481161
pt := face[I];
1149-
if Matrix[pt.y][pt.x] then
1162+
if Matrix[pt.y][pt.x] = 1 then
11501163
begin
1151-
Matrix[pt.y][pt.x] := False;
1164+
Matrix[pt.y][pt.x] := 2;
11521165
QueueB.Add(pt);
11531166
end;
11541167
end;
11551168
end;
1156-
11571169
False:
11581170
while (QueueB.Count > 0) do
11591171
begin
11601172
pt := QueueB.Pop;
1161-
Matrix[pt.y][pt.x] := False;
1162-
GetAdjacent4(face, pt);
1163-
for I:=0 to 3 do
1173+
Matrix[pt.y][pt.x] := 0;
1174+
GetAdjacent8(face, pt);
1175+
for I:=0 to 7 do
11641176
begin
11651177
pt := face[I];
1166-
if Matrix[pt.y][pt.x] then
1178+
if Matrix[pt.y][pt.x] = 1 then
11671179
begin
1168-
Matrix[pt.y][pt.x] := False;
1180+
Matrix[pt.y][pt.x] := 2;
11691181
QueueA.Add(pt);
11701182
end;
11711183
end;
@@ -1177,7 +1189,7 @@ function TPointArrayHelper.Erode(Iterations: Integer): TPointArray;
11771189
QueueA.Clear();
11781190
for Y := 0 to B.Y2-1 do
11791191
for X := 0 to B.X2-1 do
1180-
if Matrix[Y, X] then
1192+
if Matrix[Y, X] > 0 then
11811193
QueueA.Add(X + B.X1, Y + B.Y1);
11821194
Result := QueueA.ToArray(False);
11831195
end;
@@ -1205,7 +1217,7 @@ function TPointArrayHelper.Grow(Iterations: Integer): TPointArray;
12051217
for I:=0 to High(Self) do
12061218
Matrix[Self[I].Y - B.Y1][Self[I].X - B.X1] := True;
12071219

1208-
SetLength(face,4);
1220+
SetLength(face,8);
12091221
QueueA.InitWith(Self.Edges().Offset(-B.X1,-B.Y1));
12101222
QueueB.Init();
12111223
J := 0;
@@ -1214,8 +1226,8 @@ function TPointArrayHelper.Grow(Iterations: Integer): TPointArray;
12141226
True:
12151227
while (QueueA.Count > 0) do
12161228
begin
1217-
GetAdjacent4(face, QueueA.Pop());
1218-
for I:=0 to 3 do
1229+
GetAdjacent8(face, QueueA.Pop());
1230+
for I:=0 to 7 do
12191231
begin
12201232
pt := face[I];
12211233
if not(Matrix[pt.y][pt.x]) then
@@ -1229,8 +1241,8 @@ function TPointArrayHelper.Grow(Iterations: Integer): TPointArray;
12291241
False:
12301242
while (QueueB.Count > 0) do
12311243
begin
1232-
GetAdjacent4(face, QueueB.Pop());
1233-
for I:=0 to 3 do
1244+
GetAdjacent8(face, QueueB.Pop());
1245+
for I:=0 to 7 do
12341246
begin
12351247
pt := face[I];
12361248
if not(Matrix[pt.y][pt.x]) then

0 commit comments

Comments
 (0)