Trackbar OpenCV Python

opencv trackbar python

Today we will make Trackbar in OpenCV Python. You can also call it Sliders. We will be creating three trackbars / Sliders in OpenCV basically for RGB ( Red, Green, and Blue) color channels.

Let’s Code it!

Trackbar OpenCV Algorithm

Installing Packages

Install OpenCV, as its the most important package for computer vision in Python.

pip install opencv-python

If you have installed OpenCV, No need to install NumPy as it already comes pre-packed with OpenCV. Otherwise,

pip install numpy

Install Matplotlib for the Visualization of data. (Optional)

pip install matplotlib

Importing Packages

We will need the Computer Vision package for creating the window and showing the output and the NumPy package for internally creating the color.

import cv2 as cv

import numpy as np

Learn OpenCV basics and Image Manipulation.

TrackBar OpenCV Python

Define trackbar() function and the whole logic of creating a named window and mixing up of colors will be done inside it.

NumPy Zeros

This will create a black image of 300 x 512 size, with the data type of an unsigned integer of 8 bits. The black image will be stored in the IMG variable.

np.zeros() takes 2 parameters:

  1. The shape of the matrix
  2. The data type of the value of the matrix

This will create a 300 x 512 matrix, where all elements will be 0. The number of color channels is three. Later the zero’s will be filled by the specific trackbar values.

img = np.zeros((300, 512, 3), np.uint8)

OpenCV Named Window

Create a Named window that will provide residence to all the trackbars and the switches.

cv.namedWindow(“image”)

OpenCV Trackbar

We are creating three trackbars namely B, G, and R.

The trackbars will have a lower value as 0 and the highest value as 255.

createTrackbar() takes five parameters:

  1. Name of the trackbar, which will also be used to retrieve data
  2. Named window on which it will reside
  3. Lowest Value of the trackbar
  4. The highest value of the trackbar
  5. A callback function that will do nothing. 🙂

cv.createTrackbar(‘B’, ‘image’, 0, 255, change)

cv.createTrackbar(‘G’, ‘image’, 0, 255, change)

cv.createTrackbar(‘R’, ‘image’, 0, 255, change)

def change(x):
    # print(x)
    pass

OpenCV Python Switch

We are also creating ON and OFF Switch, that will turn on and off the trackbar.

If the Switch is on the trackbar will function otherwise it will not.

switch = ‘0: OFF\n 1: ON’

The switch will also reside on the Named Window i.e ‘Image’.

cv.createTrackbar(switch, ‘image’, 0, 1, change)

OpenCV Get Trackbar Pos

getTrackbarPos() takes two values:

  1. Name of the trackbar and,
  2. Name of the Window where it resides

This returns the trackbar position between low to high value i.e 0 to 255.

If the Switch is On, all the trackbar’s position will be taken and a new RGB color will be stored in the color matrix. The same matrix will be shown in the Named Window ‘image’.

    while True:
        cv.imshow("image", img)
        k = cv.waitKey(1) & 0xFF
        if k == 27:
            break

        b = cv.getTrackbarPos('B', 'image')
        g = cv.getTrackbarPos('G', 'image')
        r = cv.getTrackbarPos('R', 'image')
        s = cv.getTrackbarPos(switch, 'image')

        if s == 0:
            img[:] = 0
        else:
            img[:] = [b, g, r]

Trackbar OpenCV Python Documentation

Visit OpenCV Documentation if you want to get more ideas about the functions used.

Trackbar OpenCV Python GitHub

For the Full Source Code- Divyanshu Shekhar GitHub

How to create Named Window in OpenCV?

Named window is created in OpenCV using Named Window function which takes one argument i.e Name of the window. cv.namedWindow(“image”)

How to Get Trackbar Position Value in OpenCV?

getTrackbarPos() takes two values:
1. Name of the trackbar and,
2. Name of the Window where it resides

Example:-
b = cv.getTrackbarPos(‘B’, ‘image’)
g = cv.getTrackbarPos(‘G’, ‘image’)
r = cv.getTrackbarPos(‘R’, ‘image’)
s = cv.getTrackbarPos(switch, ‘image’)

How to Create Trackbar in OpenCV?

createTrackbar() takes five parameters:
1. Name of the trackbar, which will also be used to retrieve data
2. Named window on which it will reside
3. Lowest Value of the trackbar
4. The highest value of the trackbar
5. A callback function that will do nothing. 🙂

Example:-

cv.createTrackbar(‘B’, ‘image’, 0, 255, change)

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *