Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- function TColor.R: Byte; constref;
- begin
- Result := Self and $FF;
- end;
- function TColor.G: Byte; constref;
- begin
- Result := Self shr 8 and $FF;
- end;
- function TColor.B: Byte; constref;
- begin
- Result := Self shr 16 and $FF;
- end;
- function CompareImages(I, T: TMufasaBitmap; Normed: Boolean): Single;
- var
- invsize, sigmaR,sigmaG,sigmaB, denom,isum,tsum: Single;
- x,y: Int32;
- tcR, tcG, tcB, icR, icG, icB: TSingleMatrix;
- R: Byte;
- begin
- invsize := 1 / (T.GetWidth() * T.GetHeight());
- // compute T' for template
- tcR.SetSize(T.GetWidth(), T.GetHeight());
- tcG.SetSize(T.GetWidth(), T.GetHeight());
- tcB.SetSize(T.GetWidth(), T.GetHeight());
- sigmaR := 0;
- sigmaG := 0;
- sigmaB := 0;
- for x:=0 to T.GetWidth() - 1 do
- for y:=0 to T.GetHeight() - 1 do
- begin
- sigmaR += T.GetPixel(x,y).R;
- sigmaG += T.GetPixel(x,y).G;
- sigmaB += T.GetPixel(x,y).B;
- end;
- for x:=0 to T.GetWidth() - 1 do
- for y:=0 to T.GetHeight() - 1 do
- begin
- tcR[y,x] := T.GetPixel(x,y).R - invsize * sigmaR;
- tcG[y,x] := T.GetPixel(x,y).G - invsize * sigmaG;
- tcB[y,x] := T.GetPixel(x,y).B - invsize * sigmaB;
- end;
- // compute I' for image
- icR.SetSize(I.GetWidth(), I.GetHeight());
- icG.SetSize(I.GetWidth(), I.GetHeight());
- icB.SetSize(I.GetWidth(), I.GetHeight());
- sigmaR := 0;
- sigmaG := 0;
- sigmaB := 0;
- for x:=0 to I.GetWidth() - 1 do
- for y:=0 to I.GetHeight() - 1 do
- begin
- sigmaR += I.GetPixel(x,y).R;
- sigmaG += I.GetPixel(x,y).G;
- sigmaB += I.GetPixel(x,y).B;
- end;
- for x:=0 to I.GetWidth() - 1 do
- for y:=0 to I.GetHeight() - 1 do
- begin
- icR[y,x] := I.GetPixel(x,y).R - invsize * sigmaR;
- icG[y,x] := I.GetPixel(x,y).G - invsize * sigmaG;
- icB[y,x] := I.GetPixel(x,y).B - invsize * sigmaB;
- end;
- // ccoeff
- Result := 0;
- for x:=0 to I.GetWidth() - 1 do
- for y:=0 to I.GetHeight() - 1 do
- Result += ((icR[y,x] * tcR[y,x]) + (icG[y,x] * tcG[y,x]) + (icB[y,x] * tcB[y,x]));
- if Normed then
- begin
- isum := tsum := 0;
- for x:=0 to I.GetWidth() - 1 do
- for y:=0 to I.GetHeight() - 1 do
- begin
- isum += Sqr(icR[y,x]) + Sqr(icG[y,x]) + Sqr(icB[y,x]);
- tsum += Sqr(tcR[y,x]) + Sqr(tcG[y,x]) + Sqr(tcB[y,x]);
- end;
- Result := Result / Sqrt(isum * tsum);
- end;
- end;
- var
- T,I: TMufasaBitmap;
- begin
- T.Init(nil);
- T.LoadFromFile('test.png');
- I.Init(nil);
- I.LoadFromFile('test.png');
- I.Blur(3);
- WriteLn CompareImages(I, T, True);
- WriteLn I.MatchTemplate(T, TM_CCOEFF_NORMED)[0][0];
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement