mirror of
https://github.com/wassname/scikit-image.git
synced 2026-09-12 12:50:49 +08:00
Move template matching to feature subpackage
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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))
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user