-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline2.py
267 lines (173 loc) · 5.24 KB
/
line2.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import numpy as np
from collections import deque
import cv2
from Camera import Camera
VIDEO_SOURCE = 0
class ROI:
def __init__(self,center,size):
x = center[0]
y = center[1]
self.start = (x-size,y-size)
self.end = (x+size,y+size)
def drawBoundary(self,frame):
cv2.rectangle(frame,self.start,self.end,(0,255,0),1)
def getROI(self,frame):
return frame[ self.start[1]:self.end[1] , self.start[0]:self.end[0] ]
class Parameters:
def __init__(self,source=0):
# Selected base color
# fourth one is not used
self.hsv = (0,0,0,0)
self.hsv2 = (0,0,0,0)
self.hsv3 = (0,0,0,0)
# Thresholds
self.ht = 0
self.st = 180
self.vt = 200
self.ht_ = 0
self.st_ = 180
self.vt_ = 200
# Co-ords for the color selection square
# self.start = (0,0)
# self.end = (0,0)
self.center = (0,0)
# Boolean to check weather
# the colors are locked or not
self.locked = False
# Get the camera object
self.cam = Camera(source)
def findCenter(self):
# Read a frame
frame = self.cam.read()
# Calculate the co-ordinates for the center pixel
y = frame.shape[0]/2
x = frame.shape[1]/2
self.center = (x,y)
def threshChanged1(val):
params.ht = val
def threshChanged2(val):
params.st = val
def threshChanged3(val):
params.vt = val
def threshChanged4(val):
params.ht_ = val
def threshChanged5(val):
params.st_ = val
def threshChanged6(val):
params.vt_ = val
params = Parameters(VIDEO_SOURCE)
# get the co-ords for the square in the center
params.findCenter()
# Create an output window
cv2.namedWindow('controls')
# Create trackbars in the output window
cv2.createTrackbar('h+', 'controls',0,179,threshChanged1)
cv2.createTrackbar('s+', 'controls',0,255,threshChanged2)
cv2.createTrackbar('v+', 'controls',0,255,threshChanged3)
cv2.createTrackbar('h-', 'controls',0,179,threshChanged4)
cv2.createTrackbar('s-', 'controls',0,255,threshChanged5)
cv2.createTrackbar('v-', 'controls',0,255,threshChanged6)
# cam = cv2.VideoCapture(VIDEO_SOURCE)
# roihist = None
pts = deque(maxlen=100000)
def getMask(frame,hsv):
lower = np.array([0,0,0], dtype=np.uint8)
upper = np.array([0,0,0], dtype=np.uint8)
lower[0] = hsv[0]-params.ht_
lower[1] = hsv[1]-params.st_
lower[2] = 0
upper[0] = hsv[0]+params.ht
upper[1] = hsv[1]+params.st
upper[2] = 255
return cv2.inRange(frame,lower,upper)
reigon1 = ROI((params.center[0]+16,params.center[1]+16),10)
# reigon2 = ROI((params.center[0]+5,params.center[1]),5)
# reigon3 = ROI((params.center[0]-5,params.center[1]-5),5)
while(True):
# Read a frame from the camera
frame = params.cam.read()
# reigon2.drawBoundary(frame)
# Convert to hsv space
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
# hsv = frame
# # # Blur the image for filtering
cv2.medianBlur(hsv,5)
cv2.GaussianBlur(hsv,(5,5),0)
if not params.locked:
# Calculate average color
params.hsv = cv2.mean(reigon1.getROI(hsv))
# params.hsv2 = cv2.mean(reigon2.getROI(hsv))
# params.hsv3 = cv2.mean(reigon3.getROI(hsv))
mask = getMask(hsv,params.hsv)
# mask = mask + getMask(hsv,params.hsv2)
# mask = mask + getMask(hsv,params.hsv3)
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))
cv2.filter2D(mask,-1,disc,mask)
cv2.imshow('mask1',mask)
# cv2.imshow('mask2',mask2)
# cv2.imshow('mask3',mask3)
reigon1.drawBoundary(frame)
# reigon2.drawBoundary(frame)
# reigon3.drawBoundary(frame)
# cv2.imshow('original',frame)
_,contours,_ = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# get the contour with the greatest area
cnt=None
center = None
max_area = -1
ci = -1
for i in range(len(contours)):
cnt=contours[i]
area = cv2.contourArea(cnt)
if(area>max_area):
max_area=area
ci=i
if(ci != -1):
cnt=contours[ci]
# cv2.imshow('mask',mask)
# # Draw a rectangle in the center
# frame = cv2.rectangle(frame,params.start,params.end,(0,255,0),1)
if(ci != -1):
# Find and draw the hull around the largest contour
hull = cv2.convexHull(cnt)
cv2.drawContours(frame,[hull],0,(0,255,0),2)
# only proceed if at least one contour was found
if len(contours) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c=np.array(cnt)
#print(c)
#c = max(cnt, key=cv2.contourArea)
M = cv2.moments(c)
center = (int(M["m10"]/ M["m00"] ), int(M["m01"]/ M["m00"]))
# update the points queue
if(params.locked is True):
pts.appendleft(center)
# loop over the set of tracked points
for i in xrange(1, len(pts)):
# if either of the tracked points are None, ignore
# them
if pts[i - 1] is None or pts[i] is None:
continue
# otherwise, compute the thickness of the line andq
# draw the connecting lines
#thickness = float(np.sqrt(100000 / float(i + 1)) * 2.5)
if(params.locked is True):
cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), 2)
# show the frame to our screen
cv2.imshow("Frame", cv2.flip(frame,1))
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
elif k == ord('l'):
print 'locked'
params.locked = True
elif k == ord('u'):
print 'un-locked'
params.locked = False
elif k == ord('c'):
print 'mask captured'
cv2.imwrite('mask.png',mask)
params.cam.stop()
cv2.destroyAllWindows()