Advertisement
WarPie90

ImSplitShapes

Aug 5th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.36 KB | None | 0 0
  1. program new;
  2. {$DEFINE ERR_HIDE_ALL}
  3. {$I SimbaExt/SimbaExt.simba}
  4.  
  5. function ImSplitShapes(Im:TIntMatrix; Tol:Int32): T2DPointArray;
  6. var
  7.   i,j,w,h,hi,trace,color:Int32;
  8.   adj,tpa,queue:TPointArray;
  9.   pt:TPoint;
  10. begin
  11.   W := im.Width();
  12.   H := im.Height();
  13.   SetLength(Adj,8);
  14.   TPA := TPAFromBox([0,0,w-1,h-1]);
  15.   hi := -1;
  16.   for i:=0 to High(TPA) do
  17.   begin
  18.     pt := TPA[i];
  19.     Trace := im[pt.y,pt.x];
  20.     if Trace = -1 then Continue;
  21.     inc(hi);
  22.     SetLength(Result, hi+1);
  23.     Queue.Append(pt);
  24.     while not(Queue.IsEmpty) do
  25.     begin
  26.       se.GetAdjacent(Adj, Queue.Pop() ,True);
  27.       for j:=0 to 7 do
  28.         if InRange(adj[j].y,0,H-1) and InRange(adj[j].x,0,W-1) then
  29.         begin
  30.           color := Im[adj[j].y, adj[j].x];
  31.           if (color <> -1) and (SimilarColors(color, trace, tol)) then
  32.           begin
  33.             Queue.Append(adj[j]);
  34.             Result[hi].Append(adj[j]);
  35.             Im[adj[j].y, adj[j].x] := -1;
  36.           end;
  37.         end;
  38.     end;
  39.   end;
  40. end;
  41.  
  42. var
  43.   Im:TRafBitmap;
  44.   ATPA:T2DPointArray;
  45.   i:Int32;
  46. begin
  47.   Im.Open('tests/c__.jpg');
  48.   Im.SetSize(Im.Width*2, Im.Height);
  49.   ATPA := ImSplitShapes(Im.ToMatrix(), 60);
  50.  
  51.   for i:=0 to High(ATPA) do
  52.   begin
  53.     ATPA[i].Offset(Im.Width div 2,0);
  54.     Im.SetPixels(ATPA[i], Random($FFFFFF));
  55.   end;
  56.   Im.Debug();
  57.   Im.Save('tmp.png');
  58.   Im.Free();
  59.  
  60. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement