-
Notifications
You must be signed in to change notification settings - Fork 39
/
Tracking.py
65 lines (53 loc) · 1.54 KB
/
Tracking.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
import cv2
import numpy as np
cam = cv2.VideoCapture(0)
hueHigh = 0
hueLow = 0
satHigh = 0
satLow = 0
valHigh = 0
valLow = 0
def hueh(val):
global hueHigh
hueHigh = val
def huel(val):
global hueLow
hueLow = val
def sath(val):
global satHigh
satHigh = val
def satl(val):
global satLow
satLow = val
def valh(val):
global valHigh
valHigh = val
def vall(val):
global valLow
valLow = val
cv2.namedWindow('My frame')
cv2.createTrackbar('hueL','My frame',0,180,huel)
cv2.createTrackbar('hueH','My frame',0,180,hueh)
cv2.createTrackbar('satL','My frame',0,255,satl)
cv2.createTrackbar('satH','My frame',0,255,sath)
cv2.createTrackbar('valL','My frame',0,255,vall)
cv2.createTrackbar('valH','My frame',0,255,valh)
while True:
ignore,frame = cam.read()
frameHSV = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lowerBound = np.array([hueLow,satLow,valLow])
upperBound = np.array([hueHigh,satHigh,valHigh])
myMask = cv2.inRange(frameHSV,lowerBound,upperBound)
contours,junk = cv2.findContours(myMask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if area >=1000:
# cv2.drawContours(frame,[contour],0,(255,0,0),3)
x,y,w,h=cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
mySelection = cv2.bitwise_and(frame,frame,mask=myMask)
cv2.imshow("My Selection",mySelection)
cv2.imshow("Frame",frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cam.release()