Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- {$DEFINE E_HIDE_ALL}
- {$I SimbaExt_beta/SimbaExt.simba}
- type
- TMatchData = record
- pattern: TPointArray;
- similarity: Double;
- end;
- TMatchArr = Array of TMatchData;
- procedure SortMatchArr(var arr:TMatchArr; left, right:Int32);
- var
- i, j:Integer;
- tmp:TMatchData;
- begin
- for i := left+1 to right do begin
- j := i-1;
- tmp := arr[i];
- while (j >= left) and (arr[j].similarity > tmp.similarity) do begin
- arr[j+1] := arr[j];
- j:=j-1;
- end;
- arr[j+1] := Tmp;
- end;
- end;
- function SqDist(p,q:TPoint): Int32;
- begin
- Result := Sqr(p.x-q.x) + Sqr(p.x-q.x);
- end;
- function BlobDist(A,B:TPointArray): Double;
- var
- tree:TSlackTree;
- node:PSlackTree;
- i:Int32;
- ab,ba:Double;
- begin
- tree.Init(A);
- for i:=0 to High(B) do
- begin
- node := tree.Nearest_N(B[i],False);
- ab += SqDist(TSlackTree(node^).split, B[i]);
- end;
- ab := Sqrt(ab / Length(B));
- tree.Init(B);
- for i:=0 to High(A) do
- begin
- node := tree.Nearest_N(A[i],False);
- ba += SqDist(TSlackTree(node^).split, A[i]);
- end;
- ba := Sqrt(ba / Length(A));
- Result := MaxE(ab,ba);
- end;
- function kNearest(shapes:T2DPointArray; query:TPointArray; k:Int32): TMatchArr;
- var
- temp:TMatchArr;
- curr,test:TPointArray;
- B:TBox;
- i:Int32;
- m:TPoint;
- begin
- query := copy(query);
- m := query.Mean();
- OffsetTPA(query, Point(-m.x,-m.y));
- SetLength(Result, Length(shapes));
- for i:=0 to High(Shapes) do
- begin
- curr := Copy( Shapes[i] );
- m := curr.Mean();
- curr.Offset( -m.x,-m.y );
- Result[i].similarity := BlobDist(curr, query);
- Result[i].pattern := Shapes[i];
- end;
- SortMatchArr(Result,0,high(Result));
- SetLength(Result, k);
- end;
- //call me to crash Lape.
- function CharSegmentation(TPA:TPointArray): T2DPointArray;
- begin
- Result := ClusterTPA(TPA,5);
- end;
- var
- TPA,textTPA:TPointArray;
- ATPA:T2DPointArray;
- BMP: TRafBitmap;
- Res: TMatchArr;
- i,_:Int32;
- offset:TPoint;
- begin
- FindColorsTolerance(TPA,0, 30,20,750,300, 10);
- ATPA := CharSegmentation(TPA); ///REPLACE WITH ClusterTPA(TPA,5) AND LAPE DON'T CRASH
- TPA := ATPA[Random(0,High(ATPA))];
- Res := kNearest(ATPA, TPA, Length(ATPA));
- BMP.Create(750,300, $FFFFFF);
- for i:=0 to High(ATPA) do BMP.SetPixels(ATPA[i], 0);
- BMP.SetPixels(TPA, 255);
- for i:=0 to 6 do
- begin
- textTPA := TPAFromText(
- Replace( ToString(Round(Res[i].similarity, 1)), ',','.',[] ),
- 'StatChars', _,_
- );
- offset.x := res[i].pattern[0].x-6;
- offset.y := res[i].pattern[0].y-10;
- OffsetTPA(textTPA, offset);
- BMP.SetPixels(textTPA, $555555);
- end;
- BMP.Debug();
- BMP.Free();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement