-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_trackbar.py
40 lines (30 loc) · 1 KB
/
08_trackbar.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
import cv2
import numpy as np
def do_nothing(x):
pass
# create black image, a window
img = np.zeros((300, 512, 3), np.uint8)
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('R', 'image', 0, 255, do_nothing)
cv2.createTrackbar('G', 'image', 0, 255, do_nothing)
cv2.createTrackbar('B', 'image', 0, 255, do_nothing)
# create switch for ON/OFF functionality
switch = '0 : OFF \n1: ON'
cv2.createTrackbar(switch, 'image', 0, 1, do_nothing)
while True:
cv2.imshow('switch ON and drag, press ESC to quit', img)
k = cv2.waitKey(1) & 0xFF
if k == 27: # exit w/ ESC key
break
# get current positions of four trackbars
r = cv2.getTrackbarPos('R', 'image')
g = cv2.getTrackbarPos('G', 'image')
b = cv2.getTrackbarPos('B', 'image')
s = cv2.getTrackbarPos(switch, 'image')
if s == 0: # if switch off, keep image black
img[:] = 0
else: # else, use BGR colors from trackbars
img[:] = [b, g, r]
cv2.destroyAllWindows()
cv2.waitKey(1)