/
utilities.py
66 lines (44 loc) · 1.6 KB
/
utilities.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
from skimage.filters import threshold_otsu, threshold_li, threshold_yen
from skimage.measure import label, regionprops, moments, moments_central, moments_normalized, moments_hu
import cv2
import numpy as np
from skimage import io
from collections import Counter
num_features = 12
knn_neighbors = 5
def get_threshold(image_path):
img = cv2.imread(image_path,0)
blur = cv2.GaussianBlur(img,(3,3),0)
tmp,img_binary = cv2.threshold(blur,127,255,cv2.THRESH_BINARY)
kernel = np.ones((3,3),np.uint8)
# Applying Errosion and Dilation on the image
closing = cv2.morphologyEx(~img_binary, cv2.MORPH_CLOSE, kernel)
return closing
def classify(D_index, train_labels):
# nearest neighbor
# k-NN
pred_labels_test = []
for idx,row in enumerate(D_index):
k_labels = [train_labels[row[i]] for i in range(0,knn_neighbors)]
most_common,num_most_common = Counter(k_labels).most_common(1)[0]
pred_labels_test.append(most_common)
return pred_labels_test
def extract_features(roi, props):
features = []
m = moments(roi)
# print(m)
cr = m[0, 1] / m[0, 0]
cc = m[1, 0] / m[0, 0]
mu = moments_central(roi, (cr, cc))
nu = moments_normalized(mu)
#finding Seven Features
hu = moments_hu(nu)
# seven features to be put into feature list
features.extend(hu)
# print(features)
features.append(roi.shape[1]/roi.shape[0])
features.append(props.eccentricity)
features.append(props.convex_area/props.area)
features.append(props.orientation)
features.append(props.euler_number)
return np.array([features])