Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program New;
- {$I SCARExt/SCARExt.scar}
- function GetBitmapColorTPA(bmp: TSCARBitmap; color: Integer): TPointArray;
- var
- client: TSCARClient;
- begin
- client := SetClient(TSCARBitmapClient.Create(bmp));
- FindColorTolEx(Result, 0, 0, 0, (bmp.Width - 1), (bmp.Height - 1),30);
- SetClient(client).Free;
- end;
- function PolygonArea(TPA:TPointArray): Integer;
- var i:Integer;
- begin
- Result := 0;
- for i:=0 to High(TPA)-1 do
- Result := Result + (TPA[i].x*TPA[i+1].y) - (TPA[i+1].x*TPA[i].y);
- Result := Round(Abs(0.5 * Result));
- end;
- function TPACircularity(TPA:TPointArray): Extended;
- var
- i,area:Integer;
- Arclen,dist: Extended;
- contour:TPointArray;
- begin
- contour := XT_TPAOutline(TPA);
- Area := PolygonArea(contour);
- Arclen := 0;
- for i:=1 to High(contour) do
- begin
- dist := XT_DistEuclidean(contour[i-1], contour[i]);
- ArcLen := ArcLen + dist;
- end;
- Result := ((PI*4) * Area) / Sqr(ArcLen);
- end;
- function ATPAFilterCircularity(const ATPA:T2DPointArray; MinCirc:Extended):T2DPointArray;
- var
- I,j: Integer;
- begin
- j:=0;
- SetLength(Result, Length(ATPA));
- for i:=0 to High(ATPA) do
- begin
- if (TPACircularity(ATPA[i]) >= MinCirc) then
- begin
- Result[j] := ATPA[i]
- Inc(j);
- end;
- end;
- SetLength(Result, j);
- end;
- function TPAConvexity(TPA:TPointArray): Extended;
- var
- hullArea, ContArea:Integer;
- contour,hull:TPointArray;
- begin
- Result := 1.0;
- contour := XT_TPAOutline(TPA);
- hull := XT_ConnectTPA(XT_ConvexHull(TPA));
- if (length(hull) = 0) or (length(contour) = 0) then Exit;
- ContArea := PolygonArea(contour);
- HullArea := PolygonArea(hull);
- if HullArea = 0 then Exit;
- Result := (ContArea / HullArea);
- end;
- function ATPAFilterConvexity(const ATPA:T2DPointArray; MinCirc:Extended):T2DPointArray;
- var
- I,j: Integer;
- begin
- j:=0;
- SetLength(Result, Length(ATPA));
- for i:=0 to High(ATPA) do
- begin
- if (TPAConvexity(ATPA[i]) >= MinCirc) then
- begin
- Result[j] := ATPA[i]
- Inc(j);
- end;
- end;
- SetLength(Result, j);
- end;
- var
- i: Integer;
- TPA:TPointArray;
- ATPA,ATPA2:T2DPointArray;
- bmp:TSCARBitmap;
- begin
- bmp := TSCARBitmap.Create('');
- bmp.loadFromBmp('C:\SCAR Divi\tests\test.bmp');
- TPA := GetBitmapColorTPA(bmp, 0);
- ATPA := XT_ClusterTPA(TPA, 1, True);
- ATPA2 := ATPAFilterConvexity(ATPA, 0.9);
- for i:=0 to high(ATPA2) do
- bmp.setpixels(ATPA2[I], 255);
- DebugBitmap(bmp);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement