1Trackbar as the Color Palette {#tutorial_py_trackbar}
2=============================
3
4Goal
5----
6
7-   Learn to bind trackbar to OpenCV windows
8-   You will learn these functions : **cv.getTrackbarPos()**, **cv.createTrackbar()** etc.
9
10Code Demo
11---------
12
13Here we will create a simple application which shows the color you specify. You have a window which
14shows the color and three trackbars to specify each of B,G,R colors. You slide the trackbar and
15correspondingly window color changes. By default, initial color will be set to Black.
16
17For cv.createTrackbar() function, first argument is the trackbar name, second one is the window
18name to which it is attached, third argument is the default value, fourth one is the maximum value
19and fifth one is the callback function which is executed every time trackbar value changes. The
20callback function always has a default argument which is the trackbar position. In our case,
21function does nothing, so we simply pass.
22
23Another important application of trackbar is to use it as a button or switch. OpenCV, by default,
24doesn't have button functionality. So you can use trackbar to get such functionality. In our
25application, we have created one switch in which application works only if switch is ON, otherwise
26screen is always black.
27@code{.py}
28import numpy as np
29import cv2 as cv
30
31def nothing(x):
32    pass
33
34# Create a black image, a window
35img = np.zeros((300,512,3), np.uint8)
36cv.namedWindow('image')
37
38# create trackbars for color change
39cv.createTrackbar('R','image',0,255,nothing)
40
41cv.createTrackbar('G','image',0,255,nothing)
42cv.createTrackbar('B','image',0,255,nothing)
43
44# create switch for ON/OFF functionality
45switch = '0 : OFF \n1 : ON'
46cv.createTrackbar(switch, 'image',0,1,nothing)
47
48while(1):
49    cv.imshow('image',img)
50    k = cv.waitKey(1) & 0xFF
51    if k == 27:
52        break
53
54    # get current positions of four trackbars
55    r = cv.getTrackbarPos('R','image')
56    g = cv.getTrackbarPos('G','image')
57    b = cv.getTrackbarPos('B','image')
58    s = cv.getTrackbarPos(switch,'image')
59
60    if s == 0:
61        img[:] = 0
62    else:
63        img[:] = [b,g,r]
64
65cv.destroyAllWindows()
66@endcode
67The screenshot of the application looks like below :
68
69![image](images/trackbar_screenshot.jpg)
70
71Exercises
72---------
73
74-#  Create a Paint application with adjustable colors and brush radius using trackbars. For drawing,
75    refer previous tutorial on mouse handling.
76