Learn The Working Of HSV Range In OpenCV - EduCBA
Có thể bạn quan tâm
Updated April 19, 2023
Introduction to OpenCV HSV range
The HSV or Hue, Saturation and Value of a given object is the color space associated with the object in OpenCV where Hue represents the color, Saturation represents the greyness and Value represents the brightness and it is used to solve the problems related to computer vision because of its better performance when compared to RGB or Red, Blue and Green color space and the Hue range in HSV is [0,179], the Saturation range in HSV is [0,255] and the Value range in HSV is [0,255] and to perform object detection, finding the range of HSV is necessary.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax to define HSV range in OpenCV is as follows:
hsvcolorspace = cv.cvtColor(image, cv.COLOR_BGR2HSV) lower_hsvcolorspace = np.array([Hue range, Saturation range, Value range]) upper_hsvcolorspace = np.array([Hue range, Saturation range, Value range])where hsvcolorspace is the conversion of the given image in RGB format to HSV format,
lower_hsvcolorspace is the lower threshold for a range of some color,
upper_hsvcolorspace is the upper threshold for a range of some color,
Hue range is the Hue range in HSV which is [0,179],
Saturation range is the Saturation range in HSV which is [0,255] and
The value range is the Value range in HSV which is [0,255].
Working of HSV range in OpenCV
- The HSV or Hue, Saturation, and value of a given object is the color space associated with the object in OpenCV.
- The Hue in HSV represents the color, Saturation in HSV represents the greyness, and Value in HSV represents the brightness.
- Whenever we want to solve problems related to object detection, it is necessary to use HSV and find the range of HSV.
- The Hue, Saturation, and Value in HSV have their own range of values.
- The Hue range in HSV is [0,179], the Saturation range in HSV is [0,255] and the Value range in HSV is [0,255].
- There is also an upper bound and lower bound range for a range of each color in HSV.
- The HSV or Hue, Saturation, and value of a given object provide better performance when compared to RGB or Red, Blue, and Green color space and hence it is used widely in the area of computer vision.
Examples of OpenCV HSV range
Here are the follwoing examples mention below
Example #1
OpenCV program in python to mask the black color in the given image by converting it into an HSV image and specifying the lower threshold and upper threshold for a range of black color and then displaying the resulting image as the output on the screen
Code:
#importing the module cv2 and numpy import cv2 import numpy as np #reading the image which is to be converted to HSV color space imagergb = cv2.imread('C:/Users/admin/Desktop/log.jpg') #converting the image to HSV color space using cvtColor function imagehsv = cv2.cvtColor(imagergb, cv2.COLOR_BGR2HSV) #defining the lower threshold and upper threshold for a range of black color in HSV lower_black = np.array([0, 0, 0]) upper_black = np.array([350,55,100]) #masking the HSV image to get only black colors imagemask = cv2.inRange(imagehsv, lower_black, upper_black) #displaying the resulting HSV image with only black colors masked cv2.imwrite("C:/Users/admin/Desktop/logo1.png", imagemask)Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the image which is to be converted to HSV color space using the imread() function. Then we are converting the image to HSV color space using the cvtColor function. Then we are defining the lower threshold and upper threshold for a range of black color in HSV. Then we are masking the HSV image to get only black colors. Then we are displaying the resulting HSV image with masked black colors as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to mask the black color in the given image by converting it into an HSV image and specifying the lower threshold and upper threshold for a range of black color and then displaying the resulting image as the output on the screen:
Code:
#importing the module cv2 and numpy import cv2 import numpy as np #reading the image which is to be converted to HSV color space imagergb = cv2.imread('C:/Users/admin/Desktop/educba.jpg') #converting the image to HSV color space using cvtColor function imagehsv = cv2.cvtColor(imagergb, cv2.COLOR_BGR2HSV) #defining the lower threshold and upper threshold for a range of black color in HSV lower_black = np.array([0, 0, 0]) upper_black = np.array([350,55,100]) #masking the HSV image to get only black colors imagemask = cv2.inRange(imagehsv, lower_black, upper_black) #displaying the resulting HSV image with only black colors masked cv2.imwrite("C:/Users/admin/Desktop/educba1.jpg", imagemask)Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the image which is to be converted to HSV color space using the imread() function. Then we are converting the image to HSV color space using the cvtColor function. Then we are defining the lower threshold and upper threshold for a range of black color in HSV. Then we are masking the HSV image to get only black colors. Then we are displaying the resulting HSV image with masked black colors as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to mask the black color in the given image by converting it into an HSV image and specifying the lower threshold and upper threshold for a range of black color and then displaying the resulting image as the output on the screen
Code:
#importing the module cv2 and numpy import cv2 import numpy as np #reading the image which is to be converted to HSV color space imagergb = cv2.imread('C:/Users/admin/Desktop/educbaw.jpg') #converting the image to HSV color space using cvtColor function imagehsv = cv2.cvtColor(imagergb, cv2.COLOR_BGR2HSV) #defining the lower threshold and upper threshold for a range of black color in HSV lower_black = np.array([0, 0, 0]) upper_black = np.array([350,55,100]) #masking the HSV image to get only black colors imagemask = cv2.inRange(imagehsv, lower_black, upper_black) #displaying the resulting HSV image with only black colors masked cv2.imwrite("C:/Users/admin/Desktop/educbaw1.jpg", imagemask)Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the image which is to be converted to HSV color space using the imread() function. Then we are converting the image to HSV color space using the cvtColor function. Then we are defining the lower threshold and upper threshold for a range of black color in HSV. Then we are masking the HSV image to get only black colors. Then we are displaying the resulting HSV image with masked black colors as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “OpenCV HSV range” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
- NumPy vstack
- NumPy fft
- JavaScript Array Slice
- C++ Read File
Từ khóa » Hsv Color Space Opencv Python
-
Color Spaces In OpenCV | Python - GeeksforGeeks
-
Changing Colorspaces - OpenCV Documentation
-
Choosing The Correct Upper And Lower HSV Boundaries For Color ...
-
Changing Colorspaces - OpenCV-Python Tutorials - Read The Docs
-
OpenCV Color Spaces ( tColor ) - PyImageSearch
-
The HSV Color Space Using OpenCV In Python | Delft Stack
-
Image Segmentation Using Color Spaces In OpenCV + Python
-
Color Spaces In OpenCV (C++/Python) - LearnOpenCV
-
Detecting Colors (Hsv Color Space) - Opencv With Python - YouTube
-
Detecting Colors (Hsv Color Space) - Opencv With Python - Pysource
-
OpenCV 3 Video Capture & Switching Colorspaces RGB / HSV - 2020
-
How To Detect Colors Through HSV Color Space On OpenCV With ...
-
Color Spaces And Channels In OpenCV - Python Wife