forked from swildeman/dicflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swcorr2.m
43 lines (35 loc) · 1.31 KB
/
swcorr2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function xc = swcorr2( t, sw )
%SWCORR2 fft-based, page-wise, cross corelation between a set of
%templates and search windows, with size(sw) > size(t)
%
% SYNOPSIS: xc = swcorr2( t, sw )
%
% INPUT t: set of N square templates of size m*m (as (m*m*N) matrix)
% sw: set of N square search windows of size k*k (k > m)
% (as (k*k*N) matrix)
%
% OUTPUT xc: cross correlations between templates and search windows
% as (k-m)*(k-m)*N matrix (only valid points are returned)
%
% Copyright (c) 2017 Sander Wildeman
% Distributed under the MIT License, see LICENSE file
[tRows, tCols, ~] = size(t);
[swRows, swCols, ~] = size(sw);
maxRowDisp = swRows - tRows;
maxColDisp = swCols - tCols;
if any(mod([maxRowDisp, maxColDisp],2)) || any([maxRowDisp, maxColDisp]<0)
error('Search windows are expected to have symmetric (positive) padding around the templates in both dimensions');
else
maxRowDisp = maxRowDisp/2;
maxColDisp = maxColDisp/2;
end
f_sw = fft2(sw);
f_t = fft2(padarray(t,[maxRowDisp,maxColDisp]));
xc = fftshift(fftshift( ifft2( bsxfun(@times, f_sw, conj(f_t)) ), 1), 2);
% return only valid points
clpRows = (tRows-1)/2;
clpCols = (tCols-1)/2;
validRows = 1+ceil(clpRows) : swRows-floor(clpRows);
validCols = 1+ceil(clpCols) : swCols-floor(clpCols);
xc = real(xc(validRows, validCols,:));
end