How to find the HSV value of a color using OpenCV Python?
To find the HSV value of a color, we can use the color space conversion from BGR to HSV. First, we define the color value as numpy.ndarray in BGR format and then convert it to HSV space.
We can also find the lower and upper limits of HSV value as [H-10, 100, 100] and [H 10, 255, 255] respectively. These lower and upper limits can be used to track an object of particular color.
To find the HSV value of a color, follow these steps:
step
Import the required libraries. In all the following Python examples, the required Python libraries are OpenCV and NumPy. Make sure you have already installed them.
import cv2 import numpy as np
Define a numpy.ndarray for color, dtype=np.uint8.
green = np.uint8([[[0, 255, 0]]])
Convert the above defined color to HSV.
hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
Print the color values.
print("HSV of Green:", hsvGreen)
Let's look at some program examples to understand it clearly.
Example 1
In this example, we find the HSV value for green color. The BGR value of green is [0,255,0].
# import required libraries import numpy as np import cv2 # define a numpy.ndarray for the color # here insert the bgr values which you want to convert to hsv green = np.uint8([[[0, 255, 0]]]) # convert the color to HSV hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV) # display the color values print("BGR of Green:", green) print("HSV of Green:", hsvGreen) # Compute the lower and upper limits lowerLimit = hsvGreen[0][0][0] - 10, 100, 100 upperLimit = hsvGreen[0][0][0] + 10, 255, 255 # display the lower and upper limits print("Lower Limit:",lowerLimit) print("Upper Limit", upperLimit)
Output
When you run the above Python program, it will produce the following output −
BGR of Green: [[[ 0 255 0]]] HSV of Green: [[[ 60 255 255]]] Lower Limit: (50, 100, 100) Upper Limit (70, 255, 255)
Example 2
is:Example 2
In this example, we find the HSV value for a color whose BGR value is [106,76,89].
# import required libraries import numpy as np import cv2 green = np.uint8([[[0, 255, 0]]]) # convert the color to HSV hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV) # here insert the bgr values which you want to convert to hsv bgr = np.uint8([[[106,76,89]]]) hsv = cv2.cvtColor(green, cv2.COLOR_BGR2HSV) print("BGR Value:", bgr) print("HSV Value:", hsv) # compute the lower and upper limits lowerLimit = hsvGreen[0][0][0] - 10, 100, 100 upperLimit = hsvGreen[0][0][0] + 10, 255, 255 # display the lower and upper limits print("Lower Limit:",lowerLimit) print("Upper Limit", upperLimit)
Output
When you run the above python program, it will produce the following output −
BGR Value: [[[76 76 89]]] HSV Value: [[[ 60 255 255]]] Lower Limit: (50, 100, 100) Upper Limit (70, 255, 255)
The above is the detailed content of How to find the HSV value of a color using OpenCV Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
