Home  >  Article  >  Backend Development  >  How to create a slider of RGB color palette using OpenCV Python?

How to create a slider of RGB color palette using OpenCV Python?

WBOY
WBOYforward
2023-08-30 16:33:01587browse

In OpenCV, you can use the cv2.reateTrackbar() function to create a track bar. To access the value of the selected trackbar position, we use the cv2.getTrackbarPos() function.

Using these two functions, we create a window containing a tracking bar for R, G, B colors and a color window for displaying the selected color. By changing the position of the track bar RGB the color changes between 0 and 255. See the syntax of these two functions below.

grammar

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

parameter

  • trackbar_name - This is the trackbar name. This name is used to access the trackbar position value.

  • window_name - This is the name of the window to which the tracking bar is attached.

  • default_value - The default value set for the track bar.

  • max_value - The maximum value of the track bar.

  • callback_func - Function executed when the track bar value changes.

step

To create a RGB palette track bar, you can 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 them installed.

import cv2
import numpy as np

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

def nothing(x):
   pass

Define the black image and create a window named Trackbar Color Palette. The track bar and colors will appear in this window.

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

Create a trackbar for color changes with all five parameters. Track bar values ​​range from 0 to 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 track bar position 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 with the above colors.

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

Now displays the color of the selected track bar value 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 following Python program, we create a window as a palette. We created three track bars for the R, G, and B colors. Slide the slider to display the corresponding color 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 see the following Output window. To close the output window, press the "q" button.

如何使用OpenCV Python创建一个RGB颜色调色板的滑动条?

The default value for all three track bars is zero, and the color of the window is black. Slide the slider to see the corresponding color in the color window.

Example 2

In this program, we create a window as a palette with a toggle button.

We created four track bars, three for the R, G, B colors and one for the toggle button.

When the switch button is ON, only the colors in the color window will be displayed. Slide the slider and the corresponding color will be 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.

如何使用OpenCV Python创建一个RGB颜色调色板的滑动条?

The default value for all track bars is 0. The window color is black. When you turn on the switch (select 1) and slide the RGB color slider to the desired value, the color window will display the corresponding color.

If you slide the slider while the switch is off (set to 0), the color of the window will not change. It will remain as is (i.e. black).

The above is the detailed content of How to create a slider of RGB color palette using OpenCV Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete