Site icon Machine Learning Projects

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

Machine Learning Projects

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…

https://machinelearningprojects.net/wp-content/uploads/2022/07/bgr-pallete.mp4

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)

Step 3 – Let’s declare a window.

windowName = 'OpenCV BGR Color Palette'
cv2.namedWindow(windowName)

Step 4 – Just declare an empty function.

def emptyFunction(x):
    return

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)
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
https://machinelearningprojects.net/wp-content/uploads/2022/07/bgr-pallete.mp4

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.

Exit mobile version