Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TSingleMatrix.ArgExtremaXY(Count: Int32; HiLo: Boolean = True; XYIntersection: Boolean = True): TPointArray;
- var
- W, H: Integer;
- Buffer: TPointArray;
- function pass_x(): TPointArray;
- var
- c,X,Y: Integer;
- begin
- for Y:=0 to H-1 do
- begin
- X := 1;
- while (X < W) do
- begin
- while (X < W) and (Self[Y,X] >= Self[Y,X-1]) do Inc(X);
- Buffer[c] := [X-1,Y];
- Inc(c);
- while (X < W) and (Self[Y,X] <= Self[Y,X-1]) do Inc(X);
- end;
- end;
- Result := Copy(Buffer, 0, c);
- end;
- function pass_y(): TPointArray;
- var
- c,X,Y: Integer;
- begin
- for X:=0 to W-1 do
- begin
- Y := 1;
- while (Y < H) do
- begin
- while (Y < H) and (Self[Y,X] >= Self[Y-1,X]) do Inc(Y);
- Buffer[c] := [X,Y-1];
- Inc(c);
- while (Y < H) and (Self[Y,X] <= Self[Y-1,X]) do Inc(Y);
- end;
- end;
- Result := Copy(Buffer, 0, c);
- end;
- var
- I: Integer;
- Weights: TSingleArray;
- begin
- W := Self.Width();
- H := Self.Height();
- SetLength(buffer, W*H);
- if XYIntersection then
- begin
- Result := pass_x().Intersection(pass_y());
- end else begin
- Result := pass_x();
- Result.Extend(pass_y());
- end;
- // just use sort, since there arn't that many peaks
- SetLength(Weights, Length(Result));
- for I := 0 to High(Result) do
- Weights[I] := Self[Result[I].Y, Result[I].X];
- Result.Sort(Weights, not HiLo);
- if (Length(Result) > Count) then
- SetLength(Result, Count);
- end;
- function DistanceTransform(pts: TPointArray): TSingleMatrix;
- var
- inverse: TPointArray;
- itree: TSlackTree;
- b: TBox;
- i: Int32;
- begin
- inverse := pts.Invert();
- b := pts.Bounds.Combine(inverse.Bounds);
- Result.SetSize(b.x2+1, b.y2+1);
- itree.Init(inverse);
- for i:=0 to High(pts) do
- Result[pts[i].y, pts[i].x] := itree.Nearest(pts[i]).DistanceTo(pts[i]);
- end;
- function QuickSkeleton(pts: TPointArray): TPointArray;
- var
- m: TSingleMatrix;
- begin
- m := DistanceTransform(pts);
- Result := m.ArgExtremaXY($FFFFFF, True, False);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement