Skip to content

Commit

Permalink
CCORR and SQDIFF support for MatchTemplateMask
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jul 24, 2022
1 parent 331190f commit eb4b077
Show file tree
Hide file tree
Showing 19 changed files with 1,463 additions and 710 deletions.
2 changes: 1 addition & 1 deletion PACKAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ Example: www.testversions.com
"name" : "versionname"
}
]
```
```
22 changes: 21 additions & 1 deletion Source/Simba.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
<PackageName Value="LCL"/>
</Item4>
</RequiredPackages>
<Units Count="94">
<Units Count="99">
<Unit0>
<Filename Value="Simba.lpr"/>
<IsPartOfProject Value="True"/>
Expand Down Expand Up @@ -704,6 +704,26 @@
<Filename Value="simba.mouselogger.pas"/>
<IsPartOfProject Value="True"/>
</Unit93>
<Unit94>
<Filename Value="matchtemplate/simba.matchtemplate_ccorr.pas"/>
<IsPartOfProject Value="True"/>
</Unit94>
<Unit95>
<Filename Value="matchtemplate/simba.matchtemplate_helpers.pas"/>
<IsPartOfProject Value="True"/>
</Unit95>
<Unit96>
<Filename Value="matchtemplate/simba.matchtemplate_sqdiff.pas"/>
<IsPartOfProject Value="True"/>
</Unit96>
<Unit97>
<Filename Value="matchtemplate/simba.matchtemplate_ccoeff.pas"/>
<IsPartOfProject Value="True"/>
</Unit97>
<Unit98>
<Filename Value="simba.areapicker.pas"/>
<IsPartOfProject Value="True"/>
</Unit98>
</Units>
</ProjectOptions>
<CompilerOptions>
Expand Down
14 changes: 11 additions & 3 deletions Source/generics/simba.generics_matrix.pas
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,32 @@ implementation
var
X, Y, Width, Height: Integer;
Value: _T;
HasValue: Boolean;
begin
MinValue := Default(_T);
MaxValue := Default(_T);
HasValue := False;

if specialize MatrixSize<_T>(Matrix, Width, Height) then
begin
Width -= 1;
Height -= 1;

MinValue := Matrix[0,0];
MaxValue := Matrix[0,0];

for Y := 0 to Height do
for X := 0 to Width do
begin
Value := Matrix[Y, X];

if IsNumber(Value) then
begin
if (not HasValue) then
begin
HasValue := True;

MinValue := Value;
MaxValue := Value;
end;

if (Value > MaxValue) then MaxValue := Value;
if (Value < MinValue) then MinValue := Value;
end;
Expand Down
35 changes: 2 additions & 33 deletions Source/helpers/simba.helpers_matrix.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

{$i simba.inc}

{$MODESWITCH ARRAYOPERATORS OFF}

interface

uses
Expand Down Expand Up @@ -70,14 +72,6 @@ interface
procedure MeanStdev(out MeanValue, Stdev: Double);
end;

TComplexMatrixHelper = type helper for TComplexMatrix
function Width: Integer;
function Height: Integer;
function Area: Integer;
function Size(out AWidth, AHeight: Integer): Boolean;
procedure SetSize(const AWidth, AHeight: Integer);
end;

TByteMatrixHelper = type helper for TByteMatrix
function Width: Integer;
function Height: Integer;
Expand Down Expand Up @@ -184,31 +178,6 @@ procedure TDoubleMatrixHelper.MeanStdev(out MeanValue, Stdev: Double);
specialize MatrixMeanStdev<Double>(Self, MeanValue, Stdev);
end;

function TComplexMatrixHelper.Width: Integer;
begin
Result := specialize MatrixWidth<TComplex>(Self);
end;

function TComplexMatrixHelper.Height: Integer;
begin
Result := specialize MatrixHeight<TComplex>(Self);
end;

function TComplexMatrixHelper.Area: Integer;
begin
Result := specialize MatrixArea<TComplex>(Self);
end;

function TComplexMatrixHelper.Size(out AWidth, AHeight: Integer): Boolean;
begin
Result := specialize MatrixSize<TComplex>(Self, AWidth, AHeight);
end;

procedure TComplexMatrixHelper.SetSize(const AWidth, AHeight: Integer);
begin
specialize MatrixSetSize<TComplex>(Self, AWidth, AHeight);
end;

function TIntegerMatrixHelper.Width: Integer;
begin
Result := specialize MatrixWidth<Integer>(Self);
Expand Down
92 changes: 39 additions & 53 deletions Source/matchtemplate/simba.fftpack4.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@
interface

uses
sysutils,
simba.fftpack4_core, simba.mufasatypes,
simba.helpers_matrix;
sysutils, math,
simba.math, simba.mufasatypes, simba.matchtemplate_matrix;

type
TFFTPACK = record
function OptimalDFTSize(target: Int32): Int32;
function OptimalDFTSize(const target: Integer): Integer;

function InitFFT(n: Int32): TComplexArray;
function FFT(a, wsave: TComplexArray; Inplace: Boolean=False): TComplexArray;
function IFFT(a, wsave: TComplexArray; Inplace: Boolean=False): TComplexArray;
function InitFFT(const n: Integer): TComplexArray;
function FFT(const a, wsave: TComplexArray; const Inplace: Boolean = False): TComplexArray;
function IFFT(const a, wsave: TComplexArray; const Inplace: Boolean = False): TComplexArray;

function InitRFFT(n: Int32): TSingleArray;
function RFFT(a, wsave: TSingleArray; Inplace: Boolean=False): TSingleArray;
function IRFFT(a, wsave: TSingleArray; Inplace: Boolean=False): TSingleArray;
function InitRFFT(const n: Integer): TSingleArray;
function RFFT(const a, wsave: TSingleArray; const Inplace: Boolean = False): TSingleArray;
function IRFFT(const a, wsave: TSingleArray; const Inplace: Boolean = False): TSingleArray;

function FFT2(m: TComplexMatrix): TComplexMatrix;
function IFFT2(m: TComplexMatrix): TComplexMatrix;
function FFT2(const m: TComplexMatrix): TComplexMatrix;
function IFFT2(const m: TComplexMatrix): TComplexMatrix;
end;

var
Expand All @@ -47,8 +46,7 @@ TFFTPACK = record
implementation

uses
math,
simba.matchtemplate_matrix, simba.math;
simba.fftpack4_core;

