How to find the HSV values of a color using OpenCV Python?

To find the HSV values of a color, we can use color space conversion from BGR to HSV. First we define the color value in BGR format as numpy.ndarray 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.

Steps

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 of dtype=np.uint8 for the color ?

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)

Example 1: Converting Green Color

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)
BGR of Green: [[[  0 255   0]]]
HSV of Green: [[[ 60 255 255]]]
Lower Limit: (50, 100, 100)
Upper Limit: (70, 255, 255)

Example 2: Converting Custom BGR Color

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

# here insert the bgr values which you want to convert to hsv
bgr = np.uint8([[[106, 76, 89]]])

# convert the color to HSV
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)

print("BGR Value:", bgr)
print("HSV Value:", hsv)

# compute the lower and upper limits
lowerLimit = hsv[0][0][0] - 10, 100, 100
upperLimit = hsv[0][0][0] + 10, 255, 255

# display the lower and upper limits
print("Lower Limit:", lowerLimit)
print("Upper Limit:", upperLimit)
BGR Value: [[[106  76  89]]]
HSV Value: [[[317 147  89]]]
Lower Limit: (307, 100, 100)
Upper Limit: (327, 255, 255)

HSV Color Space Explanation

The HSV color space represents colors using three components:

  • Hue (H): The color itself, ranging from 0-179 in OpenCV
  • Saturation (S): The intensity of the color, ranging from 0-255
  • Value (V): The brightness of the color, ranging from 0-255

Conclusion

Use cv2.cvtColor() with COLOR_BGR2HSV to convert BGR colors to HSV format. The HSV values can be used to define color ranges for object tracking and color-based image processing tasks.

Updated on: 2026-03-26T21:58:32+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements