Python OpenCV BGR Color Palette With Track Bars

FREE Online Courses: Knowledge Awaits – Click for Free Access!

BGR Color Palette With Track Bars

OpenCV is a free, powerful library for computer vision that offers a variety of tools to work with images and videos. One of the key features of OpenCV is its ability to work with different color spaces. The BGR color space is the default color space used by OpenCV to represent images. BGR stands for blue, green, and red, which are the primary colors used to create all other colors in the image. The BGR color palette with track bars is a graphical user interface that allows users to interactively adjust the values of the blue, green, and red channels of an image using track bars.

This enables users to experiment easily with different color combinations and make adjustments to the color balance of an image in real time. The BGR color palette with track bars is a powerful tool that can be used in a wide range of applications, including image processing, computer vision, and machine learning.

BGR Color Space

The BGR color space is a way of representing colors in an image using three channels: blue, green, and red. It’s used in computer vision and image processing because it allows for easy manipulation of individual color channels. Most digital cameras and image sensors use the BGR color space, so it’s the default color space used by OpenCV. It’s good for detecting objects in different lighting conditions, and you can make adjustments to the colors in an image by manipulating the individual channels.

Prerequisites for BGR Color Palette Using Python OpenCV

It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this, you should have the following system requirements.

  • Python 3.7 (64-bit) and above
  • Any Python editor (VS code, Pycharm, etc.)

Download Python OpenCV BGR Color Palette Project

Please download the source code of Python OpenCV BGR Color Palette Project from the following link:  Python OpenCV BGR Color Palette Project Code.

Installation

Open Windows cmd as administrator

1. To install the opencv library, run the command from the cmd.

pip install opencv-python

Let’s Implement

1. We need to import some libraries that will be used in our implementation.

import cv2
import numpy as np

2. This creates a blank black image with a size of 400×400 pixels and three color channels in Python. The image is stored in a variable called “img”. The code also creates a window called “image” that we’ll use to display the image.

img = np.zeros((400,400,3), np.uint8)
cv2.namedWindow('image')

3. This is a small function in Python that doesn’t do anything. It takes one input argument, but it doesn’t use it. It is often used as a placeholder function in OpenCV when designing a graphical user interface that includes trackbars. Trackbars are interactive widgets that allow you to change a value by moving a slider. When a trackbar is moved, OpenCV calls a callback function that performs a specific task. If no callback function is specified, OpenCV will use the “nothing” function by default, which does nothing.

def nothing(x):
    pass

4. These lines of code create three sliders that can be used to adjust the blue, green, and red color channels of an image displayed in a window using OpenCV in Python. These sliders are called “trackbars” and are displayed within the named window. The initial value of each slider is set to zero, and the maximum value is 255, which is the maximum value that each color channel can have.

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

5. Start the while loop.

while True:

6. These lines of code retrieve the current values of the blue, green, and red trackbars that were previously created in the named window “image”. The current values are stored in the variables “blue”, “green”, and “red”. These values can be used to adjust the color balance of an image in real-time, as they represent the current amount of blue, green, and red color channels that will be used to display the image.

blue  = cv2.getTrackbarPos('B','image')
green = cv2.getTrackbarPos('G','image')
red   = cv2.getTrackbarPos('R','image')

7. This line of code updates the color balance of an image displayed in a window by assigning the values of the variables “blue”, “green”, and “red” to their corresponding color channels. This allows for real-time adjustments of the image’s color balance based on the positions of trackbars in the window.

img[:] = [blue,green,red]

8. This line of code displays the image stored in the “img” variable in a named window called “ProjectGurukul” using the OpenCV function “cv2.imshow”.

cv2.imshow('ProjectGurukul',img)

9. Once the user presses the ‘q’ key, the loop in the program will break, and the program will stop running.

if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Note:- Steps 6-9 must be written under the while loop.

10. This line of code closes all active windows in OpenCV Python, effectively ending the program’s display of images. It can be used to close all windows at once instead of closing them individually.

cv2.destroyAllWindows()

Python OpenCV BGR Color Palette Output

palette with track output

Conclusion

OpenCV BGR color palette with track bars is a really cool tool that helps you change the colors in pictures and videos. You can use it to make colors brighter or darker, and even change them completely! The tool is really user-friendly because you can see the changes happening in real-time, as you move the sliders around. By changing the different color channels, you can create all kinds of fun and interesting color effects, which can be useful for different things like improving pictures or detecting objects in videos. Whether you’re new to computer vision and image processing or an expert, this tool is a great addition to your toolkit!

Your opinion matters
Please write your valuable feedback about ProjectGurukul on Google | Facebook

Leave a Reply

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