From baa0b2cfbf3cfcb5b67fdf067a16083c980769e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 9 Dec 2013 00:42:20 +0100 Subject: [PATCH] Add tests for 3-D template matching --- skimage/feature/template.py | 3 ++- skimage/feature/tests/test_template.py | 34 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/skimage/feature/template.py b/skimage/feature/template.py index bd25b4cf..092ab309 100644 --- a/skimage/feature/template.py +++ b/skimage/feature/template.py @@ -118,6 +118,8 @@ def match_template(image, template, pad_input=False, mode='constant', else: image = pad(image, pad_width=pad_width, mode=mode) + # Use special case for 2-D images for much better performance in + # computation of integral images if image.ndim == 2: image_window_sum = _window_sum_2d(image, template.shape) image_window_sum2 = _window_sum_2d(image**2, template.shape) @@ -160,7 +162,6 @@ def match_template(image, template, pad_input=False, mode='constant', c0 = template.shape[1] - 1 c1 = c0 + image_shape[1] - template.shape[1] + 1 - if image.ndim == 3: if pad_input: d0 = (template.shape[2] - 1) // 2 diff --git a/skimage/feature/tests/test_template.py b/skimage/feature/tests/test_template.py index e5ce7244..a74c0ce7 100644 --- a/skimage/feature/tests/test_template.py +++ b/skimage/feature/tests/test_template.py @@ -1,5 +1,5 @@ import numpy as np -from numpy.testing import assert_array_almost_equal as assert_close +from numpy.testing import assert_almost_equal, assert_equal from skimage.morphology import diamond from skimage.feature import match_template, peak_local_max @@ -31,7 +31,7 @@ def test_template(): positions = positions[np.argsort(positions[:, 0])] for xy_target, xy in zip(target_positions, positions): - yield assert_close, xy, xy_target + yield assert_almost_equal, xy, xy_target def test_normalization(): @@ -114,9 +114,35 @@ def test_pad_input(): # get the max and min results. sorted_result = np.argsort(result.flat) i, j = np.unravel_index(sorted_result[:2], result.shape) - assert_close(j, (12, 0)) + assert_equal(j, (12, 0)) i, j = np.unravel_index(sorted_result[-2:], result.shape) - assert_close(j, (18, 6)) + assert_equal(j, (18, 6)) + + +def test_3d(): + np.random.seed(1) + template = np.random.rand(3, 3, 3) + image = np.zeros((12, 12, 12)) + + image[3:6, 5:8, 4:7] = template + + result = match_template(image, template) + + assert_equal(result.shape, (10, 10, 10)) + assert_equal(np.unravel_index(result.argmax(), image.shape), (2, 5, 6)) + + +def test_3d_pad_input(): + np.random.seed(1) + template = np.random.rand(3, 3, 3) + image = np.zeros((12, 12, 12)) + + image[3:6, 5:8, 4:7] = template + + result = match_template(image, template, pad_input=True) + + assert_equal(result.shape, (12, 12, 12)) + assert_equal(np.unravel_index(result.argmax(), image.shape), (4, 6, 5)) if __name__ == "__main__":