Updated April 10, 2023
Introduction to OpenCV HSV
The OpenCV HSV function is abbreviated as Hue Saturation Value which is a scale which has been provided by the system to cater for a numeric value readout with respect to the accurate color name we just corresponding to the colors that are present in the image which is provided to the system to be analyzed. The hue of the color is measured in the standard unit of degrees (between 0 degree to 360 degree). The OpenCV library has a common function which is used to identify.
The spectrum of colors with wearing definition of hue and saturation present. It must be noted that there exist more than 150 conversion methods available for the color space conversion in the OpenCV library. Let us observe the function which is most commonly used for HSV preservation.
Syntax and Functions Used for Application of HSV
Following is the syntax which is used for implementation the OpenCV HSV function:
cv2.cvtColor(src, code[, dst[, dstCn]])
Parameter of application of OpenCV HSV:
Following are the parameters that are used for defining the cvtColor function which is used for conversion of images into HSV format:
Parameter | Description of Parameter Used |
src | It is the original image which is entered by the user as the input source for which the color space has to be changed. |
code | This parameter defines the specific code which is used for the application of the conversion of the color space for the provided image. |
dst | It is the output image which is produced by the system which is of the same depth as well as the size as compared to the primary source image or src. |
dstCn | The parameter defines the total number of channels that are present in the image produced at the destination. In case the parameter has not been specified and the default value is set to be 0, in such a situation the total number of channels is by default automated and derived from the code given and on the analysis of the primary source image. This parameter is an optional parameter. |
Return Value | It is the final image or type of image file which is produced by the system after application of the function. (Output Image) |
How does OpenCV HSV Function Execute?
Since the channel for the Hue concentration accounts for the color type, this function serves very useful for programs that use image processing or have associated tasks that work on the execution model of differentiating objects based on the color concentration and hue variation. The variation that is seen in the saturation of the color varies between unsaturated (that is representative of the various shades of the color Gray) to fully saturated (a color which has no white component present). The value channel parameter of the function is responsible for describing the amount of brightness or in other words the color intensity present in various portions in the image.
For example, suppose a given picture has a portion which is filled with the color cyan this color falls between 181 degrees to 240 degrees, while another color magenta is represented between 301 to 360 degrees depending on the intensity and hue of the color used in the image.
The image provided above is a representation of the Hue Saturation Value model in the cylindrical format.
Example of OpenCV HSV
Given below is the example mentioned:
Following is an example that illustrates the use of hue saturation value (OpenCV HSV) method using the OpenCV library in Python 3 programming language.
Code:
# command used to import the OpenCV library to utilize the Hue Saturation Value function
import cv2 as cv_2
import numpy as np1
cap_1 = cv_2.VideoCapture(0)
while(1):
# Capturing each frame of image that is provided by the user
# command cap.read is used in order to read the image which is being sourced by the programmer
_, frame1 = cap.read()
# Converting the BGR format to HSV format
hsv_1 = cv_2.cvtColor1(frame, cv.COLOR_BGR2HSV)
# defining the range of the color BLUE in HSV format
lower_blue1 = np1.array([110,50,50])
upper_blue1 = np1.array([130,255,255])
# Threshold of the feature for HSV image to result in the output image which contains blue colors only
mask1 = cv_2.inRange(hsv_1, lower_blue1, upper_blue1)
# Bitwise-AND mask and original image
res1 = cv_2.bitwise_and(frame1, frame1, mask1 = mask1)
# the resultant image is displayed which is showed together the difference in the images after HSV filter is applied which has been performed on it
cv_2.imshow('frame',frame1)
cv_2.imshow('mask',mask1)
cv_2.imshow('res',res1)
k = cv_2.waitKey(5) & 0xFF
if k1 == 27:
break
cv_2.destroyAllWindows()
Output:
Displaying the original image:
Displaying the output image with HSV outlay:
Conclusion
Hue Saturation Value or HSV function is a very important functionality present in the OpenCV library, which has been provided by the system to cater for a numeric value readout with respect to the accurate color name we just corresponding to the colors that are present in the image which is provided to the system to be analyzed. Since the channel for the Hue concentration accounts for the color type, this function serves very useful for programs that use image processing or have associated tasks that work on the execution model of differentiating objects based on the color concentration and hue variation. The variation that is seen in the saturation of the color varies between unsaturated to fully saturated. It serves to be a very important function and is seen to be used extensively in the applications that involve the use of image detection and frame scanning or image detection.
Recommended Articles
We hope that this EDUCBA information on “OpenCV HSV” was beneficial to you. You can view EDUCBA’s recommended articles for more information.