Advertisement
WarPie90

CCOEFF_NORMED

Jun 3rd, 2022
1,774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.62 KB | None | 0 0
  1. program new;
  2.  
  3. function TColor.R: Byte; constref;
  4. begin
  5.   Result := Self and $FF;
  6. end;
  7.  
  8. function TColor.G: Byte; constref;
  9. begin
  10.   Result := Self shr 8 and $FF;
  11. end;
  12.  
  13. function TColor.B: Byte; constref;
  14. begin
  15.   Result := Self shr 16 and $FF;
  16. end;
  17.  
  18. function CompareImages(I, T: TMufasaBitmap; Normed: Boolean): Single;
  19. var
  20.   invsize, sigmaR,sigmaG,sigmaB, denom,isum,tsum: Single;
  21.   x,y: Int32;
  22.   tcR, tcG, tcB, icR, icG, icB: TSingleMatrix;
  23.   R: Byte;
  24. begin
  25.   invsize := 1 / (T.GetWidth() * T.GetHeight());
  26.  
  27.   // compute T' for template
  28.   tcR.SetSize(T.GetWidth(), T.GetHeight());
  29.   tcG.SetSize(T.GetWidth(), T.GetHeight());
  30.   tcB.SetSize(T.GetWidth(), T.GetHeight());
  31.  
  32.   sigmaR := 0;
  33.   sigmaG := 0;
  34.   sigmaB := 0;
  35.  
  36.   for x:=0 to T.GetWidth() - 1 do
  37.     for y:=0 to T.GetHeight() - 1 do
  38.     begin
  39.       sigmaR += T.GetPixel(x,y).R;
  40.       sigmaG += T.GetPixel(x,y).G;
  41.       sigmaB += T.GetPixel(x,y).B;
  42.     end;
  43.  
  44.   for x:=0 to T.GetWidth() - 1 do
  45.     for y:=0 to T.GetHeight() - 1 do
  46.     begin
  47.       tcR[y,x] := T.GetPixel(x,y).R - invsize * sigmaR;
  48.       tcG[y,x] := T.GetPixel(x,y).G - invsize * sigmaG;
  49.       tcB[y,x] := T.GetPixel(x,y).B - invsize * sigmaB;
  50.     end;
  51.  
  52.   // compute I' for image
  53.   icR.SetSize(I.GetWidth(), I.GetHeight());
  54.   icG.SetSize(I.GetWidth(), I.GetHeight());
  55.   icB.SetSize(I.GetWidth(), I.GetHeight());
  56.  
  57.   sigmaR := 0;
  58.   sigmaG := 0;
  59.   sigmaB := 0;
  60.  
  61.   for x:=0 to I.GetWidth() - 1 do
  62.     for y:=0 to I.GetHeight() - 1 do
  63.     begin
  64.       sigmaR += I.GetPixel(x,y).R;
  65.       sigmaG += I.GetPixel(x,y).G;
  66.       sigmaB += I.GetPixel(x,y).B;
  67.     end;
  68.  
  69.   for x:=0 to I.GetWidth() - 1 do
  70.     for y:=0 to I.GetHeight() - 1 do
  71.     begin
  72.       icR[y,x] := I.GetPixel(x,y).R - invsize * sigmaR;
  73.       icG[y,x] := I.GetPixel(x,y).G - invsize * sigmaG;
  74.       icB[y,x] := I.GetPixel(x,y).B - invsize * sigmaB;
  75.     end;
  76.  
  77.   // ccoeff
  78.   Result := 0;
  79.   for x:=0 to I.GetWidth() - 1 do
  80.     for y:=0 to I.GetHeight() - 1 do
  81.       Result += ((icR[y,x] * tcR[y,x]) + (icG[y,x] * tcG[y,x]) + (icB[y,x] * tcB[y,x]));
  82.  
  83.   if Normed then
  84.   begin
  85.     isum := tsum := 0;
  86.     for x:=0 to I.GetWidth() - 1 do
  87.       for y:=0 to I.GetHeight() - 1 do
  88.       begin
  89.         isum += Sqr(icR[y,x]) + Sqr(icG[y,x]) + Sqr(icB[y,x]);
  90.         tsum += Sqr(tcR[y,x]) + Sqr(tcG[y,x]) + Sqr(tcB[y,x]);
  91.       end;
  92.     Result := Result / Sqrt(isum * tsum);
  93.   end;
  94. end;
  95.  
  96.  
  97. var
  98.   T,I: TMufasaBitmap;
  99. begin
  100.   T.Init(nil);
  101.   T.LoadFromFile('test.png');
  102.  
  103.   I.Init(nil);
  104.   I.LoadFromFile('test.png');
  105.   I.Blur(3);
  106.  
  107.   WriteLn CompareImages(I, T, True);
  108.  
  109.   WriteLn I.MatchTemplate(T, TM_CCOEFF_NORMED)[0][0];
  110. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement