-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30 Detecting Color Using webcam and track bar.py
57 lines (40 loc) · 1.61 KB
/
30 Detecting Color Using webcam and track bar.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
"""
@author: Osama Shakeel
Detecting Color Using webcam and track bar
"""
import cv2
import numpy as np
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
#Binding Color Trackbars with image
def nothing(x):
pass
cv2.namedWindow("Color Adjustments")
cv2.createTrackbar("Lower_H", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Lower_S", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Lower_V", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Upper_H", "Color Adjustments", 255, 255, nothing)
cv2.createTrackbar("Upper_S", "Color Adjustments", 255, 255, nothing)
cv2.createTrackbar("Upper_V", "Color Adjustments", 255, 255, nothing)
while True:
_, frame = cap.read()
frame = cv2.resize(frame,(400,400))
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
l_h = cv2.getTrackbarPos("Lower_H", "Color Adjustments")
l_s = cv2.getTrackbarPos("Lower_S", "Color Adjustments")
l_v = cv2.getTrackbarPos("Lower_V", "Color Adjustments")
u_h = cv2.getTrackbarPos("Upper_H", "Color Adjustments")
u_s = cv2.getTrackbarPos("Upper_S", "Color Adjustments")
u_v = cv2.getTrackbarPos("Upper_V", "Color Adjustments")
lower_bound = np.array([l_h, l_s, l_v])
upper_bound = np.array([u_h, u_s, u_v])
#Creating Mask
mask = cv2.inRange(hsv, lower_bound, upper_bound)
#filter mask with image
res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
cv2.imshow("res", res)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()