Advertisement
WarPie90

TPAConvexity

Dec 21st, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.50 KB | None | 0 0
  1. program New;
  2. {$I SCARExt/SCARExt.scar}
  3.  
  4.  
  5. function GetBitmapColorTPA(bmp: TSCARBitmap; color: Integer): TPointArray;
  6. var
  7.   client: TSCARClient;
  8. begin
  9.   client := SetClient(TSCARBitmapClient.Create(bmp));
  10.   FindColorTolEx(Result, 0, 0, 0, (bmp.Width - 1), (bmp.Height - 1),30);
  11.   SetClient(client).Free;
  12. end;
  13.  
  14.  
  15.  
  16. function PolygonArea(TPA:TPointArray): Integer;
  17. var i:Integer;
  18. begin
  19.   Result := 0;
  20.   for i:=0 to High(TPA)-1 do
  21.     Result := Result + (TPA[i].x*TPA[i+1].y) - (TPA[i+1].x*TPA[i].y);
  22.   Result := Round(Abs(0.5 * Result));
  23. end;
  24.  
  25.  
  26.  
  27. function TPACircularity(TPA:TPointArray): Extended;
  28. var
  29.   i,area:Integer;
  30.   Arclen,dist: Extended;
  31.   contour:TPointArray;
  32. begin
  33.   contour := XT_TPAOutline(TPA);
  34.   Area := PolygonArea(contour);
  35.   Arclen := 0;
  36.   for i:=1 to High(contour) do
  37.   begin          
  38.     dist := XT_DistEuclidean(contour[i-1],  contour[i]);
  39.     ArcLen := ArcLen + dist;
  40.   end;  
  41.   Result := ((PI*4) * Area) / Sqr(ArcLen);
  42. end;
  43.  
  44.  
  45.  
  46. function ATPAFilterCircularity(const ATPA:T2DPointArray; MinCirc:Extended):T2DPointArray;
  47. var
  48.   I,j: Integer;
  49. begin
  50.   j:=0;
  51.   SetLength(Result, Length(ATPA));
  52.   for i:=0 to High(ATPA) do
  53.   begin
  54.     if (TPACircularity(ATPA[i]) >= MinCirc) then
  55.     begin
  56.       Result[j] := ATPA[i]
  57.       Inc(j);
  58.     end;
  59.   end;
  60.   SetLength(Result, j);
  61. end;
  62.  
  63.  
  64.  
  65.  
  66. function TPAConvexity(TPA:TPointArray): Extended;
  67. var
  68.   hullArea, ContArea:Integer;
  69.   contour,hull:TPointArray;
  70. begin
  71.   Result := 1.0;
  72.   contour := XT_TPAOutline(TPA);
  73.   hull := XT_ConnectTPA(XT_ConvexHull(TPA));
  74.  
  75.   if (length(hull) = 0) or (length(contour) = 0) then Exit;
  76.   ContArea := PolygonArea(contour);  
  77.   HullArea := PolygonArea(hull);
  78.   if HullArea = 0 then Exit;
  79.   Result := (ContArea / HullArea);
  80. end;
  81.  
  82.  
  83.  
  84. function ATPAFilterConvexity(const ATPA:T2DPointArray; MinCirc:Extended):T2DPointArray;
  85. var
  86.   I,j: Integer;
  87. begin
  88.   j:=0;
  89.   SetLength(Result, Length(ATPA));
  90.   for i:=0 to High(ATPA) do
  91.   begin
  92.     if (TPAConvexity(ATPA[i]) >= MinCirc) then
  93.     begin  
  94.       Result[j] := ATPA[i]
  95.       Inc(j);
  96.     end;
  97.   end;
  98.   SetLength(Result, j);
  99. end;
  100.  
  101.  
  102.  
  103. var
  104.   i: Integer;
  105.   TPA:TPointArray;
  106.   ATPA,ATPA2:T2DPointArray;
  107.   bmp:TSCARBitmap;
  108. begin
  109.   bmp := TSCARBitmap.Create('');
  110.   bmp.loadFromBmp('C:\SCAR Divi\tests\test.bmp');
  111.   TPA := GetBitmapColorTPA(bmp, 0);
  112.   ATPA := XT_ClusterTPA(TPA, 1, True);    
  113.  
  114.   ATPA2 := ATPAFilterConvexity(ATPA, 0.9);
  115.   for i:=0 to high(ATPA2) do
  116.     bmp.setpixels(ATPA2[I], 255);
  117.   DebugBitmap(bmp);
  118. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement