Files
scikit-image/skimage/detection/tests/test_template.py
T
Tony S Yu 6272c312c4 Simplify _template.pyx using integral_image from transform subpackage.
Remove `integral_images` and `integral_image_sqr` from _template.pyx in favor of calls to `skimage.transform.integral_image`.

This change required `match_template` arguments ("image" and "template") to be changed from float to double.

After this change, the template test runs about 25% slower.
2012-05-08 21:28:50 -04:00

48 lines
1.5 KiB
Python

import numpy as np
from skimage.detection import match_template
from numpy.random import randn
def test_template():
size = 100
image = np.zeros((400, 400))
target = np.tri(size) + np.tri(size)[::-1]
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()