-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorDetection04.py
57 lines (41 loc) · 1.44 KB
/
colorDetection04.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
import cv2
import numpy as np
import imutils
def nothing(x):
pass
def main():
cap = cv2.VideoCapture(0)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("L - H", "Trackbars", 0, 179, nothing)
cv2.createTrackbar("L - S", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("L - V", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("U - H", "Trackbars", 179, 179, nothing)
cv2.createTrackbar("U - S", "Trackbars", 255, 255, nothing)
cv2.createTrackbar("U - V", "Trackbars", 255, 255, nothing)
while (cap.isOpened()):
ret, frame = cap.read()
if ret is True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_h = cv2.getTrackbarPos("L - H", "Trackbars")
lower_s = cv2.getTrackbarPos("L - S", "Trackbars")
lower_v = cv2.getTrackbarPos("L - V", "Trackbars")
upper_h = cv2.getTrackbarPos("U - H", "Trackbars")
upper_s = cv2.getTrackbarPos("U - S", "Trackbars")
upper_v = cv2.getTrackbarPos("U - V", "Trackbars")
lower_range = np.array([lower_h, lower_s, lower_v])
upper_range = np.array([upper_h, upper_s, upper_v])
mask = cv2.inRange(hsv, lower_range, upper_range)
result = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
# ESC to break
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
else:
continue
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()