-
Notifications
You must be signed in to change notification settings - Fork 0
/
MASFinger.py
267 lines (208 loc) · 7.74 KB
/
MASFinger.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import cv2 as cv
from glob import glob
import os
import numpy as np
from utils.poincare import calculate_singularities
from utils.segmentation import create_segmented_and_variance_images
from utils.normalization import normalize
from utils.gabor_filter import gabor_filter
from utils.frequency import ridge_freq
from utils import orientation
from utils.crossing_number import calculate_minutiaes
from utils.skeletonize import skeletonize
from MyMAS import MAS
def fingerprint_pipline(input_img):
block_size = 16
# normalization -> orientation -> frequency -> mask -> filtering
# normalization - removes the effects of sensor noise and finger pressure differences.
normalized_img = normalize(input_img.copy(), float(100), float(100))
# color threshold
# threshold_img = normalized_img
# _, threshold_im = cv.threshold(normalized_img,127,255,cv.THRESH_OTSU)
# cv.imshow('color_threshold', normalized_img); cv.waitKeyEx()
# ROI and normalisation
(segmented_img, normim, mask) = create_segmented_and_variance_images(normalized_img, block_size, 0.2)
# orientations
angles = orientation.calculate_angles(normalized_img, W=block_size, smoth=False)
orientation_img = orientation.visualize_angles(segmented_img, mask, angles, W=block_size)
# find the overall frequency of ridges in Wavelet Domain
freq = ridge_freq(normim, mask, angles, block_size, kernel_size=5, minWaveLength=5, maxWaveLength=15)
# create gabor filter and do the actual filtering
gabor_img = gabor_filter(normim, angles, freq)
# thinning oor skeletonize
thin_image = skeletonize(gabor_img)
# minutias
minutias, end, bif = calculate_minutiaes(thin_image)
# singularities
singularities_img, FingerType = calculate_singularities(thin_image, angles, 1, block_size, mask)
output_imgs = [input_img, minutias, singularities_img]
return output_imgs, end, bif, FingerType
def DISTANCE(U, V) -> float:
"""
"""
return np.sqrt((U[0] - V[0])**2 + (U[1] - V[1])**2)
def Points_distance(points1, points2) -> list:
n = points1.shape[0]
m = points2.shape[0]
Distance = np.zeros((n, m))
for i in range(n):
for j in range(m):
Distance[i, j] = DISTANCE(points1[i], points2[j])
return Distance
def Filter_Points(ListPoints, CenterPoint, size = (0,0)):
"""
"""
n = ListPoints.shape[0]
ResultPoints = []
for i in range(n):
distance = np.abs(CenterPoint - ListPoints[i])
if (distance[0] <= size[0]/2) and (distance[1] <= size[1]/2):
ResultPoints.append(ListPoints[i].tolist())
return np.array(ResultPoints)
def Transtation_Finger(Minutiae, Trans_Vector) -> np.ndarray:
"""
"""
return Minutiae + Trans_Vector
def MATCH(finger1, finger2, alpha=0.5, box_size=(0, 0)) -> bool:
"""
"""
# Get feature
center_point1, end_1, bif_1 = finger1[0], finger1[1], finger1[2]
center_point2, end_2, bif_2 = finger2[0], finger2[1], finger2[2]
translation_vector = center_point2 - center_point1
# Translation minutiaes
end_1 = Transtation_Finger(end_1, translation_vector)
bif_1 = Transtation_Finger(bif_1, translation_vector)
# Filter minutiaes
new_end_1 = Filter_Points(end_1, center_point2, size=box_size)
new_bif_1 = Filter_Points(bif_1, center_point2, size=box_size)
new_end_2 = Filter_Points(end_2, center_point2, size=box_size)
new_bif_2 = Filter_Points(bif_2, center_point2, size=box_size)
n1_end = new_end_1.shape[0]
n1_bif = new_bif_1.shape[0]
n2_end = new_end_2.shape[0]
n2_bif = new_bif_2.shape[0]
# Match ending points
Distance_end = Points_distance(new_end_1, new_end_2)
end_min_dist = Distance_end.min(axis=0)
end_min_index = Distance_end.argmin(axis=0)
# Match bifucation points
Distance_bif = Points_distance(new_bif_1, new_bif_2)
bif_min_dist = Distance_bif.min(axis=0)
bif_min_index = Distance_bif.argmin(axis=0)
# Check Matching
end_matched = np.where(end_min_dist <= 15)[0].shape[0]
bif_matched = np.where(bif_min_dist <= 15)[0].shape[0]
if (end_matched + bif_matched)/(n1_end + n1_bif) >= alpha:
return True
else:
return False
def FeaturesExtraction(finger):
"""
"""
imgresult, end, bif, fingertype = fingerprint_pipline(finger)
center_point = np.array(fingertype)[:,1:].astype(int).mean(axis=0)
end = np.array(end)[:,1:]
bif = np.array(bif)[:,1:]
return [center_point, end, bif]
def S_Generation(PIN, lenMask) -> str:
"""
"""
S = bin(PIN)[2:]
n = len(S)
k = lenMask//n
return S*k + S[:lenMask - n*k]
def Features2Msg(features):
"""
"""
center, end, bif = features[0].astype(int), features[1].astype(int), features[2].astype(int)
center_msg = "{:3d}{:3d}".format(center[0], center[1]).replace(' ', '0')
n = end.shape[0]
m = bif.shape[0]
end_msg = ''
for point in end:
end_msg += "{:3}{:3}".format(point[0], point[1]).replace(' ', '0')
bif_msg = ''
for point in bif:
bif_msg += "{:3}{:3}".format(point[0], point[1]).replace(' ', '0')
return "{}{:3}{:3}{}{}".format(center_msg, n, m, end_msg, bif_msg).replace(' ', '0')
def EncryptFinger(finger, PIN, A, B, X, Code, lenMask, k, padWord):
"""
"""
# generation S
# S = S_Generation(PIN, lenMask)
S = b'10001101101011101000100011011010111010001110100011101000'
print('generation Mask successful!')
Msg = Features2Msg(finger)
print('generation Msg successful!')
cryptosystem = MAS(A, B, Code, X, S, k, padWord)
EMsg = cryptosystem.Encode(Msg)
print('Encode successful!')
return EMsg
def Emsg2Features(EMsg, PIN, A, B, X, Code, lenMask, k, padWord):
"""
"""
end = []
bif = []
S = b'10001101101011101000100011011010111010001110100011101000'
print('generation Mask successful!')
cryptosystem = MAS(A, B, Code, X, S, k, padWord)
Result = "".join(cryptosystem.Decode(EMsg))
CenterPoint = np.array([Result[:3], Result[3:6]]).astype(int)
n, m = int(Result[6:9]), int(Result[9: 12])
t = 12
for i in range(n):
end.append([int(Result[t: t+3]), int(Result[t+3: t+6])])
t += 6
for i in range(m):
bif.append([int(Result[t: t+3]), int(Result[t+3: t+6])])
t += 6
return [np.array(CenterPoint), np.array(end), np.array(bif)]
# Config Cryptosystem
PIN = 123456
lenMask = 56
# Example Initialization
A = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
B = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p']
Code = {'a': b'1000',
'b': b'1110',
'c': b'0011',
'd': b'1111',
'e': b'1101',
'f': b'0010',
'g': b'1100',
'h': b'0101',
'i': b'1011',
'j': b'0000',
'k': b'1001',
'l': b'0111',
'm': b'0100',
'n': b'1010',
'o': b'0001',
'p': b'0110'}
# Language
X0 = ['a', 'cgh']
X1 = ['egm', 'nmc']
X2 = ['ig', 'fce']
X3 = ['jkd']
X4 = ['bea', 'mok']
X5 = ['fno', 'ihc']
X6 = ['cei']
X7 = ['demc', 'khm']
X8 = ['lbkh']
X9 = ['kog', 'dcef']
X = [X0, X1, X2, X3, X4, X5, X6, X7, X8, X9]
paddingWord = 'p'
k = 3
finger_path1 = './sample_inputs/101_1.tif'
image1 = cv.imread(finger_path1, 0)
finger_origin = FeaturesExtraction(image1)
# Save Finger
EMsg = EncryptFinger(finger_origin, PIN, A, B, X, Code, lenMask, k, paddingWord)
print(EMsg)
# # Reload Finger
# finger_saved = Emsg2Features(EMsg, PIN, A, B, X, Code, lenMask, k, paddingWord)
# finger_path2 = './sample_inputs/101_2.tif'
# image2 = cv.imread(finger_path2, 0)
# finger_match = FeaturesExtraction(image2)
# print(MATCH(finger_saved, finger_match, box_size=(150, 300)))