Image Segmentation Using Color Spaces in OpenCV Python

In this post, we will look at Image Segmentation in Python Using Color Spaces. Often based on the properties of the picture’s pixels, image segmentation is widely used in digital image processing methods to divide an image into various portions or areas. Dividing images results between the viewer paying attention to the parts of the image they want to see and extracting the body from the picture they want to see.

What exactly is color space?

Red, blue, and green are the three basic colors that we utilize. When these three main colors are blended in different quantities, they give us distinct hues. In a cylinder-shaped arrangement of RGB color dots, it retains color information. The value ranges from 0-255, the saturation ranges from 0-255, and the hue ranges from 0-179. It mostly serves the goal of color segmentation.

Importing all the required modules:

For image segmentation, we need to import all the modules such as OpenCV, NumPy, and matplotlib.

import cv2
import matplotlib.pyplot as plt
import numpy as np

Now we have successfully imported all our libraries. For this article, we are assuming that you have the latest version of Python ready which is Python 3.0, with all the libraries in your system’s environment.

Matplotlib here will allow us to view the image and NumPy will allow us to make some changes to our image.

Color Space conversion OpenCV provides:

flags = [i for i in dir(cv2) if i.startswith('COLOR_')]

Now we are ready to attach our image and see the image segmentation. The image which we are employing is given below. We are using a very bright-colored image which allows us to see the image segmentation easily.

Code for image segmentation using OpenCV:

import cv2
import matplotlib.pyplot as plt
import numpy as np
flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
image = cv2.imread('demo_image.jpg')
plt.imshow(image)
plt.show()

Output image:

Since with this output image, we have concluded our tutorial. Learn, Crop Images with OpenCV-Python

Leave a Reply

Your email address will not be published. Required fields are marked *