A Trackbar, or a Slider, is a graphical user interface component that allows the user to adjust a parameter within a range by sliding a knob with the mouse along a horizontal bar, as shown in Figure below. It is commonly used in computer vision applications for real-time parameter tuning, for example, the users can adjust brightness from the range of 0 to 100 by dragging the knob along the bar.
In OpenCV, cv2.createTrackbar() function is used to create a trackbar. The function takes several arguments, including the name of the trackbar, the name of the window in which it will appear, the initial value of the trackbar, the maximum value, and a callback function that will be called when the trackbar is moved. When a user moves the knob the callback function will be invoked, and the actions defined in the callback function will be performed.
Here are the code snippets to create a sample trackbar -
The range of the trackbar is from 0 to a maximum value of 100, a callback function on_trackbar()is called whenever the knob is moved by the mouse, and it simply prints the current value of the trackbar to the console.
The trackbar is displayed in a window named "Trackbar Window", which is created using the cv2.namedWindow() function, the content of the window is a light gray canvas as we did previously. Inside the main loop, cv2.imshow() is called to display the trackbar window and cv2.waitKey() to wait for a key press. When the user presses the ESC key, it breaks out of the loop and destroys the window using cv2.destroyAllWindows().
When the user interacts with the trackbar by moving the knob, the on_trackbar() function is called with the current value of the trackbar as its parameter. This value can then be used to adjust a parameter in real-time, such as the brightness for an image processing algorithm.
The below example will display a webcam, together with four trackbars for adjusting the brightness, contrast, saturation and hue of the webcam.
Open the UserInputWithTrackbar.py file in the Github repository, the four callback functions are defined below:
1 import cv2
2
3 def change_brightness(value):
4 global cap
5 print("Brightness: " + str(value))
6 cap.set(cv2.CAP_PROP_BRIGHTNESS, value)
7
8 def change_contrast(value):
9 print("Contrast: " + str(value))
10 cap.set(cv2.CAP_PROP_CONTRAST, value)
11
12 def change_saturation(value):
13 print("Saturation: " + str(value))
14 cap.set(cv2.CAP_PROP_SATURATION, value)
15
16 def change_hue(value):
17 print("Hue: " + str(value))
18 cap.set(cv2.CAP_PROP_HUE, value)
Then, define the main function to setup the trackbars and display the webcam-
20 def main():
21 global cap
22
# read from webcam
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
23
# set width
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
24
# set height
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
25
# set initial brightness
cap.set(cv2.CAP_PROP_BRIGHTNESS, 100)
26
# set initial contrast
cap.set(cv2.CAP_PROP_CONTRAST, 50)
27
# set initial saturation
cap.set(cv2.CAP_PROP_SATURATION, 90)
28
# set initial hue
cap.set(cv2.CAP_PROP_HUE, 15)
29
30 cv2.namedWindow('Webcam')
31
cv2.createTrackbar('Brightness', 'Webcam', 100,
300, change_brightness)
32
cv2.createTrackbar('Contrast', 'Webcam', 50, 300,
change_contrast)
33
cv2.createTrackbar('Saturation', 'Webcam', 90,
100, change_saturation)
34
cv2.createTrackbar('Hue', 'Webcam', 15, 360,
change_hue)
35
36 success, img = cap.read()
37 while success:
38 cv2.imshow("Webcam", img)
39
40 # Press ESC key to break the loop
41 if cv2.waitKey(10) & 0xFF == 27:
42 break
43 success, img = cap.read()
44
45 cap.release()
46 cv2.destroyWindow("Webcam")
47
48 if __name__ == '__main__':
49 main()
Execute the code, there are four trackbars shown together with the webcam window, for adjusting brightness, contrast, saturation and hue. The below Figure only shows the trackbars’ area, the webcam screen also shows in the window. You will see the camera screen changes accordingly when each slider is moved by the mouse.
0 comments:
Post a Comment