OpenCV Python – Add Trackbar
Trackbars in OpenCV allow you to create interactive sliders that can control values in real-time. They are very useful for adjusting image processing parameters like brightness, threshold, blur intensity, and more.
In this tutorial, you will learn how to create and use trackbars in OpenCV Python step by step.
1. What is a Trackbar in OpenCV?
A trackbar is a GUI slider that lets users change values dynamically while a program is running.
It is commonly used for:
- Adjusting image brightness
- Changing threshold values
- Controlling blur intensity
- Real-time parameter tuning
2. Import OpenCV
import cv2
import numpy as np
3. Create a Blank Window
Trackbars must be attached to a named window.
def nothing(x):
pass
cv2.namedWindow("Trackbars")
4. Create a Trackbar
Syntax:
cv2.createTrackbar(name, window, value, max_value, callback)
Example:
cv2.createTrackbar("Brightness", "Trackbars", 0, 100, nothing)
5. Using Trackbar Value
while True:
brightness = cv2.getTrackbarPos("Brightness", "Trackbars")
print("Brightness:", brightness)
if cv2.waitKey(1) & 0xFF == 27:
break
6. Full Example: Adjust Brightness in Real-Time
import cv2
import numpy as np
def nothing(x):
pass
img = np.zeros((400, 400, 3), dtype=np.uint8)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("Brightness", "Trackbars", 0, 255, nothing)
while True:
brightness = cv2.getTrackbarPos("Brightness", "Trackbars")
temp = img.copy()
temp[:] = brightness
cv2.imshow("Image", temp)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
7. Multiple Trackbars Example
cv2.createTrackbar("R", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("G", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("B", "Trackbars", 0, 255, nothing)
8. Real-Time Color Control Example
import cv2
import numpy as np
def nothing(x):
pass
img = np.zeros((300, 300, 3), np.uint8)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("R", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("G", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("B", "Trackbars", 0, 255, nothing)
while True:
r = cv2.getTrackbarPos("R", "Trackbars")
g = cv2.getTrackbarPos("G", "Trackbars")
b = cv2.getTrackbarPos("B", "Trackbars")
img[:] = [b, g, r]
cv2.imshow("Color Picker", img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
9. Why Trackbars are Important
Trackbars are widely used in:
- Image filtering tools
- AI parameter tuning
- Real-time video processing
- Computer vision debugging tools
- Interactive UI development
10. Common Mistakes
❌ Trackbar not working
✔ Solution:
- Ensure window is created first
cv2.namedWindow("Trackbars")
❌ No update in values
✔ Solution:
-
Use
cv2.getTrackbarPos()inside loop
11. Conclusion
Trackbars in OpenCV Python are powerful tools for creating interactive applications. They allow real-time control over image processing parameters, making debugging and experimentation much easier.
Once you master trackbars, you can build advanced tools like image editors and real-time AI control panels.


0 Comments