-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHSV Trackbar.py
More file actions
72 lines (58 loc) · 2 KB
/
HSV Trackbar.py
File metadata and controls
72 lines (58 loc) · 2 KB
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
66
67
68
69
70
71
72
import cv2 as cv
import numpy as np
# optional argument for trackbars
def nothing(x):
pass
# named ites for easy reference
barsWindow = 'Bars'
hl = 'H Low'
hh = 'H High'
sl = 'S Low'
sh = 'S High'
vl = 'V Low'
vh = 'V High'
# set up for video capture on camera 0
cap = cv.VideoCapture(0)
# create window for the slidebars
cv.namedWindow(barsWindow, flags = cv.WINDOW_AUTOSIZE)
# create the sliders
cv.createTrackbar(hl, barsWindow, 0, 179, nothing)
cv.createTrackbar(hh, barsWindow, 0, 179, nothing)
cv.createTrackbar(sl, barsWindow, 0, 255, nothing)
cv.createTrackbar(sh, barsWindow, 0, 255, nothing)
cv.createTrackbar(vl, barsWindow, 0, 255, nothing)
cv.createTrackbar(vh, barsWindow, 0, 255, nothing)
# set initial values for sliders
cv.setTrackbarPos(hl, barsWindow, 0)
cv.setTrackbarPos(hh, barsWindow, 179)
cv.setTrackbarPos(sl, barsWindow, 0)
cv.setTrackbarPos(sh, barsWindow, 255)
cv.setTrackbarPos(vl, barsWindow, 0)
cv.setTrackbarPos(vh, barsWindow, 255)
while(True):
ret, frame = cap.read()
frame = cv.GaussianBlur(frame, (5, 5), 0)
# convert to HSV from BGR
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# read trackbar positions for all
hul = cv.getTrackbarPos(hl, barsWindow)
huh = cv.getTrackbarPos(hh, barsWindow)
sal = cv.getTrackbarPos(sl, barsWindow)
sah = cv.getTrackbarPos(sh, barsWindow)
val = cv.getTrackbarPos(vl, barsWindow)
vah = cv.getTrackbarPos(vh, barsWindow)
# make array for final values
HSVLOW = np.array([hul, sal, val])
HSVHIGH = np.array([huh, sah, vah])
# apply the range on a mask
mask = cv.inRange(hsv, HSVLOW, HSVHIGH)
maskedFrame = cv.bitwise_and(frame, frame, mask = mask)
# display the camera and masked images
cv.imshow('Masked', maskedFrame)
cv.imshow('Camera', frame)
# check for q to quit program with 5ms delay
if cv.waitKey(5) & 0xFF == ord('q'):
break
# clean up our resources
cap.release()
cv.destroyAllWindows()