Post

How to handle Data, and Images(9) OpenCV Trackbar

How to create a Trackbar, and position Trackbar

Lesson Notes in .ipynb file

How to handle Data, and Images(9) - OpenCV Trackbar

Topics

OpenCV Trackbar

cv2.createTrackbar(track_bar, name, window_name, value, count, on_change): Creates trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position synchronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change.

  • value: pointer to an integer variable whose value reflects the position of the slider.
  • count: Maximal position of the slider.
  • on_Change: Pointer to the function to be called every time the slider changes position.

cv2.getTrackbarPos(track_bar, name, window_name): The function returns the current position of the specified trackbar.

This code below should be ran in separate IDE (VS Code, Pycharm)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import cv2
import numpy as np

def change_color(x):
  r = cv2.getTrackbarPos("R", "Image")
  g = cv2.getTrackbarPos("G", "Image")
  b = cv2.getTrackbarPos("B", "Image")

  image[:] = [b,g,r]
  cv2.imshow("Image", image)

image = np.zeros((600, 400, 3), np.uint8)
cv2.namedWindow("Image")

cv2.createTrackbar("R", "Image", 0, 255, change_color)
cv2.createTrackbar("G", "Image", 0, 255, change_color)
cv2.createTrackbar("B", "Image", 0, 255, change_color)

cv2.imshow("Image", image)
cv2.waitKey(0)

Once you run the program, you will see a window called ‘Image’ pop up, and and controllable Trackbar above like image below and you can change the color of these by using the Tracker

Base programR,G,B = 255,0,0R,G,B = 0, 255, 0
Desktop ViewDesktop ViewDesktop View
R,G,B = 0, 0, 255R,G,B = 128, 87, 102
Desktop ViewDesktop View

Summary

  • cv2.createTrackbar(): Creates trackbar
  • cv2.getTrackbarPos(): The function returns the current position of the specified trackbar.
This post is licensed under CC BY 4.0 by the author.