BGR Palette in OpenCV – a fun application in Python – 2024

In this blog, we will be creating a BGR Palette in OpenCV that will change its color live according to our actions on the track bar.

Didn’t get that??

Check out the video here – https://youtu.be/MzWSpNbdVFs

Sneak at our Result…

This is going to be a very interesting blog, so without any further due, Let’s do it…

Step 1 – Import necessary libraries.

import cv2
import numpy as np

Step 2 – Let’s create a black image as we saw in our last blog.

img = np.zeros((512, 512, 3), np.uint8)
  • What this line will do is, it will create a 3-D array of all zeros with height and width both set to 512 pixels. The three dimensions here depict RGB channels.
  • When all these are 0 it will act as a black image because we know that (0,0,0) is black and (255,255,255) is white. Every pixel of this image will be (0,0,0).

Step 3 – Let’s declare a window.

windowName = 'OpenCV BGR Color Palette'
cv2.namedWindow(windowName)
  • Here we are just declaring a window using the cv2.namedWindow() function of the cv2 library.
  • What it will do is it will create an empty window with the window name passed as an argument.

Step 4 – Just declare an empty function.

def emptyFunction(x):
    return
  • This step is not very important, but when we have to use trackbars we have to give a function while creating it because when we change the trackbar value it needs to perform a function and that is this function.
  • Here in our use case, we don’t want anything special to happen when we change the trackbar value that’s why we have just given a return statement inside it.
  • Frankly speaking, this step is just a FORMALITY…

NOTE – here x is the int value that the track-bar returns.

Step 5 – Let’s create the BGR Palette in Opencv or BGR track bars…

Syntax : cv2.createTrackbar(trackbarName, windowName, value, count, onChange) 

cv2.createTrackbar('B', windowName, 0, 255, emptyFunction)
cv2.createTrackbar('G', windowName, 0, 255, emptyFunction)
cv2.createTrackbar('R', windowName, 0, 255, emptyFunction)
BGR Palette
Trackbars for R, G and B channels

Here we are creating 3 track bars for each of the three channels R, G and, B.

Step 6 – Let’s run an infinite while loop…

while (True):
    blue = cv2.getTrackbarPos('B', windowName)
    green = cv2.getTrackbarPos('G', windowName)
    red = cv2.getTrackbarPos('R', windowName)
    
    img[:] = [blue, green, red]

    cv2.imshow(windowName, img)

if cv2.waitKey(1) == 27:
    break
  • The first 3 lines of the while loop are just taking out the blue, green, and red values from the track bars we are operating.
  • In the next step what we are doing is implying those colors on our image.
  • After that, we are just simply showing our image using the cv2.imshow() function.
  • The last line of the loop is used to break the code. 27 is the ASCII value of the Escape key. It means that if we hit the escape key, break the code.
  • In the last line, we are just destroying all the windows using the cv2.destroyAllWindows() function.

Let’s see the whole code for BGR Palette in Opencv…

import cv2
import numpy as np

def emptyFunction(x): #here x is the int value that trackbar returns
    return

img1 = np.zeros((512, 512, 3), np.uint8)

windowName = 'OpenCV BGR Color Palette'
cv2.namedWindow(windowName)
# BGR Palette in OpenCV
cv2.createTrackbar('B', windowName, 0, 255, emptyFunction)
cv2.createTrackbar('G', windowName, 0, 255, emptyFunction)
cv2.createTrackbar('R', windowName, 0, 255, emptyFunction)

while (True):
    blue = cv2.getTrackbarPos('B', windowName)
    green = cv2.getTrackbarPos('G', windowName)
    red = cv2.getTrackbarPos('R', windowName)

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

    cv2.imshow(windowName, img1)

    if cv2.waitKey(1) == 27:
        break

cv2.destroyAllWindows()

NOTE – Read more about creating trackbars.

Do let me know if there’s any query regarding BGR Palette in OpenCV by contacting me by email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: HOW TO DRAW BASIC SHAPES ON IMAGES IN PYTHON USING OPENCV

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Leave a Reply

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