Change language

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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method