-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetHammingDistance.cpp
More file actions
215 lines (168 loc) · 5.42 KB
/
Copy pathGetHammingDistance.cpp
File metadata and controls
215 lines (168 loc) · 5.42 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
#include "GetHammingDistance.h"
#include <iostream>
using namespace std;
#include <string.h>
#include "Shifts.h"
GetHammingDistance::GetHammingDistance(int x, int y)
{
width = 0;
height = 0;
maxShiftX = x;
maxShiftY = y;
template1s = NULL;
mask1s = NULL;
mask = NULL;
C = NULL;
}
GetHammingDistance::~GetHammingDistance()
{
if(template1s != NULL)
delete[] template1s;
if(mask1s != NULL)
delete[] mask1s;
if(mask != NULL)
delete[] mask;
if(C != NULL)
delete[] C;
}
void GetHammingDistance::initializeTemplates(const MatchingTemplate* classTemplate1,
const MatchingTemplate* classTemplate2)
{
int t_width = classTemplate1->getNumAngularDivisions();
int t_height = classTemplate1->getNumRadialDivisions();
if ( (t_width != classTemplate2->getNumAngularDivisions() ) ||
(t_height != classTemplate2->getNumRadialDivisions() ) )
{
cout << "Error: Template dimensions not equal" << endl;
return;
}
if (width*height == 0)
{
template1s = new int[t_width*t_height];
mask1s = new int[t_width*t_height];
mask = new int[t_width*t_height];
C = new int[t_width*t_height];
}
else
{
if(template1s != NULL)
delete[] template1s;
template1s = new int[t_width*t_height];
if(mask1s != NULL)
delete[] mask1s;
mask1s = new int[t_width*t_height];
if(mask != NULL)
delete[] mask;
mask = new int[t_width*t_height];
if(C != NULL)
delete[] C;
C = new int[t_width*t_height];
}
width = t_width;
height = t_height;
}
double GetHammingDistance::calcHD(int* tTemplate, int* tMask, int* qTemplate, int* qMask,
int* newTemplate, int* newMask)
{
int nummaskbits = 0;
int bitsdiff = 0;
int totalbits = 0;
double hd1 = -1;
for (int i = 0; i < width * height; i++)
{
mask[i] = newMask[i] | qMask[i];//Bitwise: OR
if (mask[i] == 1)
nummaskbits++;
C[i] = newTemplate[i] ^ qTemplate[i];//Bitwise: XOR
C[i] = C[i] & (1-mask[i]);//Bitwise: AND
if (C[i] == 1)
bitsdiff++;
}
totalbits = width * height - nummaskbits;
if (totalbits != 0)
{
hd1 = (double)bitsdiff / totalbits;
}
return hd1;
}
double GetHammingDistance::computeHDX(const MatchingTemplate* classTemplate1,
const MatchingTemplate* classTemplate2,
int scales)
{
initializeTemplates(classTemplate1, classTemplate2);
int *template1 = classTemplate1->getIrisTemplatePtr();
int *template2 = classTemplate2->getIrisTemplatePtr();
int *mask1 = classTemplate1->getIrisMaskPtr();
int *mask2 = classTemplate2->getIrisMaskPtr();
double hd = -1;
//int index = 1;
for (int shifts = -maxShiftX; shifts <= maxShiftX; shifts++)
{
Shifts::X_ShiftBits(template1, width, height, shifts, scales, template1s);
Shifts::X_ShiftBits(mask1, width, height, shifts, scales, mask1s);
double hd1 = calcHD(template1, mask1, template2, mask2, template1s, mask1s);
if(hd1 < hd || hd == -1)
hd = hd1;
}
return hd;
}
double GetHammingDistance::computeHDY(const MatchingTemplate* classTemplate1,
const MatchingTemplate* classTemplate2,
int scales)
{
initializeTemplates(classTemplate1, classTemplate2);
int *template1 = classTemplate1->getIrisTemplatePtr();
int *template2 = classTemplate2->getIrisTemplatePtr();
int *mask1 = classTemplate1->getIrisMaskPtr();
int *mask2 = classTemplate2->getIrisMaskPtr();
double hd = -1;
int index = 1;
for (int shifts = -maxShiftY; shifts <= maxShiftY; shifts++)
{
Shifts::Y_ShiftBits(template1, width, height, shifts, scales, template1s);
Shifts::Y_ShiftBits(mask1, width, height, shifts, scales, mask1s);
double hd1 = calcHD(template1, mask1, template2, mask2, template1s, mask1s);
if(hd1 < hd || hd == -1)
hd = hd1;
}
return hd;
}
double GetHammingDistance::computeHDXorY(const MatchingTemplate* classTemplate1,
const MatchingTemplate* classTemplate2,
int scales)
{
double xHD = computeHDX(classTemplate1, classTemplate2, scales);
double yHD = computeHDY(classTemplate1, classTemplate2, scales);
double hd = (xHD < yHD) ? hd = xHD : hd = yHD;
return hd;
}
double GetHammingDistance::computeHDXandY(const MatchingTemplate* classTemplate1,
const MatchingTemplate* classTemplate2,
int scales)
{
initializeTemplates(classTemplate1, classTemplate2);
template2s = new int[width*height];
mask2s = new int[width*height];
int *template1 = classTemplate1->getIrisTemplatePtr();
int *template2 = classTemplate2->getIrisTemplatePtr();
int *mask1 = classTemplate1->getIrisMaskPtr();
int *mask2 = classTemplate2->getIrisMaskPtr();
double hd = -1;
//int index = 1;
for (int shifts1 = -maxShiftY; shifts1 <= maxShiftY; shifts1++)
{
Shifts::Y_ShiftBits(template1, width, height, shifts1, scales, template1s);
Shifts::Y_ShiftBits(mask1, width, height, shifts1, scales, mask1s);
for (int shifts2 = -maxShiftX; shifts2 <= maxShiftX; shifts2++)
{
Shifts::X_ShiftBits(template1s, width, height, shifts2, scales, template2s);
Shifts::X_ShiftBits(mask1s, width, height, shifts2, scales, mask2s);
double hd1 = calcHD(template1, mask1, template2, mask2, template2s, mask2s);
if(hd1 < hd || hd == -1)
hd = hd1;
}
}
delete[] template2s;
delete[] mask2s;
return hd;
}