DEV Community

Cover image for BGR Color Palette with Trackbars Using Python and OpenCV
Sona
Sona

Posted on

BGR Color Palette with Trackbars Using Python and OpenCV

BGR Color Palette with Trackbars Using Python and OpenCV. OpenCV (Open Source Computer Vision Library) is a popular library designed for real-time computer vision tasks. It offers various functions to work with images and videos, making it an essential tool in computer vision applications.

In this tutorial, we will create an interactive RGB color palette with trackbars using OpenCV in Python. By adjusting these trackbars, the RGB values will change dynamically between 0 and 255, allowing you to experiment and find the exact color represented by those RGB values.

Required Libraries:
To build this RGB color palette, we will need the following libraries:

OpenCV: For creating the window, trackbars, and capturing RGB values.
NumPy: For handling arrays and creating a blank image (black window) where the colors will be displayed.
Approach:
We will create a blank black window with a resolution of 512×512 pixels and three color channels (Blue, Green, and Red). Using OpenCV’s built-in functions, we will set up trackbars to control the intensity of each color channel (BGR).

The range for these trackbars will be from 0 to 255, allowing for the full spectrum of color values. As we move the trackbars, the RGB values will update in real-time, and the black window will change to reflect the new color.

Steps:
Create a Blank Black Window: Using NumPy, we will initialize a black window of 512×512 pixels, where each pixel has three channels for B, G, and R.
Set Up Trackbars: With OpenCV’s createTrackbar() function, we will create three trackbars to control the Blue, Green, and Red color channels. These trackbars will allow the user to adjust the intensity of each color component.
Update the Window in Real-Time: The values from the trackbars will be captured using getTrackbarPos() and used to modify the color displayed in the black window. Each time a trackbar is moved, the window will update to display the corresponding color based on the RGB values.
Here’s the full Python code for this project:

# Importing required libraries
import cv2
import numpy as np

# Define an empty function to handle trackbar events
def emptyFunction():
    pass

def main():
    # Create a blank black image of size 512x512 pixels with 3 color channels (B, G, R)
    image = np.zeros((512, 512, 3), np.uint8)  
    windowName = "OpenCV Color Palette"

    # Create a window with the specified name
    cv2.namedWindow(windowName)

    # Create trackbars for Blue, Green, and Red colors
    # The trackbars range from 0 to 255, representing the color intensity
    cv2.createTrackbar('Blue', windowName, 0, 255, emptyFunction)
    cv2.createTrackbar('Green', windowName, 0, 255, emptyFunction)
    cv2.createTrackbar('Red', windowName, 0, 255, emptyFunction)

    # Infinite loop to keep the window open until the ESC key is pressed
    while True:
        # Display the current image in the window
        cv2.imshow(windowName, image)

        # Exit the loop when the ESC key is pressed (ASCII value 27)
        if cv2.waitKey(1) == 27:
            break

        # Get the current positions of the trackbars
        blue = cv2.getTrackbarPos('Blue', windowName)
        green = cv2.getTrackbarPos('Green', windowName)
        red = cv2.getTrackbarPos('Red', windowName)

        # Update the image by filling it with the new color values from the trackbars
        image[:] = [blue, green, red]

        # Print the current RGB values to the console
        print(blue, green, red)

    # Close all windows when the loop is terminated
    cv2.destroyAllWindows()

# Calling the main function
if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Read More Below

Top comments (0)