How to Find the HSV values of a Color Using OpenCV Python?

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.

Like(0)

Related

Python OpenCV
Color Identification in Images using Python and OpenCVColor quantization in an image using K-means in OpenCV PythonDetecting corners using Harris corner detector in Python OpenCVHow to Access and Modify Pixel Value in an Image Using OpenCV PythonHow to access image properties in OpenCV using Python?How to Apply Affine Transformation on an Image in OpenCV Python?How to Apply Custom Filters to Images (2D Convolution) Using OpenCV Python?How to apply Perspective Transformations on an image using OpenCV Python?How to Approximate a Contour Shape in an Image Using OpenCV PythonHow to Blend Images Using Image Pyramids in OpenCV Python?How to Blur Faces in an Image using OpenCV Python?How to Change the Contrast and Brightness of an Image Using OpenCV in PythonHow to check if an image contour is convex or not in OpenCV Python?How to Compare Histograms of Two Images Using OpenCV Python?How to compare two images in OpenCV Python?How to Compute and Plot 2D Histograms of an Image in OpenCV Python?How to Compute Hu-Moments of an Image in OpenCV Python?How to Compute Image Moments in OpenCV Python?How to Compute the Area and Perimeter of an Image Contour using OpenCV Python?How to Compute the Aspect Ratio of an Object in an Image using OpenCV Python?How to Compute the Extent of an Object in an Image using OpenCV Python?How to Compute the Morphological Gradient of an Image Using OpenCV in Python?How to Convert a Colored Image to HLS in OpenCV using Python?How to convert an RGB image to HSV image using OpenCV Python?How to create a black image and a white image using OpenCV Python?How to Create a Depth Map from Stereo Images in OpenCV Python?How to Create a Trackbar as the HSV Color Palette using OpenCV Python?How to create a trackbar as the RGB color palette using OpenCV Python?How to Create a Watermark on an Image Using OpenCV Python?How to Crop and Save Detected Faces in OpenCV Python?How to detect a face and draw a bounding box around it using OpenCV Python?Detecting Rectangles and Squares in Images with OpenCV and PythonHow to detect a triangle in an image using OpenCV Python?How to Detect and Draw FAST Feature Points in OpenCV Python?How to detect cat faces in an image in OpenCV using Python?How to detect eyes in an image using OpenCV Python?How to Detect Humans in an Image in OpenCV Python?How to Detect License Plates Using OpenCV Python?How to detect polygons in image using OpenCV Python?How to Draw an Arrowed Line on an Image in OpenCV PythonHow to Draw Filled Ellipses in OpenCV using PythonHow to draw polylines on an image in OpenCV using Python?How to Extract the Foreground of an Image Using OpenCV Python?How to find and draw Convex Hull of an image contour in OpenCV Python?How to Find Discrete Cosine Transform of an Image Using OpenCV PythonHow to Find Gaussian Pyramids for an Image Using OpenCV in Python?How to Find Image Gradients using the Scharr Operator in OpenCV Python?How to Find Laplassian Pyramids for an Image Using OpenCV in Python?How to find patterns in a chessboard using OpenCV Python?How to find the bounding rectangle of an image contour in OpenCV Python?How to find the Fourier Transform of an image using OpenCV Python?How to find the Fourier Transforms of Gaussian and Laplacian filters in OpenCV Python?How to Find the HSV values of a Color Using OpenCV Python?How to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?How to Find the Minimum Enclosing Circle of an Object in OpenCV Python?How to find the solidity and equivalent diameter of an object in an image using OpenCV Python?How to fit the ellipse to an object in an image using OpenCV Python?How to flip an image in OpenCV Python?How to Implement FLANN Based Feature Matching in OpenCV PythonHow to Implement ORB Feature Detectors in OpenCV Python?How to implement probabilistic Hough Transform in OpenCV Python?How to join two images horizontally and vertically using OpenCV Python?How to Mask an Image in OpenCV Python?How to Match Image Shapes in OpenCV Python?How to Normalize an Image in OpenCV Python?How to Perform Adaptive Mean and Gaussian Thresholding of an Image using Python OpenCV?How to Perform Bilateral Filter Operation on an Image in OpenCV using Python?How to Perform Bitwise AND Operation on Two Images in OpenCV Python?How to Perform Bitwise OR Operation on Two Images in OpenCV PythonHow to Perform Bitwise XOR Operation on Images in OpenCV Python?How to Perform Different Simple Thresholding of an Image Using Python OpenCV?How to Perform Distance Transformation on a Given Image in OpenCV Python?How to Perform Image Rotation in OpenCV using PythonHow to Perform Image Translation Using OpenCV in Python?How to Perform Image Transpose Using OpenCV Python?How to Perform Matrix Transformation in OpenCV Python?How to perform Otsu's thresholding on an image using Python OpenCV?How to Plot Histograms of Different Colors of an Image in OpenCV Python?How to Resize an Image in OpenCV Using Python?How to Rotate an Image in OpenCV Python?How to Split an Image into Different Color Channels in OpenCV Python?Implementing k-Nearest Neighbor in OpenCV PythonImplementing Shi-Tomasi Corner Detector in OpenCV PythonOpenCV Python ŌĆō How to add borders to an image?OpenCV Python ŌĆō How to compute and plot the histogram of a region of an image?OpenCV Python ŌĆō How to Convert a Colored Image to a Binary Image?OpenCV Python – How to detect and draw keypoints in an image using SIFT?Opencv Python – How to display the coordinates of points clicked on an image?OpenCV Python ŌĆō How to draw a rectangle using Mouse Events?OpenCV Python ŌĆō How to Draw Circles Using Mouse Events?OpenCV Python ŌĆō How to draw curves using Mouse Events?OpenCV Python ŌĆō How to find and draw extreme points of an object on an image?OpenCV Python ŌĆō How to find the shortest distance between a point in the image and a contour?OpenCV Python ŌĆō How to perform bitwise NOT operation on an image?OpenCV Python ŌĆō How to Perform SQRBox Filter Operation on An ImageOpenCV Python – Implementing feature matching between two images using SIFTOpenCV Python – Matching the key points of two images using ORB and BFmatcherSmile Detection using Haar Cascade in OpenCV using Python