-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractFeatures.py
69 lines (46 loc) · 1.56 KB
/
extractFeatures.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
import cv2
import numpy as np
import math
def extractFeatures(bwimage):
"""
Given an image, returns a feature array containing
circularity, elongation, and principal axes.
"""
# circularity
img = bwimage.copy()
img1, contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
if len(contours)==0:
return []
B = contours[0]
C = B[:,0,0]
l = C.size
if abs(B[0,0,0] - B[l-1,0,0]) + abs(B[0,0,1] - B[l-1,0,1]) == 2:
P8 = math.sqrt(2)
else:
P8 = 1
for j in range(0,l-1):
if abs((B[j+1,0,0] - B[j,0,0])) + abs(B[j+1,0,1] - B[j,0,1]) == 2:
P8 = P8 + math.sqrt(2)
else:
P8 = P8 + 1
n = np.count_nonzero(bwimage)
circularity = P8*P8/n
# elongation
idx = np.nonzero(bwimage);
c = idx[1]
r = idx[0]
meanx = np.mean(c)
meany = np.mean(r)
pows = 2*np.ones(n)
sigxx = np.sum(np.power((c-meanx),pows))/n
sigyy = np.sum(np.power((r-meany),pows))/n
sigxy = np.sum(np.multiply((r-meany),(c-meanx)))/n
covMat = np.array([[sigxx, sigxy], [sigxy, sigyy]])
val, vects = np.linalg.eig(covMat);
maxEigenValue = np.amax(val)
minEigenValue = np.amin(val.ravel()[np.flatnonzero(val)])
elongation = math.sqrt(maxEigenValue/minEigenValue);
# principal axis
maxidx = np.argmax(val)
principalAxisVector = vects[maxidx]
return [circularity, elongation, principalAxisVector]