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}
- function MatchTemplate(im,sub:TIntMatrix): TFloatMatrix;
- type
- TRGBMatrix = Array of Array of TRGB32;
- var
- ImW,ImH,SubW,SubH,area:Int32;
- x,y,xx,yy,iw,ih:Int32;
- cc:Int64;
- pix1,pix2:TRGB32;
- i1,i2:TRGBMatrix;
- const
- MAX_SQRGB:Double = (255*255*3.0);
- begin
- ImW := Length(Im[0]);
- ImH := Length(Im);
- SubW := Length(Sub[0]);
- SubH := Length(Sub);
- SetLength(Result, ImH-SubH, ImW-SubW);
- ih := subh-1;
- iw := subw-1;
- area := subW * subH;
- i1 := TRGBMatrix(im);
- i2 := TRGBMatrix(sub);
- for y:=0 to ImH-SubH-1 do
- for x:=0 to ImW-SubW-1 do
- begin
- cc := 0;
- for yy:=0 to ih do
- for xx:=0 to iw do
- Inc(cc, Sqr(i1[y+yy,x+xx].B - i2[yy,xx].B) +
- Sqr(i1[y+yy,x+xx].G - i2[yy,xx].G) +
- Sqr(i1[y+yy,x+xx].R - i2[yy,xx].R) );
- Result[y,x] := 1 - (cc / area / MAX_SQRGB); //Result range in 0..1
- end;
- end;
- procedure Debug(Mat:TFloatMatrix);
- var
- Debug:TRafBitmap;
- Data:TIntMatrix;
- x,y:Int32;
- H,S,L:Extended;
- begin
- Mat := Mat.Normalize(0,100);
- SetLength(Data,Length(Mat), Length(Mat[0]));
- for y:=0 to High(Mat) do
- for x:=0 to High(Mat[0]) do
- begin
- //ColorToHSL(Mat[y,x],H,S,L);
- H := 58;
- S := Mat[y,x];
- L := Mat[y,x];
- Data[y,x] := HSLToColor(H,S,L);
- end;
- Debug.FromMatrix( Data );
- Debug.Debug();
- Debug.Free();
- end;
- var
- BMP,SUB:TRafBitmap;
- Mat:TFloatMatrix;
- m1,m2:TIntMatrix;
- t:Double;
- begin
- SUB.Open('rs_cave_sub.png');
- BMP.Open('rs_cave.png');
- m1 := BMP.ToMatrix();
- m2 := Sub.ToMatrix();
- t := se.MarkTime();
- mat := MatchTemplate(m1, m2);
- WriteLn(se.MarkTime() - t);
- Debug( mat );
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement