Advertisement
WarPie90

TPACircularity

Dec 21st, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.75 KB | None | 0 0
  1. program New;
  2. {$I SCARExt/SCARExt.scar}
  3.  
  4. function GetBitmapColorTPA(bmp: TSCARBitmap; color: Integer): TPointArray;
  5. var
  6.   client: TSCARClient;
  7. begin
  8.   client := SetClient(TSCARBitmapClient.Create(bmp));
  9.   FindColorTolEx(Result, 0, 0, 0, (bmp.Width - 1), (bmp.Height - 1),30);
  10.   SetClient(client).Free;
  11. end;
  12.  
  13.  
  14. function PolygonArea(TPA:TPointArray): Integer;
  15. var i:Integer;
  16. begin
  17.   Result := 0;
  18.   for i:=0 to High(TPA)-1 do
  19.     Result := Result + (TPA[i].x*TPA[i+1].y) - (TPA[i+1].x*TPA[i].y);
  20.   Result := Round(Abs(0.5 * Result));
  21. end;
  22.  
  23.  
  24. function TPACircularity(TPA:TPointArray): Extended;
  25. var
  26.   i,area:Integer;
  27.   Arclen,dist: Extended;
  28.   contour:TPointArray;
  29. begin
  30.   contour := XT_TPAOutline(TPA);
  31.   Area := PolygonArea(contour);
  32.   Arclen := 0;
  33.   for i:=1 to High(contour) do
  34.   begin          
  35.     dist := XT_DistEuclidean(contour[i-1],  contour[i]);
  36.     ArcLen := ArcLen + dist;
  37.   end;  
  38.   Result := ((PI*4) * Area) / Sqr(ArcLen);
  39. end;
  40.  
  41.  
  42. function ATPAFilterCircularity(const ATPA:T2DPointArray; MinCirc:Extended):T2DPointArray;
  43. var
  44.   I,j: Integer;
  45. begin
  46.   j:=0;
  47.   SetLength(Result, Length(ATPA));
  48.   for i:=0 to High(ATPA) do
  49.   begin
  50.     if (TPACircularity(ATPA[i]) >= MinCirc) then
  51.     begin      
  52.       WriteLn(TPACircularity(ATPA[i]));
  53.       Result[j] := ATPA[i]
  54.       Inc(j);
  55.     end;
  56.   end;
  57.   SetLength(Result, j);
  58. end;
  59.  
  60.  
  61. var
  62.   i: Integer;
  63.   bmp:TSCARBitmap;
  64.   TPA:TPointArray;
  65.   ATPA,ATPA2:T2DPointArray;
  66. begin
  67.   bmp := TSCARBitmap.Create('');
  68.   bmp.loadFromBmp('C:\SCAR Divi\tests\shapes.bmp');
  69.   TPA := GetBitmapColorTPA(bmp, 0);
  70.   ATPA := XT_ClusterTPA(TPA, 1, True);    
  71.  
  72.   ATPA2 := ATPAFilterCircularity(ATPA, 0.1);
  73.   for i:=0 to high(ATPA2) do
  74.     bmp.setpixels(ATPA2[I], 255);
  75.   DebugBitmap(bmp);
  76. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement