Image Segmentation Algorithms With Implementation in Python
Image Segmentation Algorithms With Implementation in Python
Image Segmentation helps to obtain the region of interest (ROI) from the image. It is the process of
separating an image into different areas. The parts into which the image is divided are called Image
Objects. It is done based on the image properties like similarity, discontinuity, etc. The goal of image
segmentation is to simplify the image for better analysis. It is the process of assigning labels to every pixel
in an image. Image segmentation has wide applications in Machine Learning, Computer Vision, AI, Medical
imaging, Recognition tasks, Video surveillance, Object detection, etc. It impacts several domains, from
healthcare to space science.
In this article, we will be discussing different image segmentation algorithms like- Otsu’s segmentation,
Edge-based segmentation algorithms, Region-based segmentation algorithms, Clustering-based
segmentation algorithms, Neural networks for segmentation, and Watershed segmentation algorithms.
It comes under threshold-based segmentation. In Otsu’s Segmentation, the input image is first processed,
and then we try to obtain the histogram of the image, which will show the distribution of pixels in the
image. Here we focus on peak value. The next step is to compute the threshold value and compare it with
the image pixels. Set the pixel to white; if they are greater than the threshold else, set it to black.
Thus, it performs automatic thresholding. This method is not suitable for noisy images. Applications
include scanning documents, recognizing patterns.
# SEGMENTATION import numpy as np import cv2 from matplotlib import pyplot as plt img =
markers+1 # Now, mark the region of unknown with zero markers[unknown==255] = 0 markers =
plt.tight_layout() plt.show()
Output:
In this method, an edge filter is applied to the image. The image pixels are then classified as edge or non-
edge depending on the filter output. Edge detection helps to remove unwanted and unnecessary
information from the image. It includes 2 steps- Edge detection and edge linking.
ii. Prewitt operator: It is used for detecting vertical and horizontal edges in images
iii. Sobel operator: It is used for detecting vertical and horizontal edges in images. Similar to Prewitt
% Read Input Image input_image = imread('[name of input image file].[file format]'); % Displaying Input Image
RGB image to the grayscale image input_image = rgb2gray(input_image); % Convert the image to double
% When i = 1 and j = 1, then filtered_image pixel % position will be filtered_image(1, 1) % The mask is of
% Read Input Image input_image = imread('[name of input image file].[file format]'); % Displaying Input Image
RGB image to the grayscale image input_image = rgb2gray(input_image); % Convert the image to double
1]; % Edge Detection Process % When i = 1 and j = 1, then filtered_image pixel % position will be
sqrt(Gx.^2 + Gy.^2); end end % Displaying Filtered Image filtered_image = uint8(filtered_image); figure,
Here we grow the regions recursively by including the neighbour pixels which are similar and connected to
that pixel, and we will use similarity measures for regions with homogeneous grey levels. The process is
iterated in the same way as any general data clustering algorithm. Some of the advantages of this method
include faster and easier computation, better performance, etc.
Output:
It includes methods like fuzzy c-means, k-means, improved k-means, etc. Here we will take each point as a
separate cluster and merge two clusters with the minimum inter-cluster distance. Repeat this step until
clustering is satisfactory.
Another approach is using K means algorithm where we select K points and assign them a cluster center
by calculating the mean. Now, allocate other points nearest to the particular centers and form clusters.
Repeat unlit the cluster centers don’t change.
Code implementation:
import math n=int(input("Enter the number of points = ")) l1=[] l2=[] for i in range (1,n+1):
ordinate of %d point = "%(i)))) l=zip(l1,l2) #combines the 2lists to create a list of tuples #((1,1),(2,1))
p=list(l) #converts l to list [(1,1),(2,1)] print("The points are = ",p) a=int(input("Enter the number of
clusters = ")) k=[] b=[] for i in range (0,a): #a -> no. of clusters m=list(p[i]) #converts each
tuple(consisting of co-ordinates) to list [[1,1],[2,1]] k.append(m) #list carrying the centroids b.append(m)
#copy of centroids used later to chk if the repeat print("The centroids are = ",k) print() y=[] #distance
matrix while True: for j in range (0,a): x=[] #sublist in y considering dist of each pt from each cluster for
h=list(x) y.append(h) print("The distance of all points from centroid is : ") print() for i in range (0,a):
print() print() g=[] #new cluster for i in range (0,a): g.append([]) #creating sublists in g which is equal
to no. of clusters for i in range (0,n): min1=y[0][i] #dist of pt from 1st cluster for j in range (1,a):
if(min1>y[j][i]): g[j].append(i) else: g[0].append(i) print("New clusters are :",g) print() k.clear() #cal
new centroids for i in range(0,a): j=0 s1=0.0 s2=0.0 while(j<len(g[i])): e=g[i][j] s1=s1+l1[e] s2=s2+l2[e]
j=j+1 c1=s1/len(g[i]) c2=s2/len(g[i]) k.append([c1,c2]) print("The new centroids are = ",k) print() x.clear()
y.clear() g.clear() if(b==k): break else: b.clear() b=k print() print("Final centroids are = ",k)
Neural networks for image segmentation
Segmentation may require large neural networks. CNN is most widely used for object detection, pattern
recognition etc. It can even process large datasets. They are used for image segmentation due to their
high accuracy. It works as a hierarchical model with various layers and results in a segmented image as the
output.
Code Implementation:
import tensorflow as tf from tensorflow import keras from tensorflow.keras.models import Sequential from
model.add(Conv2D(filters=16,kernel_size=3,padding="same",activation="relu",input_shape=(50,50,1)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=32,kernel_size=3,padding="same",activation="relu"))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=64,kernel_size=3,padding="same",activation="relu"))
model.summary() model.compile(loss="categorical_crossentropy",optimizer='adam',metrics=['accuracy'])
model.fit(X_train,y_train,batch_size=50,epochs=20,verbose=1)
It is based on a topological interpretation of image boundaries. The closing operation helps close small
holes inside the foreground objects, or small black points on the object. Morphological dilation will make
objects more visible, and it fills small holes in the objects. Distance transform is calculated using the
Euclidean distance formula. These distance values are calculated for all the pixels in an image, and a
distance matrix is formed. It is an input for watershed transform.
Watershed segmentation can segment multiple objects in a single threshold setting. If the threshold is not
set properly, then the objects can result in over-segmented or unsegmented images. It then creates the
matrix of the distance between each pixel called label matrix. The label matrix is then fed as an input to the
watershed. Then watershed segmentation is applied in which the image is segmented into regions where
conceptually rainwater would flow into the same lake by identifying local minima in the image. This will
result in segmenting the image.
# SEGMENTATION import numpy as np import cv2 from matplotlib import pyplot as plt img =
Output:
plt.subplot(211),plt.imshow(dist_transform, 'gray') plt.title("Distance Transform"), plt.xticks([]),
Output:
plt.tight_layout() plt.show()
Output:
Conclusion
Thus Segmentation is used to isolate the desired object from the image in order to perform an analysis of
the object. CNN is a good approach for image segmentation but it can take more time during training if the
dataset is huge. Clustering-based segmentation takes huge computation time. Edge-based segmentation
is good for images having better contrast between objects.
The media shown in this ar ticle are not owned by Analytics Vidhya and are used at the Author’s
discretion.
Shruti Sureshan