Template mean is precalculated and referenced

This commit is contained in:
Pradyumna Narayana
2016-03-03 11:45:09 -07:00
parent 14852f4578
commit 2c39c773f4
+3 -2
View File
@@ -131,8 +131,9 @@ def match_template(image, template, pad_input=False, mode='constant',
image_window_sum = _window_sum_3d(image, template.shape)
image_window_sum2 = _window_sum_3d(image ** 2, template.shape)
template_mean = template.mean()
template_volume = np.prod(template.shape)
template_ssd = np.sum((template - template.mean()) ** 2)
template_ssd = np.sum((template - template_mean) ** 2)
if image.ndim == 2:
xcorr = fftconvolve(image, template[::-1, ::-1],
@@ -141,7 +142,7 @@ def match_template(image, template, pad_input=False, mode='constant',
xcorr = fftconvolve(image, template[::-1, ::-1, ::-1],
mode="valid")[1:-1, 1:-1, 1:-1]
nom = xcorr - image_window_sum * (template.sum() / template_volume)
nom = xcorr - image_window_sum * template_mean
denom = image_window_sum2
np.multiply(image_window_sum, image_window_sum, out=image_window_sum)