How to create a trackbar as the RGB color palette using OpenCV Python?


In OpenCV, a trackbar can be created using cv2.reateTrackbar() function. To access the value of the selected trackbar position, we use cv2.getTrackbarPos() function.

Using these two functions, we create a window that contains the trackbars for R, G, B colors and a color window to display the selected color. By changing the position of trackbars RGB colors change between 0 and 255. See the below syntaxes for both functions.

Syntax

cv2.createTrackbar(trackbar_name, window_name, default_value, max_value, callback_func)
cv2.getTrackbarPos(trackbar_name, window_name)

Parameters

  • trackbar_name − It's the trackbar name. This name is used to access the trackbar position value.

  • window_name − It is the name of the window to which the trackbar is attached.

  • default_value − default value set for the trackbar.

  • max_value − maximum value for the trackbar.

  • callback_func − function executed when trackbar value changes.

Steps

To create a trackbar as RGB color palette, one could follow the steps given below −

The first step is to import the required libraries. The required Python libraries are OpenCV and NumPy. Make sure you have already installed them.

import cv2
import numpy as np

Next, define a callback function. It takes the trackbar position as a default argument. We define this function to do nothing.

def nothing(x):
   pass

Define the black image and create a window named Trackbar Color Palette. The trackbars and the color will be shown in this window.

img = np.zeros((300,650,3), np.uint8)
window_name = 'Trackbar Color Palette'
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)

Create trackbars for color change passing all five arguments. A trackbar value ranges between 0 and 255.

cv2.createTrackbar('R',window_name,0,255,nothing)
cv2.createTrackbar('G',window_name,0,255,nothing)
cv2.createTrackbar('B',window_name,0,255,nothing)

Get the current trackbar positions for all three RGB colors.

r = cv2.getTrackbarPos('R',window_name)
g = cv2.getTrackbarPos('G',window_name)
b = cv2.getTrackbarPos('B',window_name)

Update the color image window for the above colors.

img[:] = [b,g,r]

Now display the color for selected trackbar values in the color window.

while(True):
   cv2.imshow(window_name,img)
   k = cv2.waitKey(1) & 0xFF
   if k == ord('q'):
      break

Example 1

In the Python program below, we create a window as a color palette. We have created three trackbars for R, G and B colors. Sliding the trackbars you will get the corresponding color displayed in the color window.

# import required libraries import cv2 import numpy as np def nothing(x): pass # Create a black image, and the window img = np.zeros((300,650,3), np.uint8) window_name = 'Trackbar Color Palette' cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE) # create trackbars for color change cv2.createTrackbar('R',window_name,0,255,nothing) cv2.createTrackbar('G',window_name,0,255,nothing) cv2.createTrackbar('B',window_name,0,255,nothing) while(True): cv2.imshow(window_name,img) k = cv2.waitKey(1) & 0xFF if k == ord('q'): break # get current positions of four trackbars r = cv2.getTrackbarPos('R',window_name) g = cv2.getTrackbarPos('G',window_name) b = cv2.getTrackbarPos('B',window_name) img[:] = [b,g,r] cv2.destroyAllWindows()

Output

When you run the above program, you will get the following output window. To close the output window, press the ‘q’ button.

Default is zero for all three trackbars and the color of the window is black. Slide the trackbars and you will see the corresponding color in the color window.

Example 2

In this program, we create a window as a color palette having a switch button.

We have created four trackbars, three for R, G, B colors and one for the switch button.

When the switch button is ON, then only the colors in the color window are displayed. Sliding the trackbars, you will get the corresponding color displayed in the color window.

import cv2 import numpy as np def nothing(x): pass # Create a black image, a window img = np.zeros((300,650,3), np.uint8) window_name = 'Trackbar Color Palette' cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE) # create trackbars for color change cv2.createTrackbar('R',window_name,0,255,nothing) cv2.createTrackbar('G',window_name,0,255,nothing) cv2.createTrackbar('B',window_name,0,255,nothing) # create switch for ON/OFF functionality # switch = '0 : OFF \n1 : ON' cv2.createTrackbar("switch", window_name,0,1,nothing) while(True): cv2.imshow(window_name,img) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break # get current positions of four trackbars r = cv2.getTrackbarPos('R',window_name) g = cv2.getTrackbarPos('G',window_name) b = cv2.getTrackbarPos('B',window_name) s = cv2.getTrackbarPos("switch",window_name) if s == 0: img[:] = 0 else: img[:] = [b,g,r] cv2.destroyAllWindows()

Output

When you run the above program, it will display the following output window. To close the output window, press the ‘q’ button.

The default is 0 for all trackbars. The color of the window is black. When you turn the switch on (select it as 1) and slide the RGB color trackbars to your required values, the color window displays the corresponding color.

If you slide the trackbars when the switch is off (set at 0), then there will be no change in the color of the window. It will remain as it is (that is, black).

Updated on: 27-Sep-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements