-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilarity_methods_target_patches.py
49 lines (42 loc) · 1.33 KB
/
similarity_methods_target_patches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
# Similarity methods
def cross_correlation(template, image):
return np.sum(template * image)
def normalized_cross_correlation(template, image):
numerator = np.sum(template * image)
denominator = np.sqrt(np.sum(template**2) * np.sum(image**2))
return numerator / denominator
def sum_of_absolute_differences(template, image):
return np.sum(np.abs(template - image))
# Templates and Image patch
templates = {
'T1': np.array([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
]),
'T2': np.array([
[1, 1, 1],
[1, 1, 0],
[1, 1, 1]
]),
'T3': np.array([
[1, 1, 1],
[1, 0, 0],
[1, 1, 1]
])
}
I = np.array([
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
])
methods = [cross_correlation, normalized_cross_correlation, sum_of_absolute_differences]
names = ['Cross-correlation', 'Normalized Cross-correlation', 'Sum of Absolute Differences']
# Calculate similarities for each template with the image patch
for template_name, template in templates.items():
print(f"Comparing template {template_name} with image patch I:")
for k, method in enumerate(methods):
score = method(template, I)
print(f"{names[k]}: {score}")
print("\n")