From b815b5d04508ed6e61217cf1b2996fe6940f4f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 10 Dec 2013 09:19:47 +0100 Subject: [PATCH] Refactor final slicing --- skimage/feature/template.py | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/skimage/feature/template.py b/skimage/feature/template.py index 020bf8f1..8a508f73 100644 --- a/skimage/feature/template.py +++ b/skimage/feature/template.py @@ -156,27 +156,14 @@ def match_template(image, template, pad_input=False, mode='constant', response[mask] = nom[mask] / denom[mask] - if pad_input: - r0 = (template.shape[0] - 1) // 2 - r1 = r0 + image_shape[0] - c0 = (template.shape[1] - 1) // 2 - c1 = c0 + image_shape[1] - else: - r0 = template.shape[0] - 1 - r1 = r0 + image_shape[0] - template.shape[0] + 1 - c0 = template.shape[1] - 1 - c1 = c0 + image_shape[1] - template.shape[1] + 1 - - if image.ndim == 3: + slices = [] + for i in range(template.ndim): if pad_input: - d0 = (template.shape[2] - 1) // 2 - d1 = d0 + image_shape[2] + d0 = (template.shape[i] - 1) // 2 + d1 = d0 + image_shape[i] else: - d0 = template.shape[2] - 1 - d1 = d0 + image_shape[2] - template.shape[2] + 1 + d0 = template.shape[i] - 1 + d1 = d0 + image_shape[i] - template.shape[i] + 1 + slices.append(slice(d0, d1)) - response = response[r0:r1, c0:c1, d0:d1] - else: - response = response[r0:r1, c0:c1] - - return response + return response[slices]