-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhausdorff_distance.cpp
More file actions
executable file
·243 lines (199 loc) · 7.6 KB
/
hausdorff_distance.cpp
File metadata and controls
executable file
·243 lines (199 loc) · 7.6 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/****************************************************************************
**
** Copyright (C) 2010-2012 Fabien Bessy.
** Contact: [email protected]
**
** This file is part of project Ofeli.
**
** http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
** You may use this file under the terms of the CeCILL license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Fabien Bessy and its Subsidiary(-ies) nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
**
****************************************************************************/
#include "hausdorff_distance.hpp"
#include <cmath> // for the function "std::sqrt"
#include <algorithm> // for the functions "std::max" and "std::swap"
#include <iostream> // for the object "std::cerr"
namespace ofeli
{
HausdorffDistance::HausdorffDistance(const int* list1_1, int list1_1_length, int img1_1_width, int img1_1_height,
const int* list2_1, int list2_1_length, int img2_1_width, int img2_1_height) :
list1(list1_1), list1_length(list1_1_length), img1_width(img1_1_width), img1_height(img1_1_height),
list2(list2_1), list2_length(list2_1_length), img2_width(img2_1_width), img2_height(img2_1_height),
max_dist( calculate_max_dist(img1_1_width,img1_1_height,img2_1_width,img2_1_height) )
{
if( list1_1 == NULL )
{
std::cerr << std::endl << "The pointer list1_1 must be a non-null pointer, it must be allocated." << std::endl;
}
if( list1_1_length == 0 )
{
std::cerr << std::endl << "list1_1 must not be empty." << std::endl;
}
if( list2_1 == NULL )
{
std::cerr << std::endl << "The pointer list2_1 must be a non-null pointer, it must be allocated." << std::endl;
}
if( list2_1_length == 0 )
{
std::cerr << std::endl << "list2_1 must not be empty." << std::endl;
}
//////////////// it calculates the centroids ////////////////////
int x1, y1;
int sum_x1 = 0;
int sum_y1 = 0;
for( int index = 0; index < list1_length; index++ )
{
y1 = list1[index]/img1_width;
x1 = list1[index]-y1*img1_width;
sum_x1 += x1;
sum_y1 += y1;
}
// centroid of list 1
if( list1_length != 0 )
{
mean_x1 = double(sum_x1)/double(list1_length);
mean_y1 = double(sum_y1)/double(list1_length);
}
int x2, y2;
int sum_x2 = 0;
int sum_y2 = 0;
for( int index = 0; index < list2_length; index++ )
{
y2 = list2[index]/img2_width;
x2 = list2[index]-y2*img2_width;
sum_x2 += x2;
sum_y2 += y2;
}
// centroid of list 2
if( list2_length != 0 )
{
mean_x2 = double(sum_x2)/double(list2_length);
mean_y2 = double(sum_y2)/double(list2_length);
}
////////////////////////////////////////////////////////////////////
// hd, Hausdorff distance
// mhd, modified Hausdorff distance
double hd, mhd, backward_hd, backward_mhd;
compute_hausdorff_dist(hd,mhd); // passed by reference (output arguments)
swap_lists(); // list1 <--> list2
compute_hausdorff_dist(backward_hd, backward_mhd); // passed by reference (output arguments)
hausdorff_dist = std::max(hd,backward_hd);
modified_hausdorff_dist = std::max(mhd,backward_mhd);
centroids_dist = std::sqrt( square(mean_x2-mean_x1) + square(mean_y2-mean_y1) );
hausdorff_ratio = hausdorff_dist/max_dist;
modified_hausdorff_ratio = modified_hausdorff_dist/max_dist;
centroids_ratio = centroids_dist/max_dist;
}
void HausdorffDistance::compute_hausdorff_dist(double& hausdorff_dist, double& modified_hausdorff_dist) const
{
int x1, y1, x2, y2;
double relative_x1, relative_y1, relative_x2, relative_y2;
double euclidean_dist, sum_min_dist, min_dist;
sum_min_dist = 0.0;
// initialization in order to maximize
hausdorff_dist = 0.0;
// for each point of list 1
for( int index1 = 0; index1 < list1_length; index1++ )
{
y1 = list1[index1]/img1_width;
x1 = list1[index1]-y1*img1_width;
// position in the centroid frame
relative_x1 = double(x1)-mean_x1;
relative_y1 = double(y1)-mean_y1;
// initialization in order to minimize
min_dist = 999999.999;
// computes the distance of all points of list 2
for( int index2 = 0; index2 < list2_length; index2++ )
{
y2 = list2[index2]/img2_width;
x2 = list2[index2]-y2*img2_width;
// position in the centroid frame
relative_x2 = double(x2)-mean_x2;
relative_y2 = double(y2)-mean_y2;
// computes the euclidean distance
euclidean_dist = std::sqrt( square(relative_x2-relative_x1) + square(relative_y2-relative_y1) );
// it minimizes min_dist
if( euclidean_dist < min_dist )
{
min_dist = euclidean_dist;
}
}
// it maximizes hausdorff_dist
if( min_dist > hausdorff_dist )
{
hausdorff_dist = min_dist;
}
sum_min_dist += min_dist;
}
// modified hausdorff distance = mean of min_dist
modified_hausdorff_dist = sum_min_dist/double(list1_length);
return;
}
void HausdorffDistance::swap_lists()
{
std::swap(list1,list2);
std::swap(list1_length,list2_length);
std::swap(img1_width,img2_width);
std::swap(img1_height,img2_height);
std::swap(mean_x1,mean_x2);
std::swap(mean_y1,mean_y2);
return;
}
double HausdorffDistance::calculate_max_dist(int width1, int height1, int width2, int height2)
{
int max_width = std::max(width1,width2);
int max_height = std::max(height1,height2);
return std::sqrt( double( square(max_width) + square(max_height) ) );
}
double HausdorffDistance::get_hausdorff_dist() const
{
return hausdorff_dist;
}
double HausdorffDistance::get_modified_hausdorff_dist() const
{
return modified_hausdorff_dist;
}
double HausdorffDistance::get_hausdorff_ratio() const
{
return hausdorff_ratio;
}
double HausdorffDistance::get_modified_hausdorff_ratio() const
{
return modified_hausdorff_ratio;
}
double HausdorffDistance::get_centroids_dist() const
{
return centroids_dist;
}
double HausdorffDistance::get_centroids_ratio() const
{
return centroids_ratio;
}
}