


How to create a slider of RGB color palette using OpenCV Python?
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.
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
