Files
scikit-image/skimage/feature/tests/test_template.py
T
Tony S Yu 2a00e24b12 Change _template.match_template variables to float.
Template and image in test are converted to float32 before passing to `match_template`. This change is temporary: `match_template` should convert these variables internally.
2012-05-08 21:32:06 -04:00

49 lines
1.6 KiB
Python

import numpy as np
from skimage.feature import match_template
from numpy.random import randn
def test_template():
size = 100
image = np.zeros((400, 400), dtype=np.float32)
target = np.tri(size) + np.tri(size)[::-1]
target = target.astype(np.float32)
target_positions = [(50, 50), (200, 200)]
for x, y in target_positions:
image[x:x + size, y:y + size] = target
image += randn(400, 400) * 2
for method in ["norm-corr", "norm-coeff"]:
result = match_template(image, target, method=method)
delta = 5
found_positions = []
# find the targets
for i in range(50):
index = np.argmax(result)
y, x = np.unravel_index(index, result.shape)
if not found_positions:
found_positions.append((x, y))
for position in found_positions:
distance = np.sqrt((x - position[0]) ** 2 +
(y - position[1]) ** 2)
if distance > delta:
found_positions.append((x, y))
result[y, x] = 0
if len(found_positions) == len(target_positions):
break
for x, y in target_positions:
print x, y
found = False
for position in found_positions:
distance = np.sqrt((x - position[0]) ** 2 +
(y - position[1]) ** 2)
if distance < delta:
found = True
assert found
if __name__ == "__main__":
from numpy import testing
testing.run_module_suite()