const
__OptimalDFT: array[0..168] of Integer = (
Expand All @@ -74,7 +72,7 @@ implementation
// --------------------------------------------------------------------------------
// Compute the optimal size for FFT

function TFFTPACK.OptimalDFTSize(target: Integer): Integer;
function TFFTPACK.OptimalDFTSize(const target: Integer): Integer;
var
n,match,quotient,p2,p5,p35: Integer;
begin
Expand Down Expand Up @@ -121,13 +119,13 @@ function TFFTPACK.OptimalDFTSize(target: Integer): Integer;
// --------------------------------------------------------------------------------
// complex 2 complex FFT

function TFFTPACK.InitFFT(n: Integer): TComplexArray;
function TFFTPACK.InitFFT(const n: Integer): TComplexArray;
begin
SetLength(Result, CPLX_BUFFSZ);
cffti(n, @Result[0]);
end;

function TFFTPACK.FFT(a, wsave: TComplexArray; Inplace:Boolean=False): TComplexArray;
function TFFTPACK.FFT(const a, wsave: TComplexArray; const Inplace: Boolean): TComplexArray;
var n: Integer;
begin
if Inplace then Result := a
Expand All @@ -137,7 +135,7 @@ function TFFTPACK.FFT(a, wsave: TComplexArray; Inplace:Boolean=False): TComplexA
cfftf(n, @Result[0], @wsave[0]);
end;

function TFFTPACK.IFFT(a, wsave: TComplexArray; Inplace:Boolean=False): TComplexArray;
function TFFTPACK.IFFT(const a, wsave: TComplexArray; const Inplace: Boolean): TComplexArray;
var
n: Integer;
f: Single;
Expand All @@ -159,13 +157,13 @@ function TFFTPACK.IFFT(a, wsave: TComplexArray; Inplace:Boolean=False): TComplex
// --------------------------------------------------------------------------------
// real 2 real FFT

function TFFTPACK.InitRFFT(n: Integer): TSingleArray;
function TFFTPACK.InitRFFT(const n: Integer): TSingleArray;
begin
SetLength(Result, REAL_BUFFSZ);
rffti(n, @Result[0]);
end;

function TFFTPACK.RFFT(a, wsave: TSingleArray; Inplace:Boolean=False): TSingleArray;
function TFFTPACK.RFFT(const a, wsave: TSingleArray; const Inplace: Boolean): TSingleArray;
var n: Integer;
begin
if Inplace then Result := a
Expand All @@ -175,7 +173,7 @@ function TFFTPACK.RFFT(a, wsave: TSingleArray; Inplace:Boolean=False): TSingleAr
rfftf(n, @Result[0], @wsave[0]);
end;

function TFFTPACK.IRFFT(a, wsave: TSingleArray; Inplace:Boolean=False): TSingleArray;
function TFFTPACK.IRFFT(const a, wsave: TSingleArray; const Inplace: Boolean): TSingleArray;
var
n: Integer;
f: Single;
Expand All @@ -193,52 +191,40 @@ function TFFTPACK.IRFFT(a, wsave: TSingleArray; Inplace:Boolean=False): TSingleA
// --------------------------------------------------------------------------------
// 2d complex fft

function TFFTPACK.FFT2(m: TComplexMatrix): TComplexMatrix;
function TFFTPACK.FFT2(const m: TComplexMatrix): TComplexMatrix;
var
W,H,Y: Integer;
Y: Integer;
plan: TComplexArray;
rot: TComplexMatrix;
begin
W := M.Width;
H := M.Height;

plan := InitFFT(W);
for Y := 0 to H - 1 do
plan := InitFFT(m.Width);
for Y := 0 to m.Height - 1 do
FFTPACK.FFT(m[Y], Plan, True);

m := Rot90(m);

W := M.Width;
H := M.Height;

plan := InitFFT(W);
for Y := 0 to H - 1 do
FFTPACK.FFT(m[Y], Plan, True);
rot := Rot90(m);
plan := InitFFT(rot.Width);
for Y := 0 to rot.Height - 1 do
FFTPACK.FFT(rot[Y], Plan, True);

Result := Rot90(m);
Result := Rot90(rot);
end;

function TFFTPACK.IFFT2(m: TComplexMatrix): TComplexMatrix;
function TFFTPACK.IFFT2(const m: TComplexMatrix): TComplexMatrix;
var
W,H,Y: Integer;
Y: Integer;
plan: TComplexArray;
rot: TComplexMatrix;
begin
W := M.Width;
H := M.Height;

plan := InitFFT(W);
for Y := 0 to H - 1 do
plan := InitFFT(m.Width);
for Y := 0 to m.Height - 1 do
FFTPACK.IFFT(m[Y], Plan, True);

m := Rot90(m);

W := M.Width;
H := M.Height;

plan := InitFFT(W);
for Y := 0 to H - 1 do
FFTPACK.IFFT(m[Y], Plan, True);
rot := Rot90(m);
plan := InitFFT(rot.Width);
for Y := 0 to rot.Height - 1 do
FFTPACK.IFFT(rot[Y], Plan, True);

Result := Rot90(m);
Result := Rot90(rot);
end;

end.
Loading

0 comments on commit eb4b077

Please sign in to comment.