Image segmentation is one of the key processes in machine vision applications to partition a digital image into a group of pixels. There are many great ways to segment an image. In this article, I will take you through the task of Image Segmentation with Python.
Image Segmentation with Python
Take a look at the image below of candies placed in a particular order to form a word. And, if a robot with vision was a task to count the number of candies by colour, it would be important for him to understand the boundaries between the candies.
I will use the image above for the task of Image Segmentation with Python. Now, let’s load the necessary packages and load the image from Unsplash to get started with this task:
import matplotlib.pyplot as plt from skimage.io import imread from skimage import color import numpy as np cimage = imread('https://images.unsplash.com/photo-1580015915218-685fd3cbfa97?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1267&q=80') fig, ax = plt.subplots(figsize=(20,20)) ax.imshow(cimage) ax.axis('off')
To segment this image we need to find the boundary of all the candies and then we will see what colour they are consisting off, for this I will plot a scatter plot to visualize all the colours of the candies with proper segmentation to understand all the colours inside the image. It will be a long code but it will be easy to run and learn:
As we are done with the scatter plot to segment the image according to the colours of the candies, now we can use this idea to segment the candies inside the image properly according to their colours:
This looks amazing right. I hope you now know how to perform a task of Image segmentation with Python. I hope you liked this article on Image Segmentation with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.