-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetColor.py
48 lines (32 loc) · 1.35 KB
/
setColor.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
import cv2
import numpy as np
def nothing(x):
pass
cap = cv2.VideoCapture(0)
cv2.namedWindow("test") # creates empty window for trackbars
cv2.createTrackbar("l_h", "test", 98, 255, nothing) # creates trackbars
cv2.createTrackbar("l_s", "test", 185, 255, nothing)
cv2.createTrackbar("l_v", "test", 67, 255, nothing)
cv2.createTrackbar("u_h", "test", 120, 255, nothing)
cv2.createTrackbar("u_s", "test", 255, 255, nothing)
cv2.createTrackbar("u_v", "test", 206, 255, nothing)
while True:
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # converts BGR feed to HSV
lH = cv2.getTrackbarPos("l_h", "test") # gets trackbar position
lS = cv2.getTrackbarPos("l_s", "test")
lV = cv2.getTrackbarPos("l_v", "test")
uH = cv2.getTrackbarPos("u_h", "test")
uS = cv2.getTrackbarPos("u_s", "test")
uV = cv2.getTrackbarPos("u_v", "test")
lowerBlue = np.array([lH, lS, lV]) # sets the hsv value derived from trackbar
upperBlue = np.array([uH, uS, uV])
mask = cv2.inRange(hsv, lowerBlue, upperBlue) # creates mask for cloak
res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow("final", res)
cv2.imshow("frame",frame)
cv2.imshow("mask", mask)
k = cv2.waitKey(1)
if k == ord("q"):
break
cv2.destroyAllWindows()