Python | OpenCV BGR color palette with trackbars

OpenCV — is a library of programming functions mainly for real-time computer vision. 
In this article, let`s create a window that will contain an RGB color palette with track stripes. By moving the trackbars, the RGB Colors value will change from 0 to 255. So using the same we can find a color with its RGB values.

  Libraries needed:  OpenCV Numpy  

Fit:
Create a black window with three color channels at 512 x 512 resolution. Then create three trackbars & # 39; B & # 39; & # 39; G & # 39; & # 39; R & # 39; using the predefined functions of the OpenCV library. Set channel values ​​from 0 to 255. Merges a black box with these colored trackbars.

# Python program for creating RGB color
# palette with trackbars

 
# import libraries

import cv2

import numpy as np

  
# empty function is called when
# any trackbar movement

def emptyFunction ():

pass

 

def main ():

  

# black window with 3 color channels

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

windowName = "Open CV Color Palette"

 

# window name

cv2.namedWindow (windowName) 

 

  # there are trackbars named

Number of trackbars min. and max. values ​​

cv2.createTrackbar ( ` Blue` , windowName, 0 , 255 , emptyFunction)

cv2.createTrackbar ( ` Green` , windowName, 0 , 255 , emptyFunction)

cv2.createTrackbar ( `Red` , windowName, 0 , 255 , emptyFunction)

 

  # Used to open the window

# before clicking ESC keys

while ( True ):

cv2.imshow (windowName, image)

 

if cv2.waitKey ( 1 ) = = 27 :

break

 

# blue values , green , red

blue = cv2.getTrackbarPos ( `Blue` , windowName)

green = cv2.getTrackbarPos ( ` Green` , windowName)

red = cv2.getTrackbarPos ( `Red` , windowName)

  

# combine all three color channels and

# make the image a composite image from rgb

< p> image [:] = [blue, green, red]

  print (blue, green, red)

 

  cv2.destroyAllWindows ()

 
# Call main ()

if __ name__ = = "__ main__" :

main ()

Exit: