-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCentroids.py
34 lines (31 loc) · 1.33 KB
/
getCentroids.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
import os
import cv2
import pickle
import numpy as np
def main(detectorName, dictionarySize, descriptors):
print("Currently in centroid function")
p = "Centroids/" + detectorName + ".txt"
if not os.path.isfile(p):
descriptors = np.float32(descriptors)
# Clustering
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 0.01)
# Apply KMeans with flag cv2.KMEANS_RANDOM_CENTERS (Just to avoid line break in the code)
flags = cv2.KMEANS_PP_CENTERS
# desc is a type32 numpy array of vstacked descriptors
print("Kmeans has started, wait a minute")
compactness, labels, centroids = cv2.kmeans(descriptors, dictionarySize, None, criteria, 1, flags)
# Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
# My criteria is such that, whenever 10 iterations of algorithm is ran,
# or an accuracy of epsilon = 1.0 is reached, stop the algorithm and return the answer.
directory = "Centroids/"
if not os.path.exists(directory):
os.makedirs(directory)
f = open(p, "wb")
pickle.dump(centroids, f)
f.close()
else:
print("Importing centroids from: " + p)
f = open(p, "rb")
centroids = pickle.load(f)
f.close()
return centroids