- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert an RGB image to HSV image using OpenCV Python?
An RGB (colored) image has three channels, Red, Blue and Green. A colored image in OpenCV has a shape in [H, W, C] format, where H, W, and C are image height, width and number of channels. All three channels have a value range between 0 and 255.
The HSV image also has three channels, the Hue, Saturation and Value channels. In OpenCV, the values of the Hue channel range from 0 to 179, whereas the Saturation and Value channels range from 0 to 255.
In OpenCV, to convert an RGB image to HSV image, we use the cv2.cvtColor() function. This function is used to convert an image from one color space to another.
This function takes two arguments− first the input image and second the color conversion method. See the syntax given below −
cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
Steps
To convert an RGB image to HSV image, follow the steps given below −
Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.
import cv2
Read the input RGB image using cv2.imread(). The RGB image read using this method is in BGR format. Optionally assign the read BGR image to bgr_img.
bgr_img = cv2.imread('water.jpg')
Now convert this BGR image to HSV image as below using cv2.cvtColor() function. Optionally assign the converted HSV image to hsv_img.
hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
Display the above converted HSV image.
cv2.imshow('HSV image', hsv_img) cv2.waitKey(0) cv2.destroyAllWindows()
Input Image
We will use this image as the input file in the following example.
Example
This Python program converts an RGB image to HSV image.
import cv2 # read the input RGB image as BGR format bgr_img = cv2.imread('water.jpg') # Convert the BGR image to HSV Image hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV) cv2.imwrite('hsv_image.jpg', hsv_img) # Display the HSV image cv2.imshow('HSV image', hsv_img) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output −
Notice the difference between the original RGB image and the HSV image.
- Related Articles
- How to convert RGB image to HSV using Java OpenCV library?
- How to convert HSV to colored image using Java OpenCV library?
- How to convert HSV to BGR image using Java OpenCV library?
- How to get pixels (RGB values) of an image using Java OpenCV library?
- OpenCV Python – How to convert a colored image to a binary image?
- How to Set/modify the pixels(RGB values) of an image using Java OpenCV Library?
- How to resize an image in OpenCV using Python?
- How to convert a colored image to HLS in OpenCV using Python?
- How to convert a negative image to positive image using Java OpenCV library?
- How to convert a colored image to Sepia image using Java OpenCV library?
- Using OpenCV in Python to Cartoonize an Image
- How to blur faces in an image using OpenCV Python?
- How to detect eyes in an image using OpenCV Python?
- How can scikit-learn be used to convert an image from RGB to grayscale in Python?
- OpenCV Python Program to analyze an image using Histogram
