-
Notifications
You must be signed in to change notification settings - Fork 11
/
10_Trackerbar_RGB_Chanels.py
51 lines (38 loc) · 1.12 KB
/
10_Trackerbar_RGB_Chanels.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
"""
Learn RGB channels with trackerbar
"""
# Import the packages
import cv2 as cv
import numpy as np
# Define the trackbar callback function
def nothing(x):
print(x)
# Create a black image, a window
img = np.zeros((512, 512, 3), np.uint8)
cv.namedWindow('image', cv.WINDOW_NORMAL)
# Create trackbars for color change
cv.createTrackbar('B', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('R', 'image', 0, 255, nothing)
# Create switch for ON/OFF functionality
switch = '0 : OFF, 1 : ON'
cv.createTrackbar(switch, 'image', 0, 1, nothing)
while (1):
# Show the image
cv.imshow('image', img)
# Wait 1ms until the ESC button pressed
k = cv.waitKey(1) & 0xFF
if k == 27:
break
# Get the current positions of trackbars
b = cv.getTrackbarPos('B', 'image')
g = cv.getTrackbarPos('G', 'image')
r = cv.getTrackbarPos('R', 'image')
s = cv.getTrackbarPos(switch, 'image')
# Switch the mode: show color or not
if s == 0:
img[:] = 0
else:
img[:] = [b, g, r]
# Destroy all the windows opened before
cv.destroyAllWindows()