-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDistance.hpp
executable file
·184 lines (162 loc) · 4.43 KB
/
Distance.hpp
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
/***********************************************************************
$FILENAME : Distance.hpp
$TITLE : Distance template functions
$DATE : 7 Nov 2017
$VERSION : 1.0.0
$DESCRIPTION : Includes utility template funtions for computing distance
between two matrixes
$AUTHOR : Armin Zare Zadeh (ali.a.zarezadeh @ gmail.com)
************************************************************************/
#ifndef DISTANCE_H_
#define DISTANCE_H_
#include <opencv2/core/core.hpp> // Basic OpenCV structures
//#include <stdio.h>
//#include <stdint.h>
//#include "math.h"
#include "Constants.h"
using namespace std;
using namespace cv;
template<class TEMPL>
void distanceRow(InputArray _in1, InputArray _in2, const int flag, OutputArray _dist)
{
Mat in1_ = _in1.getMat();
Mat in2_ = _in2.getMat();
_dist.create( in1_.rows, in2_.rows, CV_32FC1 );
Mat dist_ = _dist.getMat();
switch (flag)
{
case 1 : // normalized correlation
{
double corr = 0.0;
double norm1 = 0.0;
double norm2 = 0.0;
for(int i=0; i<in1_.rows; i++){
for(int j=0; j<in2_.rows; j++){
corr = 0.0; norm1 = 0.0; norm2 = 0.0;
for(int jj=0; jj<in1_.cols; jj++){
corr += in1_.at<TEMPL>(i,jj)*in2_.at<TEMPL>(j,jj);
norm1 += in1_.at<TEMPL>(i,jj)*in1_.at<TEMPL>(i,jj);
norm2 += in2_.at<TEMPL>(j,jj)*in2_.at<TEMPL>(j,jj);
}
dist_.at<float>(i,j) = (float)((corr / sqrt(norm1*norm2) + 1) / 2.0);
}
}
}
break;
case 2 : // euclidean distance
{
double sum;
for(int i=0; i<in1_.rows; i++){
for(int j=0; j<in2_.rows; j++){
sum = 0;
for(int jj=0; jj<in1_.cols; jj++){
sum += pow(in1_.at<TEMPL>(i,jj)-in2_.at<TEMPL>(j,jj), 2);
}
dist_.at<float>(i,j) = (float)sqrt(sum);
}
}
}
break;
}
return;
}
template<class TEMPL>
void distanceCol(InputArray _in1, InputArray _in2, const int flag, OutputArray _dist)
{
Mat in1_ = _in1.getMat();
Mat in2_ = _in2.getMat();
_dist.create( in1_.cols, in2_.cols, CV_32FC1 );
Mat dist_ = _dist.getMat();
switch (flag)
{
case 1 : // normalized correlation
{
double corr = 0.0;
double norm1 = 0.0;
double norm2 = 0.0;
for(int i=0; i<in1_.cols; i++){
for(int j=0; j<in2_.cols; j++){
corr = 0.0; norm1 = 0.0; norm2 = 0.0;
for(int jj=0; jj<in1_.rows; jj++){
corr += in1_.at<TEMPL>(jj,i)*in2_.at<TEMPL>(jj,j);
norm1 += in1_.at<TEMPL>(jj,i)*in1_.at<TEMPL>(jj,i);
norm2 += in2_.at<TEMPL>(jj,j)*in2_.at<TEMPL>(jj,j);
}
dist_.at<float>(i,j) = (float)((corr / sqrt(norm1*norm2) + 1) / 2.0);
}
}
}
break;
case 2 : // euclidean distance
{
double sum;
for(int i=0; i<in1_.cols; i++){
for(int j=0; j<in2_.cols; j++){
sum = 0;
for(int jj=0; jj<in1_.rows; jj++){
sum += pow(in1_.at<TEMPL>(jj,i)-in2_.at<TEMPL>(jj,j), 2);
}
dist_.at<float>(i,j) = (float)sqrt(sum);
}
}
}
break;
default:
assert(0);
CV_Error( CV_BadDepth, "" );
}
return;
}
template<class TEMPL>
void distanceCol(InputArray _in, const int flag, OutputArray _dist)
{
Mat in_ = _in.getMat();
_dist.create( 1, (in_.cols)*(in_.cols-1)/2, CV_32FC1 );
Mat dist_ = _dist.getMat();
switch (flag)
{
case 1 : // normalized correlation
{
double corr = 0.0;
double norm1 = 0.0;
double norm2 = 0.0;
int cnt = 0;
for(int i=0; i<in_.cols-1; i++){
for(int j=i+1; j<in_.cols; j++){
corr = 0; norm1 = 0; norm2 = 0;
for(int jj=0; jj<in_.rows; jj++){
corr += in_.at<TEMPL>(jj,i)*in_.at<TEMPL>(jj,j);
norm1 += in_.at<TEMPL>(jj,i)*in_.at<TEMPL>(jj,i);
norm2 += in_.at<TEMPL>(jj,j)*in_.at<TEMPL>(jj,j);
}
CV_Assert( cnt < dist_.cols );
dist_.at<float>(0,cnt) = (float)((corr / sqrt(norm1*norm2) + 1) / 2.0);
cnt++;
}
}
}
break;
case 2 : // euclidean distance
{
double sum;
int cnt = 0;
for(int i=0; i<in_.cols-1; i++){
for(int j=i+1; j<in_.cols; j++){
sum = 0.0;
for(int jj=0; jj<in_.rows; jj++){
sum += pow(in_.at<TEMPL>(jj,i)-in_.at<TEMPL>(jj,j), 2);
}
CV_Assert( cnt < dist_.cols );
dist_.at<float>(0,cnt) = (float)sqrt(sum);
cnt++;
}
}
}
break;
default:
assert(0);
CV_Error( CV_BadDepth, "" );
}
return;
}
#endif /* DISTANCE_H_ */