-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
147 lines (115 loc) · 3.07 KB
/
metrics.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
from numba import njit
@njit
def normalized_cross_correlation(
patch1: np.ndarray,
patch2: np.ndarray
) -> float:
"""Calculates NNC.
Args:
patch1: patch from left/right image
patch2: patch from right/left image
Returns:
NNC value
"""
N = len(patch1.flatten())
p1m = np.mean(patch1)
p2m = np.mean(patch2)
t1 = patch1 - p1m
t2 = patch2 - p2m
num = 1 / N * np.sum(t1 * t2)
denom = np.std(patch1) * np.std(patch2) + 1e-7
return -num / denom
@njit
def sum_of_squared_differences(
patch1: np.ndarray,
patch2: np.ndarray
) -> float:
"""Calculates SSD.
Args:
patch1: patch from left/right image
patch2: patch from right/left image
Returns:
SSD values
"""
return np.sum(np.square(patch1 - patch2))
@njit
def normalized_sum_of_squared_differences(
patch1: np.ndarray,
patch2: np.ndarray
) -> float:
"""Calculates Normalized SSD.
Args:
patch1: patch from left/right image
patch2: patch from right/left image
Returns:
NSSD value.
"""
p1m = np.mean(patch1)
p2m = np.mean(patch2)
t1 = patch1 - p1m
t2 = patch2 - p2m
tt1 = t1 / np.sqrt(np.sum(np.square(t1)))
tt2 = t2 / np.sqrt(np.sum(np.square(t2)))
return np.sum(np.square(tt1 - tt2))
@njit
def sum_of_absolute_differences(
patch1: np.ndarray,
patch2: np.ndarray
) -> float:
"""Calculates SAD.
Args:
patch1: patch from left/right image
patch2: patch from right/left image
Returns:
SAD value.
"""
return np.sum(np.abs(patch1 - patch2))
@njit
def structural_similarity_index(
patch1: np.ndarray,
patch2: np.ndarray
) -> float:
"""Calculates SSIM.
Args:
patch1: patch from left/right image
patch2: patch from right/left image
Returns:
SSIM value.
"""
c = 1e-7
mp1 = patch1.mean()
mp2 = patch2.mean()
cov12 = np.mean((patch1 - patch1.mean()) * (patch2 - patch2.mean()))
stdp1 = patch1.std()
stdp2 = patch2.std()
num = (2 * mp1 * mp2 + c) * (2 * cov12 + c)
denom = (mp1 ** 2 + mp2 ** 2 + c) * (stdp1 ** 2 + stdp2 ** 2 + c)
# * -1 because we take argmin
ssim = num / denom * -1
return ssim
def compile_metrics(patch1: np.ndarray, patch2: np.ndarray) -> None:
"""Utility function to precompile numba functions to machine code.
Args:
patch1: patch from left/right image
patch2: patch from right/left image
Returns:
"""
_ = normalized_cross_correlation(patch1, patch2)
_ = sum_of_squared_differences(patch1, patch2)
_ = normalized_sum_of_squared_differences(patch1, patch2)
_ = sum_of_absolute_differences(patch1, patch2)
_ = structural_similarity_index(patch1, patch2)
if __name__ == '__main__':
x = 13
bls = np.array([
list(range(x)),
list(range(x, 2 * x)),
list(range(2 * x, 3 * x))
])
temp = np.array([
[4, 5, 6],
[17, 18, 19],
[30, 31, 32]
])
compile_metrics(temp, bls[:, 10:13])