Programming, Python, Tutorials

Trackbar in OpenCV Python

There are times when you want to run the program with different value or parameter and experiment to find the optimum value to figure out the best parameter value.

Editing the variable value in source code and running the program, again and again, can be tedious, instead, you can bind trackbars to the output image window.

These trackbars will either call a function or update variable value based on slider position

In this tutorial, we will learn how to make trackbars and use them for experimentation with examples code

  1. What is a trackbar
  2. Trackbars in OpenCV
  3. cv2.createTrackbar() arguments
  4. cv2.getTrackbarPos() arguments
  5. OpenCV Trackbar Example Code
  6. Using the trackbar callback function
  7. OpenCV Trackbar as on/off switch or button
  8. More Example of opencv trackbar

What is a trackbar?

Trackbar is a GUI element that let the user to select a specific value within a range of values by sliding a slider linearly. It’s similar to scrolling but it limits the user to select a specific value with its minimum and maximum limits.

Trackbars in OpenCV

Trackbars in OpenCV are helpful to tweak a variable value instantly without closing and relaunching the program.

To create a trackbar in OpenCV the OpenCV library provides cv2.createTrackbar() function, to read the current poisition of the trackbar slider you can use cv2.getTrackbarPos() function to change the position of trackbar use cv2.setTrackbarPos().

trackbar slider
Opencv Trackbar slider

cv2.createTrackbar() arguments

  1. Trackbar name
  2. Window name
  3. Default slider value
  4. Maximum value
  5. Callback function

cv2.getTrackbarPos() arguments

  1. Trackbar name
  2. Window name

cv2.setTrackbarPos() arguments

  1. Trackbar name
  2. Window name
  3. New Value

OpenCV Trackbar Example Code

For this example, we will be making a simple program using OpenCV tracker to change the radius of a circle.

For every trackbar there are 2 necessary requirement

  1. a window which can contain the trackbar
  2. a callback function

Flow of the sample trackbar program

  1. Import necessary packages
    • cv2 and numpy
  2. Define a function which can be used as call back function for the trackbar
    • def nothing
  3. create a window ‘controls’ to contain the trackbar
    • cv2.namedWindow(‘controls’)
  4. create a trackbar ‘r’ in ‘controls’ window
    • cv2.createTrackbar(‘r’,’controls’,15,255,nothing)
  5. create a while loop
  6. Create a black image
    • img = np.zeros((512,512,3), np.uint8)
    • calculate center of the image for circle center
  7. create a radius varible
    • use cv2.getTrackbarPos(‘r’,’controls’) to assign current trackbar position value to radius variable
  8. draw a circle in the center of the black image with radius ‘radius’
    •,(img_center_y,img_center_x), radius, (0,0,255), -1)
  9. show the black imge
    • cv2.imshow(‘img’,img)
  10. use waitkey to add delay and stop the function when the user presses esc key
    • k = cv2.waitKey(1) & 0xFF
  11. finnaly destroy all widows after exiting the while loop
    • cv2.destroyAllWindows()
Simple trackbar opencv
Simple trackbar opencv

Using the trackbar callback function

The callback function used in the OpenCV trackbar function is called every time the user moves the slider.

The below program changes the color of the circle from ‘red’ to ‘blue’ if the trackbar slider value is >127.

The change_color callback function checks the slider value and changes the global color value to blue as soon as the slider value is above 127.

OpenCV Trackbar as on/off switch or button

A trackbar can be modified to be used as a switch by limiting the slider range to just 2 numbers (0-1).

cv2.createTrackbar(‘r’,’controls’,0,1,change_color) will convert the slider into a switch.

The below program draws a circle if the switch slider is 0 (off) and draws a square if switch slider is on (1)

trackbar switch opencv
trackbar switch opencv

Explore more trackbar example:

  • HSV color trackbar
  • RGB color palette trackbar
  • Image blur trackbar
  • Brightness, and Contrast trackbar.

Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube , Linked In , Instagram

Stay updated with the latest advancements Explore event, webinars and conferences