Add tests for 3-D template matching

This commit is contained in:
Johannes Schönberger
2013-12-09 00:42:20 +01:00
parent 8443c8469c
commit baa0b2cfbf
2 changed files with 32 additions and 5 deletions
+2 -1
View File
@@ -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
+30 -4
View File
@@ -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__":