-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
zsad
86 lines (70 loc) · 2.9 KB
/
zsad
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_VISION_MATCH_SCORE_ZSAD_HPP
#define GTL_VISION_MATCH_SCORE_ZSAD_HPP
// Summary: Zero-mean sum of absolute distances between two patches. [wip]
namespace gtl {
template<
int patch_width = 8,
int patch_height = 8,
typename type_lhs = unsigned char,
typename type_rhs = unsigned char
>
float zsad(
const type_lhs* __restrict data_lhs,
const int stride_lhs,
const type_rhs* __restrict data_rhs,
const int stride_rhs
) {
constexpr static const auto abs = [](float value) -> float {
if ((value + 0.0f) < 0) {
return -value;
}
return value;
};
// Cache the data pointer so we can reset after calculating the mean.
const type_lhs* __restrict pointer_lhs = data_lhs;
const type_rhs* __restrict pointer_rhs = data_rhs;
const int step_lhs = stride_lhs - patch_width;
const int step_rhs = stride_rhs - patch_width;
// Calculate the mean of the patch.
float mean_lhs = 0.0f;
float mean_rhs = 0.0f;
for (int y = 0; y < patch_height; ++y, data_lhs += step_lhs, data_rhs += step_rhs) {
for (int x = 0; x < patch_width; ++x, ++data_lhs, ++data_rhs) {
const float pixel_lhs = *data_lhs;
const float pixel_rhs = *data_rhs;
mean_lhs += pixel_lhs;
mean_rhs += pixel_rhs;
}
}
mean_lhs /= static_cast<float>(patch_width * patch_height);
mean_rhs /= static_cast<float>(patch_width * patch_height);
// Reset data pointer.
data_lhs = pointer_lhs;
data_rhs = pointer_rhs;
float sum = 0;
for (int y = 0; y < patch_height; ++y, data_lhs += step_lhs, data_rhs += step_rhs) {
for (int x = 0; x < patch_width; ++x, ++data_lhs, ++data_rhs) {
const float pixel_lhs = *data_lhs;
const float pixel_rhs = *data_rhs;
const float difference = (pixel_lhs - mean_lhs) - (pixel_rhs - mean_rhs);
const float difference_absolute = abs(difference);
sum += difference_absolute;
}
}
return sum;
}
}
#endif // GTL_VISION_MATCH_SCORE_ZSAD_HPP