How to Find the HSV values of a Color Using OpenCV Python?
OpenCV is a popular library for computer vision applications that provides an easy-to-use interface for developing image processing algorithms. In this article, we will learn how to find the HSV values of a color using OpenCV Python.
What are HSV values?
HSV stands for Hue, Saturation, and Value. It is a color model that represents colors in terms of their hue, saturation, and brightness. The Hue value represents the color shade, Saturation value represents the intensity of the color, and Value represents the brightness of the color.
Why is it important to find the HSV values of a color?
Finding the HSV values of a color is essential in many computer vision applications, such as object detection, color segmentation, and tracking. HSV values provide a better representation of the color information than the standard RGB color space. Therefore, it is essential to know how to find HSV values for a given color.
Steps to find HSV values of a color using OpenCV Python
The following are the steps to find HSV values of a color using OpenCV Python-
Step 1: Import necessary libraries
First, we need to import the necessary libraries. We will be using the OpenCV library and its cv2 submodule to process images and find the HSV values of a color.
import cv2
import numpy as np
Step 2: Read the image
To find the HSV values of a color, we need to read an image in OpenCV Python. We can do this by using the cv2.imread()
function. The function reads an image in the BGR color format.
img = cv2.imread('image.png')
Step 3: Convert the BGR color space to HSV space
To find the HSV values of a color, we need to convert the BGR color space of the image to the HSV color space using the cv2.cvtColor()
function.
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
Step 4: Define the color range for which you want to find the HSV values
To find the HSV values of a specific color, we need to define the color range using the lower and upper bounds of the color. We can do this by creating a numpy array of the lower and upper bounds of the color in the HSV color space.
color_lower = np.array([0, 50, 50])
color_upper = np.array([10, 255, 255])
In this example, we are defining the HSV range for the red color. The lower and upper bounds are in the form of [H, S, V], where:
- H: Hue range (0-179)
- S: Saturation range (0-255)
- V: Value range (0-255)
Step 5: Create a mask to filter the image
After defining the color range, we can create a mask to filter the image pixels that fall within the defined color range. We can do this using the cv2.inRange()
function, which returns a binary image where pixels within the color range are white (255), and pixels outside the range are black (0).
mask = cv2.inRange(hsv_img, color_lower, color_upper)
We can visualize the mask by using cv2.imshow('mask', mask)
.
Step 6: Find the HSV values of the color
Now that we have a binary image with the filtered color pixels, we can find the average HSV values of the color using the cv2.mean()
function.
color_hsv = cv2.mean(hsv_img, mask=mask)
The cv2.mean()
function computes the mean (average) value of each channel (H, S, V) in the masked image pixels.
Step 7: Print the HSV values
Finally, we can print the HSV values of the color by accessing the values stored in the color_hsv
variable.
print("HSV values: ", color_hsv)
Full code
Here is the full code to find the HSV values of a color using OpenCV Python.
import cv2
import numpy as np
# Step 1: Read the image
img = cv2.imread('image.png')
# Step 2: Convert the BGR color space to HSV space
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Step 3: Define the color range for which you want to find the HSV values
color_lower = np.array([0, 50, 50])
color_upper = np.array([10, 255, 255])
# Step 4: Create a mask to filter the image
mask = cv2.inRange(hsv_img, color_lower, color_upper)
# Step 5: Find the HSV values of the color
color_hsv = cv2.mean(hsv_img, mask=mask)
# Step 6: Print the HSV values
print("HSV values: ", color_hsv)
Conclusion
In this article, we learned how to find the HSV values of a color using OpenCV Python. We used a simple step-by-step process that involved reading an image, converting the BGR color space to HSV space, defining the color range, creating a mask to filter the image, finding the HSV values of the color, and printing the values. We hope that this article will be helpful to you in your computer vision projects that involve color detection and segmentation.