-
Notifications
You must be signed in to change notification settings - Fork 1
/
sift_feature.py
100 lines (89 loc) · 3.18 KB
/
sift_feature.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
import numpy as np
import cv2
from utils import *
import matplotlib.pyplot as plt
def get_sift(img, kp = [], mode = "gray", mask = None):
img = denormalize_image(img)
img = np.array(img*255, dtype="uint8")
if mode == "gray":
gray= cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp,des = sift.compute(gray,kp)
return des.flatten()
# img=cv2.drawKeypoints(gray,kp, None)
# cv2.imshow('result', img), cv2.waitKey(0)
# cv2.destroyWindow("result")
elif mode == "color":
# image chnnel mask
hist1 = cv2.calcHist([img],[0],mask,[32],[0,256]).T[0]
hist2 = cv2.calcHist([img],[1],mask,[32],[0,256]).T[0]
hist3 = cv2.calcHist([img],[2],mask,[32],[0,256]).T[0]
# plt.hist(gray_img.ravel(),256,[0,256])
# plt.title('Histogram for gray scale picture')
# plt.show()
return np.append(np.append(hist1, hist2), hist3)
def generate_kp(features_line):
kp = []
for i in range(int(len(features_line)/2)):
left_eye = np.array([features_line[0], features_line[1]])
right_eye = np.array([features_line[2], features_line[3]])
nose = np.array([features_line[4], features_line[5]])
face_line = left_eye - right_eye
kp_scale = np.linalg.norm(face_line)/4
kp_angle = np.degrees(np.arctan2(face_line[1], face_line[0]))
for i in range(8):
tmp_x = features_line[2*i]
tmp_y = features_line[2*i+1]
kp.append(cv2.KeyPoint(tmp_x, tmp_y, _size=kp_scale, _angle=kp_angle))
# mask
center = (left_eye + right_eye + nose)/3
face_scale = np.linalg.norm(face_line)/3
mask = np.zeros((128,128), dtype="uint8")
col_min = np.maximum(int(center[0]-face_scale), 0)
col_max = np.minimum(int(center[0]+face_scale), 127)
row_min = np.maximum(int(center[1]-face_scale), 0)
row_max = np.minimum(int(center[1]+face_scale), 127)
mask[row_min:row_max,col_min:col_max] = 1
return kp, mask
def get_new_feature(X, feature):
new_features = []
for x_row, f_row in zip(X, feature):
kp, mask = generate_kp(f_row)
sift_vec = get_sift(x_row, kp = kp)
hist_vec = get_sift(x_row, mode = "color", mask = mask)
# sift_vec = sift_vec/np.sum(sift_vec)*5
hist_vec = hist_vec/np.sum(hist_vec)
# concatnate them to be the new feature.
new_features.append( np.append(sift_vec, hist_vec) )
return new_features
def visualize_face(image, feature):
"""
Visualize face of dog.
Input: image - a single image. e.g. x_train[0]
feature - feature points for a single data entry.
e.g. features_train[0]
"""
_image = [image]
_feature = [feature]
for x_row, f_row in zip(_image, _feature):
kp, mask = generate_kp(f_row)
left_top = [-1, -1]
right_bottom = [-1, -1]
for r in range(len(mask)):
for c in range(len(mask[r])):
if mask[r][c] == 1 and left_top == [-1, -1]:
left_top = [r, c]
elif mask[r][c] == 0 and left_top != [-1, -1]:
right_bottom = [-1, c]
break
if right_bottom != [-1, -1]:
break
for r in range(len(mask)):
if mask[r][left_top[1]] == 0 and r > left_top[0]:
right_bottom[0] = r
break
image_found = cv2.rectangle(denormalize_image(image), (left_top[1], left_top[0]),
(right_bottom[1], right_bottom[0]), (0, 0, 100), 1)
plt.imshow(image_found)
plt.scatter(x=feature[::2], y=feature[1::2], c='r', s=60)
plt.show()