-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHSV Track Bar.py
88 lines (69 loc) · 2.36 KB
/
HSV 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2 as cv
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 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
image = cv.imread('sampl.jpg')
resized_image = cv.resize(image, (1200, 600))
plt.imshow(resized_image)
# cap = cv.VideoCapture(0)
# cap.set(3,480)
# cap.set(4,640)
# 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()
# lower_link = np.array([110,70,0])
# upper_link = np.array([129,255,255])
lower_link = np.array([80,80,0])
upper_link = np.array([89,165,255])