-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2.py
104 lines (90 loc) · 2.72 KB
/
v2.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
import cv2
import numpy as np
#to read image
img=cv2.imread('img20.......................................................................jpg',0)
h,w=img.shape
#resizing image with 200 width
r=200/w
img=cv2.resize(img,(200,np.int(h*r)))
origh,origw=img.shape
cv2.imshow('image',img )
cv2.waitKey(0)
#normalizing the gray scale matrix
img3=np.int64(img)+127
img3=np.minimum(img3,255)
img2=img3[:,100:200]
img4=img[:,100:200]
h,w=img2.shape
#finding the position of eyeball
maxdif = 0
for i in range(0,int(h/2)):
for j in range(1,w):
if (abs(np.int64(img2[i][j - 1]) - np.int64(img2[i][j])) >= maxdif):
maxdif = abs(np.int64(img2[i][j - 1]) - np.int64(img2[i][j]))
eye_r = i
eye_c=j
h,w=img.shape
#predicting the position of nose using geometric model
#eye_r contains row index of the position of eyeball
x1=int(eye_r-(0.15*w))
y1=int(0.32*w)
x2=int(x1+(0.55*w))
y2=int(y1+0.4*w)
img2=img[x1:x2,y1:y2]
up=x1
cv2.imshow('image',img2)
cv2.waitKey(0)
#removing the upper part of the predicted nose region
ret,th1 = cv2.threshold(img2,127,255,cv2.THRESH_BINARY)
hi,lo=np.unravel_index(th1.argmin(), th1.shape)
up=up+hi
img2=img2[hi:,:]
th1=th1[hi:,:]
h,w=th1.shape
#extracting the lower one third part of the nose where nosetrils are present
h=int(2*h/3)
up=up+h
img2=img2[h:,:]
img3=np.int64(img2)+127
th1=np.minimum(img3,255)
#finding the position of nosetrills
hi,lo=np.unravel_index(th1.argmin(), th1.shape)
up=up+hi
#extracting the lower part of face
up=int(up+0.05*origw)
img2=img[up:origh,:]
h2,w2=img2.shape
img3=np.int64(img2)+127
img3=np.minimum(img3,255)
h,w=img3.shape
#removing the background using histogram approach in vertical direction
maxdif=0
for i in range(int(h/2)-15,int(h/2)+5):
for j in range(1,40):
if (abs(np.int64(img3[i][j - 1]) - np.int64(img3[i][j])) > maxdif):
maxdif = abs(np.int64(img3[i][j - 1]) - np.int64(img3[i][j]))
ans = i
ans1=j
if (abs(np.int64(img3[i-1][j]) - np.int64(img3[i][j])) > maxdif):
maxdif = abs(np.int64(img3[i-1][j]) - np.int64(img3[i][j]))
ans = i
ans1=j
img2=img2[:,ans1:]
img3=np.int64(img2)+127
img3=np.minimum(img3,255)
h,w=img3.shape
maxdif=0
for i in range(int(h/2)-15,int(h/2)+5):
for j in range(w-40,w):
if (abs(np.int64(img3[i][j - 1]) - np.int64(img3[i][j])) >= maxdif):
maxdif = abs(np.int64(img3[i][j - 1]) - np.int64(img3[i][j]))
ans = i
ans1=j
if (abs(np.int64(img3[i-1][j]) - np.int64(img3[i][j])) > maxdif):
maxdif = abs(np.int64(img3[i-1][j]) - np.int64(img3[i][j]))
ans = i
ans1=j
img2=img2[:,:ans1]
#final image of the lower face
cv2.imshow('image',img2)
cv2.waitKey(0)