-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathl2_norm_patch_distance.cpp
104 lines (86 loc) · 4.27 KB
/
l2_norm_patch_distance.cpp
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
/**
* Copyright (C) 2015, Vadim Fedorov <[email protected]>
* Copyright (C) 2015, Gabriele Facciolo <[email protected]>
* Copyright (C) 2015, Pablo Arias <[email protected]>
*
* This program is free software: you can use, modify and/or
* redistribute it under the terms of the simplified BSD
* License. You should have received a copy of this license along
* this program. If not, see
* <http://www.opensource.org/licenses/bsd-license.html>.
*/
#include "l2_norm_patch_distance.h"
L2NormPatchDistance::L2NormPatchDistance()
: APatchDistance() { }
L2NormPatchDistance::L2NormPatchDistance(Shape &patch_size, float gaussian_sigma)
: APatchDistance(patch_size, gaussian_sigma) { }
float L2NormPatchDistance::calculate(const Point &source_point,
const Point &target_point)
{
if (_patch_weighting.is_empty()) {
_patch_weighting = GaussianWeights::calculate(_patch_size.size_x,
_patch_size.size_y,
_gaussian_sigma,
_gaussian_sigma);
}
// calculate distance between source and target patches
int radius_x = _patch_size.size_x / 2;
int radius_y = _patch_size.size_y / 2;
// NOTE: direct access - we sacrifice readability in favor of performance
const float *source_points = _source.raw();
const float *target_points = _target.raw();
const float *p_weight = _patch_weighting.raw();
int number_of_channels = _source.get_number_of_channels();
int source_stride = _source.get_size_x();
int target_stride = _target.get_size_x();
float distance = 0.0;
if (number_of_channels == 3) {
// NOTE: unrolled channel loop for 3 channels
for (int dy = -radius_y; dy < ((int)_patch_size.size_y - radius_y); dy++) {
for (int dx = -radius_x; dx < ((int)_patch_size.size_x - radius_x); dx++, ++p_weight) {
// NOTE: this is the same as: _patch_weighting(dx + radius_x, dy + radius_y);
float weight = *p_weight;
// calculate partial result as a weighted norm
int source_id = 3 * (source_stride * (source_point.y + dy) + (source_point.x + dx));
int target_id = 3 * (target_stride * (target_point.y + dy) + (target_point.x + dx));
float norm = (source_points[source_id] - target_points[target_id]) * (source_points[source_id] - target_points[target_id]) +
(source_points[source_id + 1] - target_points[target_id + 1]) * (source_points[source_id + 1] - target_points[target_id + 1]) +
(source_points[source_id + 2] - target_points[target_id + 2]) * (source_points[source_id + 2] - target_points[target_id + 2]);
distance += weight * norm;
}
}
} else if (number_of_channels == 1) {
// NOTE: no channel loop for 1 channel
for (int dy = -radius_y; dy < ((int)_patch_size.size_y - radius_y); dy++) {
for (int dx = -radius_x; dx < ((int)_patch_size.size_x - radius_x); dx++, ++p_weight) {
// NOTE: this is the same as: _patch_weighting(dx + radius_x, dy + radius_y);
float weight = *p_weight;
// calculate partial result as a weighted norm
int source_id = source_stride * (source_point.y + dy) + (source_point.x + dx);
int target_id = target_stride * (target_point.y + dy) + (target_point.x + dx);
float norm = (source_points[source_id] - target_points[target_id]) * (source_points[source_id] - target_points[target_id]);
distance += weight * norm;
}
}
} else {
for (int dy = -radius_y; dy < ((int)_patch_size.size_y - radius_y); dy++) {
for (int dx = -radius_x; dx < ((int)_patch_size.size_x - radius_x); dx++, ++p_weight) {
// NOTE: this is the same as: _patch_weighting(dx + radius_x, dy + radius_y);
float weight = *p_weight;
// calculate partial result as a weighted norm
float norm = 0.0;
int source_id = number_of_channels * (source_stride * (source_point.y + dy) + (source_point.x + dx));
int target_id = number_of_channels * (target_stride * (target_point.y + dy) + (target_point.x + dx));
for (int ch = 0; ch < number_of_channels; ch++) {
// NOTE: this is the same as: source(source_point + dp);
float color_a = source_points[source_id + ch];
// NOTE: this is the same as: target(target_point + dp);
float color_b = target_points[target_id + ch];
norm += (color_a - color_b) * (color_a - color_b);
}
distance += weight * norm;
}
}
}
return distance / (_patch_size.size_x * _patch_size.size_y);
}