-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorPicker.py
51 lines (42 loc) · 1.45 KB
/
colorPicker.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
import cv2 as cv
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)
def empty(a):
pass
cv.namedWindow('HSV')
cv.resizeWindow('HSV', 640, 240)
cv.createTrackbar('HUE Min', 'HSV', 0, 179, empty)
cv.createTrackbar('SAT Min', 'HSV', 0, 255, empty)
cv.createTrackbar('VALUE Min', 'HSV', 0, 255, empty)
cv.createTrackbar('HUE Max', 'HSV', 179, 179, empty)
cv.createTrackbar('SAT Max', 'HSV', 255, 255, empty)
cv.createTrackbar('VALUE Max', 'HSV', 255, 255, empty)
while True:
_, img = cap.read()
imgHsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
h_min = cv.getTrackbarPos('HUE Min', 'HSV')
h_max = cv.getTrackbarPos('HUE Max', 'HSV')
s_min = cv.getTrackbarPos('SAT Min', 'HSV')
s_max = cv.getTrackbarPos('SAT Max', 'HSV')
v_min = cv.getTrackbarPos('VALUE Min', 'HSV')
v_max = cv.getTrackbarPos('VALUE Max', 'HSV')
lower = np.array([h_min, s_min, v_min])
upper = np.array([h_max, s_max, v_max])
mask = cv.inRange(imgHsv, lower, upper)
result = cv.bitwise_and(img, img, mask=mask)
mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
hStack = np.hstack([img, mask, result])
#cv.imshow('Original', img)
#cv.imshow('HSV color space', imgHsv)
#cv.imshow('Mask', mask)
#cv.imshow('Result', result)
cv.imshow('Horizontal Stacking', hStack)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()