Skip to content

Commit 87ba2f7

Browse files
authored
Add files via upload
1 parent 9dd8bcf commit 87ba2f7

19 files changed

+747
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import cv2
2+
import numpy as np
3+
import glob
4+
import math
5+
import scipy
6+
from scipy.spatial import distance
7+
from scipy import signal
8+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
9+
import matplotlib.pyplot as plt
10+
import pandas as pd
11+
from sklearn import metrics
12+
13+
14+
#modulating function as defined in paper
15+
def m(x ,y, f):
16+
val = np.cos(2*np.pi*f*math.sqrt(x **2 + y**2))
17+
return val
18+
#spatial filter as defined in paper
19+
def gabor(x, y, dx, dy, f):
20+
gb = (1/(2*math.pi*dx*dy))*np.exp(-0.5*(x**2 / dx**2 + y**2 / dy**2)) * m(x, y, f)
21+
return gb
22+
23+
#function to calculate spatial filter over 8x8 blocks
24+
def spatial(f,dx,dy):
25+
sfilter=np.zeros((8,8))
26+
for i in range(8):
27+
for j in range(8):
28+
sfilter[i,j]=gabor((-4+j),(-4+i),dx,dy,f)
29+
return sfilter
30+
31+
def get_vec(convolvedtrain1,convolvedtrain2):
32+
feature_vec=[]
33+
for i in range(6):
34+
for j in range(64):
35+
#Run 8 by 8 filtered block iteratively over the entire image
36+
start_height = i*8
37+
end_height = start_height+8
38+
start_wid = j*8
39+
end_wid = start_wid+8
40+
grid1 = convolvedtrain1[start_height:end_height, start_wid:end_wid]
41+
grid2 = convolvedtrain2[start_height:end_height, start_wid:end_wid]
42+
43+
# Channel 1
44+
absolute = np.absolute(grid1)
45+
# mean
46+
mean = np.mean(absolute)
47+
feature_vec.append(mean)
48+
#deviation
49+
std = np.mean(np.absolute(absolute-mean))
50+
feature_vec.append(std)
51+
52+
# Channel 2
53+
absolute = np.absolute(grid2)
54+
# mean
55+
mean = np.mean(absolute)
56+
feature_vec.append(mean)
57+
#deviation
58+
std = np.mean(np.absolute(absolute-mean))
59+
feature_vec.append(std)
60+
61+
return feature_vec
62+
63+
def FeatureExtraction(enhanced):
64+
con1=[]
65+
con2=[]
66+
#get spatial filters
67+
filter1=spatial(0.67,3,1.5)
68+
filter2=spatial(0.67,4,1.5)
69+
70+
feature_vector=[]
71+
72+
for i in range(len(enhanced)):
73+
img=enhanced[i]
74+
#define a 48x512 region over which the filters are applied
75+
img_roi=img[:48,:]
76+
77+
filtered1=scipy.signal.convolve2d(img_roi,filter1,mode='same')
78+
filtered2=scipy.signal.convolve2d(img_roi,filter2,mode='same')
79+
80+
con1.append(filtered1)
81+
con2.append(filtered2)
82+
fv=get_vec(filtered1,filtered2)
83+
feature_vector.append(fv)
84+
return feature_vector #each feature vector has a dimension of 1536
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# coding: utf-8
3+
4+
import cv2
5+
import numpy as np
6+
import glob
7+
import math
8+
from scipy.spatial import distance
9+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
10+
import matplotlib.pyplot as plt
11+
import pandas as pd
12+
from sklearn import metrics
13+
14+
#Equalizes the histogram of the image
15+
def ImageEnhancement(normalized):
16+
enhanced=[]
17+
for res in normalized:
18+
res = res.astype(np.uint8)
19+
im=cv2.equalizeHist(res)
20+
enhanced.append(im)
21+
return enhanced
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
# coding: utf-8
3+
4+
import cv2
5+
import numpy as np
6+
import glob
7+
import math
8+
from scipy.spatial import distance
9+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
10+
import matplotlib.pyplot as plt
11+
import pandas as pd
12+
from sklearn import metrics
13+
14+
def IrisLocalization(images):
15+
#convert image to a color image
16+
target = [cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for img in images]
17+
boundary=[] #initialize empty list that will eventually contain all the images with boundaries
18+
centers=[] #initialize empty list that will contain the centers of the boundary circles
19+
for img in target:
20+
21+
draw_img=img
22+
23+
# remove noise by blurring the image
24+
blur = cv2.bilateralFilter(img, 9,75,75)
25+
img=blur
26+
27+
#estimate the center of pupil
28+
horizontalProjection = np.mean(img,0);
29+
verticalProjection = np.mean(img,1);
30+
center_x=horizontalProjection.argmin()
31+
center_y=verticalProjection.argmin()
32+
33+
#recalculate of pupil by concentrating on a 120X120 area
34+
centrecrop_x = img[center_x-60:center_x+60]
35+
centrecrop_y = img[center_y-60:center_y+60]
36+
horizontalProjection = np.mean(centrecrop_y,0);
37+
verticalProjection = np.mean(centrecrop_x,0);
38+
crop_center_x=horizontalProjection.argmin()
39+
crop_center_y=verticalProjection.argmin()
40+
41+
cimg=img.copy()
42+
cv2.circle(cimg,(crop_center_x,crop_center_y),1,(255,0,0),2)
43+
44+
#apply Canny edge detector on the masked image
45+
maskimage = cv2.inRange(img, 0, 70)
46+
output = cv2.bitwise_and(img, maskimage)
47+
edged = cv2.Canny(output, 100, 220)
48+
49+
# Apply Hough transform to find potential boundaries of pupil
50+
circles = cv2.HoughCircles(edged, cv2.HOUGH_GRADIENT, 10, 100)
51+
52+
#define the center of the pupil
53+
a = (crop_center_x,crop_center_y)
54+
55+
out = img.copy()
56+
min_dst=math.inf
57+
for i in circles[0]:
58+
#find the circle whose center is closest to the approx center found above
59+
b=(i[0],i[1])
60+
dst = distance.euclidean(a, b)
61+
if dst<min_dst:
62+
min_dst=dst
63+
k=i
64+
65+
#draw the inner boundary
66+
cv2.circle(draw_img, (k[0], k[1]), k[2], (255, 0, 0), 3)
67+
68+
pupil=circles[0][0]
69+
radius_pupil = int(k[2])
70+
71+
#draw the outer boundary, which is approximately found to be at a distance 53 from the inner boundary
72+
cv2.circle(draw_img, (k[0], k[1]), radius_pupil+53, (255, 0, 0), 3)
73+
boundary.append(draw_img)
74+
centers.append([k[0],k[1],k[2]])
75+
return boundary,centers
76+
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# coding: utf-8
2+
3+
import cv2
4+
import numpy as np
5+
import glob
6+
import math
7+
from scipy.spatial import distance
8+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
9+
import matplotlib.pyplot as plt
10+
import pandas as pd
11+
from sklearn import metrics
12+
13+
def dim_reduction(feature_vector_train,feature_vector_test,components):
14+
'''TRAINING'''
15+
ft_train=feature_vector_train
16+
17+
#get the classes of all training feature vectors
18+
y_train=[]
19+
for i in range(0,108):
20+
for k in range(0,3):
21+
y_train.append(i+1)
22+
y_train=np.array(y_train)
23+
24+
#fit the LDA model on training data with n components
25+
sklearn_lda = LDA(n_components=components)
26+
sklearn_lda.fit(ft_train,y_train)
27+
28+
#transform the traning data
29+
red_train=sklearn_lda.transform(ft_train)
30+
31+
'''TESTING'''
32+
ft_test=feature_vector_test
33+
34+
#transform the testing data
35+
red_test=sklearn_lda.transform(ft_test)
36+
37+
38+
#get a list of predicted values for the testing data to calculate ROC
39+
y_pred=sklearn_lda.predict(ft_test)
40+
41+
#return transformed training and testing data, and the testing classes and predicted values for ROC
42+
return red_train,red_test
43+
44+
45+
def IrisMatching(feature_vector_train,feature_vector_test,components,flag):
46+
47+
#if flag is 1, we do not need to reduce dimesionality otherwise we call the dim_reduction function
48+
if flag==1:
49+
red_train=feature_vector_train
50+
red_test=feature_vector_test
51+
52+
elif flag==0:
53+
red_train,red_test=dim_reduction(feature_vector_train,feature_vector_test,components)
54+
55+
56+
arr_f=red_test #test
57+
arr_fi=red_train #train
58+
59+
index_L1=[]
60+
index_L2=[]
61+
index_cosine=[]
62+
min_cosine=[]
63+
64+
#this loop iterates over each test image
65+
for i in range(0,len(arr_f)):
66+
L1=[]
67+
L2=[]
68+
Cosine=[]
69+
70+
#this loop iterates over every training image - to be compared to each test image
71+
for j in range(0,len(arr_fi)):
72+
f=arr_f[i]
73+
fi=arr_fi[j]
74+
sumL1=0 #L1 distance
75+
sumL2=0 #L2 distance
76+
sumcos1=0
77+
sumcos2=0
78+
cosinedist=0 #cosine distance
79+
80+
#calculate L1 and L2 using the formulas in the paper
81+
for l in range(0,len(f)):
82+
sumL1+=abs(f[l]-fi[l])
83+
sumL2+=math.pow((f[l]-fi[l]),2)
84+
85+
86+
#calculate sum of squares of all features for cosine distance
87+
for k in range(0,len(f)):
88+
sumcos1+=math.pow(f[k],2)
89+
sumcos2+=math.pow(fi[k],2)
90+
91+
92+
#calculate cosine distance using sumcos1 and sumcos2 calculated above
93+
cosinedist=1-((np.matmul(np.transpose(f),fi))/(math.pow(sumcos1,0.5)*math.pow(sumcos2,0.5)))
94+
95+
L1.append(sumL1)
96+
L2.append(sumL2)
97+
Cosine.append(cosinedist)
98+
#get minimum values for L1 L2 and cosine distance for each test image and store their index
99+
index_L1.append(L1.index(min(L1)))
100+
index_L2.append(L2.index(min(L2)))
101+
index_cosine.append(Cosine.index(min(Cosine)))
102+
min_cosine.append(min(Cosine))
103+
104+
match=0
105+
count=0
106+
107+
#stores final matching - correct(1) or incorrect (0)
108+
match_L1=[]
109+
match_L2=[]
110+
match_cosine=[]
111+
match_cosine_ROC=[]
112+
113+
#calculating matching of the test set according to the ROC thresholds
114+
thresh=[0.4,0.5,0.6]
115+
116+
for x in range(0,len(thresh)):
117+
match_ROC=[]
118+
for y in range(0,len(min_cosine)):
119+
if min_cosine[y]<=thresh[x]:
120+
match_ROC.append(1)
121+
else:
122+
match_ROC.append(0)
123+
match_cosine_ROC.append(match_ROC)
124+
125+
126+
for k in range(0,len(index_L1)):
127+
'''count goes from 0 to 3 because we compare the indexes obtained for the first 4 images of the test data
128+
to the indexes of the first 3 images of
129+
the train data (for which match is incremented by 3 everytime count exceeds the value of 3)'''
130+
if count<4:
131+
count+=1
132+
else:
133+
match+=3
134+
count=1
135+
136+
'''check if matching is done correctly (1) or not (0) for L1 L2 and cosine distance and accordingly update
137+
the arrays match_L1,match_L2,match_cosine'''
138+
if index_L1[k] in range(match,match+3):
139+
match_L1.append(1)
140+
else:
141+
match_L1.append(0)
142+
if index_L2[k] in range(match,match+3):
143+
match_L2.append(1)
144+
else:
145+
match_L2.append(0)
146+
if index_cosine[k] in range(match,match+3):
147+
match_cosine.append(1)
148+
else:
149+
match_cosine.append(0)
150+
#reuturns the matching arrays, and test calsses and predicted values to calculate ROC
151+
return match_L1,match_L2,match_cosine,match_cosine_ROC
152+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# coding: utf-8
3+
import cv2
4+
import numpy as np
5+
import glob
6+
import math
7+
from scipy.spatial import distance
8+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
9+
import matplotlib.pyplot as plt
10+
import pandas as pd
11+
from sklearn import metrics
12+
13+
def IrisNormalization(boundary,centers):
14+
target = [img for img in boundary]
15+
normalized=[]
16+
cent=0
17+
for img in target:
18+
#load pupil centers and radius of inner circles
19+
center_x = centers[cent][0]
20+
center_y = centers[cent][1]
21+
radius_pupil=int(centers[cent][2])
22+
23+
iris_radius = 53 # width of space between inner and outer boundary
24+
25+
#define equally spaced interval to iterate over
26+
nsamples = 360
27+
samples = np.linspace(0,2*np.pi, nsamples)[:-1]
28+
polar = np.zeros((iris_radius, nsamples))
29+
for r in range(iris_radius):
30+
for theta in samples:
31+
#get x and y for values on inner boundary
32+
x = (r+radius_pupil)*np.cos(theta)+center_x
33+
y = (r+radius_pupil)*np.sin(theta)+center_y
34+
x=int(x)
35+
y=int(y)
36+
try:
37+
#convert coordinates
38+
polar[r][int((theta*nsamples)/(2*np.pi))] = img[y][x]
39+
except IndexError: #ignores values which lie out of bounds
40+
pass
41+
continue
42+
res = cv2.resize(polar,(512,64))
43+
normalized.append(res)
44+
cent+=1
45+
return normalized #returns a list of 64x512 normalized images

0 commit comments

Comments
 (0)