-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormxcorr2.m
284 lines (226 loc) · 7.43 KB
/
normxcorr2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
function C = normxcorr2(varargin)
%NORMXCORR2 Normalized two-dimensional cross-correlation.
% C = NORMXCORR2(TEMPLATE,A) computes the normalized cross-correlation of
% matrices TEMPLATE and A. The matrix A must be larger than the matrix
% TEMPLATE for the normalization to be meaningful. The values of TEMPLATE
% cannot all be the same. The resulting matrix C contains correlation
% coefficients and its values may range from -1.0 to 1.0.
%
% Class Support
% -------------
% The input matrices can be numeric. The output matrix C is double.
%
% Remarks
% -------
% Normalized cross correlation is an undefined operation in regions where
% A has zero variance over the full extent of the template. In these
% regions, we assign correlation coefficients of zero to the output C.
%
% Example
% -------
%
% % Load images
% onion = rgb2gray(imread('onion.png'));
% peppers = rgb2gray(imread('peppers.png'));
% imshowpair(peppers,onion,'montage')
%
% c = normxcorr2(onion,peppers);
% figure, surf(c), shading flat
%
% [ypeak, xpeak] = find(c==max(c(:)));
% % Compute translation from max location in correlation matrix
% yoffSet = ypeak-size(onion,1);
% xoffSet = xpeak-size(onion,2);
%
% % Display matched area
% figure
% hAx = axes;
% imshow(peppers,'Parent', hAx);
% imrect(hAx, [xoffSet+1, yoffSet+1, size(onion,2), size(onion,1)]);
%
% See also CORRCOEF.
% Copyright 1993-2015 The MathWorks, Inc.
% Input-output specs
% ------------------
% T: 2-D, real, full matrix
% logical, uint8, uint16, or double
% no NaNs, no Infs
% prod(size(T)) >= 2
% std(T(:))~=0
%
% A: 2-D, real, full matrix
% logical, uint8, uint16, or double
% no NaNs, no Infs
% size(A,1) >= size(T,1)
% size(A,2) >= size(T,2)
%
% C: double
[T, A] = ParseInputs(varargin{:});
% We normalize the cross correlation to get correlation coefficients using the
% definition of Haralick and Shapiro, Volume II (p. 317), generalized to
% two-dimensions.
%
% Lewis explicitly defines the normalized cross-correlation in two-dimensions
% in this paper (equation 2):
%
% "Fast Normalized Cross-Correlation", by J. P. Lewis, Industrial Light & Magic.
%
% Our technical reference document on NORMXCORR2 shows how to get from
% equation 2 of the Lewis paper to the code below.
xcorr_TA = xcorr2_fast(T,A);
[m, n] = size(T);
mn = m*n;
local_sum_A = local_sum(A,m,n);
local_sum_A2 = local_sum(A.*A,m,n);
% Note: diff_local_sums should be nonnegative, but may have negative
% values due to round off errors. Below, we use max to ensure the
% radicand is nonnegative.
diff_local_sums = ( local_sum_A2 - (local_sum_A.^2)/mn );
denom_A = sqrt( max(diff_local_sums,0) );
denom_T = sqrt(mn-1)*std(T(:));
denom = denom_T*denom_A;
numerator = (xcorr_TA - local_sum_A*sum(T(:))/mn );
% We know denom_T~=0 from input parsing;
% so denom is only zero where denom_A is zero, and in
% these locations, C is also zero.
C = zeros(size(numerator));
tol = sqrt( eps( max(abs(denom(:)))) );
i_nonzero = find(denom > tol);
C(i_nonzero) = numerator(i_nonzero) ./ denom(i_nonzero);
% Another numerics backstop. If any of the coefficients are outside the
% range [-1 1], the numerics are unstable to small variance in A or T. In
% these cases, set C to zero to reflect undefined 0/0 condition.
C( ( abs(C) - 1 ) > sqrt(eps(1)) ) = 0;
%-------------------------------
% Function local_sum
%
function local_sum_A = local_sum(A,m,n)
% We thank Eli Horn for providing this code, used with his permission,
% to speed up the calculation of local sums. The algorithm depends on
% precomputing running sums as described in "Fast Normalized
% Cross-Correlation", by J. P. Lewis, Industrial Light & Magic.
B = padarray(A,[m n]);
s = cumsum(B,1);
c = s(1+m:end-1,:)-s(1:end-m-1,:);
s = cumsum(c,2);
local_sum_A = s(:,1+n:end-1)-s(:,1:end-n-1);
%-------------------------------
% Function xcorr2_fast
%
function cross_corr = xcorr2_fast(T,A)
T_size = size(T);
A_size = size(A);
outsize = A_size + T_size - 1;
% figure out when to use spatial domain vs. freq domain
conv_time = time_conv2(T_size,A_size); % 1 conv2
fft_time = 3*time_fft2(outsize); % 2 fft2 + 1 ifft2
if (conv_time < fft_time)
cross_corr = conv2(rot90(T,2),A);
else
cross_corr = freqxcorr(T,A,outsize);
end
%-------------------------------
% Function freqxcorr
%
function xcorr_ab = freqxcorr(a,b,outsize)
% calculate correlation in frequency domain
Fa = fft2(rot90(a,2),outsize(1),outsize(2));
Fb = fft2(b,outsize(1),outsize(2));
xcorr_ab = ifft2(Fa .* Fb,'symmetric');
%-------------------------------
% Function time_conv2
%
function time = time_conv2(obssize,refsize)
% time a spatial domain convolution for 10-by-10 x 20-by-20 matrices
% a = ones(10);
% b = ones(20);
% mintime = 0.1;
% t1 = cputime;
% t2 = t1;
% k = 0;
% while (t2-t1)<mintime
% c = conv2(a,b);
% k = k + 1;
% t2 = cputime;
% end
% t_total = (t2-t1)/k;
% % convolution time = K*prod(size(a))*prod(size(b))
% % t_total = K*10*10*20*20 = 40000*K
% K = t_total/40000;
% K was empirically calculated by the commented-out code above.
K = 2.7e-8;
% convolution time = K*prod(obssize)*prod(refsize)
time = K*prod(obssize)*prod(refsize);
%-------------------------------
% Function time_fft2
%
function time = time_fft2(outsize)
% time a frequency domain convolution by timing two one-dimensional ffts
R = outsize(1);
S = outsize(2);
% Tr = time_fft(R);
% K_fft = Tr/(R*log(R));
% K_fft was empirically calculated by the 2 commented-out lines above.
K_fft = 3.3e-7;
Tr = K_fft*R*log(R);
if S==R
Ts = Tr;
else
% Ts = time_fft(S); % uncomment to estimate explicitly
Ts = K_fft*S*log(S);
end
time = S*Tr + R*Ts;
% %-------------------------------
% % Function time_fft
% %
% function T = time_fft(M)
% % time a complex fft that is M elements long
% vec = complex(ones(M,1),ones(M,1));
% mintime = 0.1;
% t1 = cputime;
% t2 = t1;
% k = 0;
% while (t2-t1) < mintime
% dummy = fft(vec);
% k = k + 1;
% t2 = cputime;
% end
% T = (t2-t1)/k;
%-----------------------------------------------------------------------------
function [T, A] = ParseInputs(varargin)
narginchk(2,2)
T = varargin{1};
A = varargin{2};
validateattributes(T,{'logical','numeric'},{'real','nonsparse','2d','finite'},mfilename,'T',1)
validateattributes(A,{'logical','numeric'},{'real','nonsparse','2d','finite'},mfilename,'A',2)
checkSizesTandA(T,A)
% See geck 342320. If either A or T has a minimum value which is negative, we
% need to shift the array so all values are positive to ensure numerically
% robust results for the normalized cross-correlation.
A = shiftData(A);
T = shiftData(T);
%checkIfFlat(T);
%-----------------------------------------------------------------------------
function B = shiftData(A)
B = double(A);
is_unsigned = isa(A,'uint8') || isa(A,'uint16') || isa(A,'uint32');
if ~is_unsigned
min_B = min(B(:));
if min_B < 0
B = B - min_B;
end
end
%-----------------------------------------------------------------------------
function checkSizesTandA(T,A)
if numel(T) < 2
error(message('images:normxcorr2:invalidTemplate'))
end
if size(A,1)<size(T,1) || size(A,2)<size(T,2)
error(message('images:normxcorr2:invalidSizeForA'))
end
% %-----------------------------------------------------------------------------
% function checkIfFlat(T)
%
% if std(T(:)) == 0
% error(message('images:normxcorr2:sameElementsInTemplate'))
% end