-
Notifications
You must be signed in to change notification settings - Fork 2
/
object_tracking_by_color.py
53 lines (41 loc) · 1.17 KB
/
object_tracking_by_color.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
import cv2 as cv
import numpy as np
import imutils
def main():
cap = cv.VideoCapture(0)
if cap.isOpened():
ret , frame = cap.read()
else:
ret = False
while ret:
ret, frame = cap.read()
frame = imutils.resize(frame, width=450)
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# blur range
b_low = np.array([100,50,50])
b_high = np.array([140,255,255])
# green range
sensitivity = 15;
g_low = np.array([40, 100, 50])
g_high = np.array([100, 255, 255])
# red range
r_low = np.array([170,25,20])
r_high = np.array([179,255,255])
# make mask
img_mask_b = cv.inRange(hsv,b_low,b_high)
img_mask_g = cv.inRange(hsv,g_low,g_high)
img_mask_r = cv.inRange(hsv,r_low,r_high)
# bitwise operation
output_b = cv.bitwise_and(frame,frame,mask=img_mask_b)
output_g = cv.bitwise_and(frame,frame,mask=img_mask_g)
output_r = cv.bitwise_and(frame,frame,mask=img_mask_r)
cv.imshow('blue',output_b)
cv.imshow('green',output_g)
cv.imshow('red',output_r)
cv.imshow('original',frame)
if cv.waitKey(1) == ord('q'):
break
cv.destroyAllWindows()
cap.release()
if __name__ == '__main__':
main()