-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkmeansDetect.py
More file actions
executable file
·161 lines (123 loc) · 4.41 KB
/
Copy pathkmeansDetect.py
File metadata and controls
executable file
·161 lines (123 loc) · 4.41 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
__author__ = 'arun'
import cv2
import math
import numpy as np
from optparse import OptionParser
from scipy.cluster import vq
from scipy.spatial import distance
from scipy import stats
def bestCluster(cluster, distCluster):
var = np.inf
id = 0
for i,group in enumerate(distCluster):
if group.shape[0]>clusterThresh:
if np.var(group) < var:
var = np.var(group)
id = i
#print var,stats.normaltest([distCluster[id]],None)
return var,id
def saturate(num,min,max):
if num<min:
num=min
elif num>max:
num=max
return num
def checkpoints(pnts):
global nclusters
distMat = distance.pdist(pnts)
newpnts = np.empty([1,2])
newpnts[0] = pnts[-1]
b = 0
for i in range(pnts.shape[0]-1):
a = b
b += (pnts.shape[0]-1) - i
if np.all(distMat[a:b]>distThresh):
newpnts = np.append(newpnts, [pnts[i]], axis=0)
nclusters = saturate(nclusters,minClusters,maxClusters)
if newpnts.shape[0] < nclusters:
for i in range(nclusters-newpnts.shape[0]):
while True:
randPnt = [(h-1)*np.random.randn(2)]
cdistMat = distance.cdist(randPnt,newpnts)
if np.all(cdistMat>distThresh):
newpnts = np.append(newpnts, randPnt, axis=0)
break
return newpnts
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-v", "--video", action="store", type="int", dest="video", default=0,
help="Enter video device number -- usually available at /dev/video")
(opts, args) = parser.parse_args()
cap = cv2.VideoCapture(opts.video)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.cv.CV_CAP_PROP_FPS, 30)
boundaries = [
([10, 10, 140], [150, 150, 255])
]
nfeatures = 500
distThresh = 200
minClusters = 2
maxClusters = 10
scaleFactor = 1.01
nclusters = minClusters
oldCenteroids = None
while True:
ret, frame = cap.read()
h,w,_ = frame.shape
h=int((1-0.65)*h)
frame = frame[h:,:,:]
frame = cv2.bilateralFilter(frame,5,75,75)
orb = cv2.ORB(int(nfeatures),scaleFactor)
kp = orb.detect(frame, None)
kp, des = orb.compute(frame, kp)
kpoints = np.asarray([point.pt for point in kp])
clusterThresh = 0.2*kpoints.shape[0]
try:
if oldCenteroids==None:
centers, distort = vq.kmeans(kpoints, nclusters, iter=10)
else:
centers, distort = vq.kmeans(kpoints, oldCenteroids)
except:
centers = oldcenters
kpoints = oldkpoints
pass
code, dist = vq.vq(kpoints, centers)
oldcenters = centers
oldkpoints = kpoints
cluster = []
distCluster = []
for i in range(nclusters):
cluster.append(kpoints[code==i])
distCluster.append(dist[code==i])
var, id = bestCluster(cluster, distCluster)
if var == np.inf:
nclusters -= 1
print centers.shape
newFrame = frame.copy()
newFrame = cv2.drawKeypoints(frame, kp, color=(0,255,0), flags=0)
clusterPoints = tuple(map(tuple, cluster[id].astype(int)))
cnt = np.array([map(tuple, cluster[id].astype(int))])
hull = cv2.convexHull(cnt)
M = cv2.moments(hull)
if M['m00'] != 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
if var < 1000:
cv2.drawContours(newFrame, [hull], 0, (255, 0 , 255), 2)
cv2.circle(newFrame, (cx, cy), 3, (255,0,0), 2)
if distance.euclidean(np.array([cx,cy]), centers[id]) > 0.1*distThresh:
nclusters += 1
for i,points in enumerate(clusterPoints):
cv2.circle(newFrame, points, 1, (255,0,255), 2)
oldCenteroids = centers
oldCenteroids = checkpoints(oldCenteroids)
centers = tuple(map(tuple, centers.astype(int)))
for i,center in enumerate(centers):
cv2.circle(newFrame, center, 2, (0,0,255), 2)
cv2.imshow("grayscale", newFrame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()