Visualize Different Color Spaces using OpenCV in Python
Hey fellow Python coders! In this tutorial, we will be learning about visualizing images in different color spaces using OpenCV in Python programming. Color Spaces helps in image processing and complex computer vision tasks. Let’s get started straight with the implementation.
Step 1: Importing Necessary Libraries
For this tutorial, we will make use of the OpenCV
library and matplotlib
library for converting the images to different color spaces and visualizing them respectively.
import cv2 import matplotlib.pyplot as plt
Step 2: Loading the Image into the Program
In this step, we will load an image in the program using the imread
function under the openCV library. Just make sure that the image exists in the path you have mentioned in your code snippet.
image_path = 'image.jpg' image = cv2.imread(image_path)
Step 3: Visualizing the Raw Image
To visualize the image, we will make use of the matplotlib
and OpenCV
library. To show the image, we will make use of the imshow
function. Have a look at the code and output below.
plt.imshow(image) plt.title('Original Image') plt.axis('off') plt.show()
In the image, the colors are messed up, as it’s a raw pixelated image without any color space formatting performed on the image.
Step 4: Converting Image to Different Color Spaces
In this section, we will convert the raw image to different color spaces. For this tutorial, we will convert the image to the following color spaces:
- RGB – Color combinations of red, green, and blue.
- HSV – Colors based on their hue, saturation, and value.
- Grayscale – Color values ranging from black to white.
- LAB – Color combinations that approximate human vision.
Let’s implement one color space after another in the upcoming sections.
Convert Image to RGB
RGB_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) plt.imshow(RGB_image) plt.title('RBG Image') plt.axis('off') plt.show()
Convert Image to HSV
HSV_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) plt.imshow(HSV_image) plt.title('HSV Image') plt.axis('off') plt.show()
Convert Image to Grayscale
GRAY_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(GRAY_image) plt.title('Grayscale Image') plt.axis('off') plt.show()
Convert Image to LAB Color Spaces
LAB_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) plt.imshow(LAB_image) plt.title('LAB Image') plt.axis('off') plt.show()
Complete Code Implementation for Color Spaces Visualization
Let’s combine all the code snippets into one single code snippet and make use of the power of subplots
to plot all the visualizations into one single plot. Have a look at the code snippet below.
import cv2 import matplotlib.pyplot as plt image_path = 'image1.jpg' image = cv2.imread(image_path) RGB_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) HSV_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) GRAY_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) LAB_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) fig, axs = plt.subplots(2, 2, figsize=(10, 10)) axs[0, 0].imshow(RGB_image) axs[0, 0].set_title('RGB Image') axs[0, 0].axis('off') axs[0, 1].imshow(HSV_image, cmap='hsv') axs[0, 1].set_title('HSV Image') axs[0, 1].axis('off') axs[1, 0].imshow(GRAY_image, cmap='gray') axs[1, 0].set_title('Grayscale Image') axs[1, 0].axis('off') axs[1, 1].imshow(LAB_image) axs[1, 1].set_title('LAB Image') axs[1, 1].axis('off') plt.show()
The final plot is as below.
Also Read:
- Edge detection using OpenCV in Python
- Count the Number of Object from an image using OpenCV in Python
- Simplification of image with binarization in OpenCV Python
- How to sharpen an image in Python using OpenCV
Happy Learning!
Leave a Reply