Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert an RGB image to HSV image using OpenCV Python?
An RGB (colored) image has three channels: Red, Green, and Blue. 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: Hue, Saturation and Value. 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 converts an image from one color space to another.
Syntax
cv2.cvtColor(src, code)
Parameters:
- src Input image
-
code Color space conversion code (e.g.,
cv2.COLOR_BGR2HSV)
Steps to Convert RGB to HSV
Follow these steps to convert an RGB image to HSV ?
Step 1: Import Required Libraries
import cv2 import numpy as np
Step 2: Read the Input Image
Read the input RGB image using cv2.imread(). Note that OpenCV reads images in BGR format by default ?
bgr_img = cv2.imread('image.jpg')
Step 3: Convert BGR to HSV
Convert the BGR image to HSV using cv2.cvtColor() ?
hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
Step 4: Display the Results
cv2.imshow('Original BGR Image', bgr_img)
cv2.imshow('HSV Image', hsv_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Complete Example
Here's a complete example that demonstrates RGB to HSV conversion ?
import cv2
import numpy as np
# Create a sample RGB image using NumPy
rgb_array = np.zeros((200, 200, 3), dtype=np.uint8)
rgb_array[:, :100] = [255, 0, 0] # Red half
rgb_array[:, 100:] = [0, 255, 0] # Green half
# Convert RGB to BGR (OpenCV format)
bgr_img = cv2.cvtColor(rgb_array, cv2.COLOR_RGB2BGR)
# Convert BGR to HSV
hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
# Display both images
cv2.imshow('Original BGR Image', bgr_img)
cv2.imshow('HSV Image', hsv_img)
# Print HSV values for inspection
print("HSV shape:", hsv_img.shape)
print("Sample HSV values (red region):", hsv_img[100, 50])
print("Sample HSV values (green region):", hsv_img[100, 150])
cv2.waitKey(0)
cv2.destroyAllWindows()
Understanding HSV Values
The HSV color space represents colors differently than RGB ?
| Channel | Range in OpenCV | Description |
|---|---|---|
| Hue | 0-179 | Color type (0=Red, 60=Yellow, 120=Green) |
| Saturation | 0-255 | Color intensity (0=Gray, 255=Pure color) |
| Value | 0-255 | Brightness (0=Black, 255=Bright) |
Practical Example with File Input
This example reads an actual image file and converts it to HSV ?
import cv2
# Read the input image
bgr_img = cv2.imread('input_image.jpg')
# Check if image is loaded successfully
if bgr_img is None:
print("Error: Could not load image")
exit()
# Convert BGR to HSV
hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
# Save the HSV image
cv2.imwrite('hsv_output.jpg', hsv_img)
# Display both images
cv2.imshow('Original Image', bgr_img)
cv2.imshow('HSV Image', hsv_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("HSV conversion completed successfully!")
Common Use Cases
HSV color space is particularly useful for ?
- Color-based object detection HSV makes it easier to define color ranges
- Image segmentation Separating objects based on color
- Color filtering Removing or enhancing specific colors
- Lighting-independent processing HSV is less sensitive to lighting changes
Conclusion
Converting RGB images to HSV using OpenCV is straightforward with cv2.cvtColor(). HSV color space is particularly useful for color-based computer vision tasks as it separates color information from brightness, making it more robust for various lighting conditions.
