-
Notifications
You must be signed in to change notification settings - Fork 3
/
sift_extractors.py
54 lines (32 loc) · 1.15 KB
/
sift_extractors.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
import cv2
from sklearn.cluster import KMeans
import pickle
from scipy.spatial.distance import cdist
import os
import numpy as np
def extract_sift_features(list_image):
image_descriptors = []
sift = cv2.xfeatures2d.SIFT_create()
for image in list_image:
_, descriptor = sift.detectAndCompute(image, None)
image_descriptors.append(descriptor)
return image_descriptors
def kmean_bow(all_descriptors, num_cluster):
bow_dict = []
kmeans = KMeans(n_clusters = num_cluster)
kmeans.fit(all_descriptors)
bow_dict = kmeans.cluster_centers_
if not os.path.isfile('bow_dictionary.pkl'):
pickle.dump(bow_dict, open('bow_dictionary.pkl', 'wb'))
return bow_dict
def create_feature_bow(image_descriptors, BoW, num_cluster):
X_features = []
for i in range(len(image_descriptors)):
features = np.array([0] * num_cluster)
if image_descriptors[i] is not None:
distance = cdist(image_descriptors[i], BoW)
argmin = np.argmin(distance, axis = 1)
for j in argmin:
features[j] += 1
X_features.append(features)
return X_features