Image Segmentation using OpenCV

Plaban Nayak
6 min readNov 19, 2020

What is Image Segmentation ?

Image Segmentation is the process by which a digital image is partitioned into various subgroups (of pixels) called Image Objects, which can reduce the complexity of the image, and thus analyzing the image becomes simpler.

Usage of Segmentation in real world applications

One of the distinct and famous applications can be seen in Cancer cell detection systems where Image Segmentation proved pivotal in faster detection of disease tissues and cells from the imagery and hence enabling the Doctors in providing timely treatment.

The manufacturing industry now highly relies on image recognition techniques to detect anomalies which usually escape human eyes, hence increasing the efficiency of the products.

Image segmentation Techniques

we have the following techniques for image segmentation.

  1. Threshold Method
  2. Edge Based Segmentation
  3. Region Based Segmentation
  4. Clustering Based Segmentation
  5. Watershed Based Method
  6. Artificial Neural Network Based Segmentation
Comparison between Different Techniques

Here we have opted for clustering based segmentation.

Clustering Based Segmentation Methods

Clustering algorithms are unsupervised algorithms, unlike Classification algorithms, where the user has no pre-defined set of features, classes, or groups. Clustering algorithms help in fetching the underlying, hidden information from the data like, structures, clusters, and groupings that are usually unknown from a heuristic point of view.

The clustering based techniques segment the image into clusters or disjoint groups of pixels with similar characteristics. By the virtue of basic Data Clustering properties, the data elements get split into clusters such that elements in same cluster are more similar to each other as compared to other clusters. Some of the more efficient clustering algorithms such as k-means, improved k means, fuzzy c-mean (FCM) and improved fuzzy c mean algorithm (IFCM) are being widely used in the clustering based approaches proposed.

K means clustering is a chosen and popular method because of its simplicity and computational efficiency. The Improved K-means algorithm can minimize the number of iterations usually involved in a k-means algorithm.

A cluster refers to a collection of data points aggregated together because of certain similarities. For image segmentation, clusters here are different image colors.

Code Implementation

Import Libraries

Load Input Image and process it on OpenCV

Steps to perform segmentation

  • convert the image to RGB format
  • reshape the image to a 2D array of pixels and 3 color values (RGB)
  • cv2.kmeans() function which takes a 2D array as input hence we have to flatten the image
  • define stopping criteria for the cluster formation
  • Converting back to the original image shape and display the segmented image

K-means is one of the simplest unsupervised learning algorithms which can address the clustering problems, in general. The process follows a simple and easy way to classify a given image through a certain number of clusters which are fixed apriori.

The algorithm actually starts at this point where the image space is divided into k pixels, representing k group centroids. Now, each of the objects is then assigned to the group based on its distance from the cluster. When all the pixels are assigned to all the clusters, the centroids now move and are reassigned. These steps repeat until the centroids can no longer shift.

At the convergence of this algorithm, we have areas within the image, segmented into “K” groups where the constituent pixels show some levels of similarity.

Understanding Parameters for K-Means Clustering in OpenCV

Input parameters

  1. samples : It should be of np.float32 data type, and each feature should be put in a single column.
  2. nclusters(K) : Number of clusters required at end
  3. criteria : It is the iteration termination criteria. When this criteria is satisfied, algorithm iteration stops. Actually, it should be a tuple of 3 parameters. They are `( type, max_iter, epsilon )`:

a. type of termination criteria. It has 3 flags as below:

  • cv.TERM_CRITERIA_EPS — stop the algorithm iteration if specified accuracy, epsilon, is reached.
  • cv.TERM_CRITERIA_MAX_ITER — stop the algorithm after the specified number of iterations, max_iter.
  • cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER — stop the iteration when any of the above condition is met.

b. max_iter — An integer specifying maximum number of iterations.

c. epsilon — Required accuracy

  1. attempts : Flag to specify the number of times the algorithm is executed using different initial labeling. The algorithm returns the labels that yield the best compactness. This compactness is returned as output.
  2. flags : This flag is used to specify how initial centers are taken. Normally two flags are used for this : cv.KMEANS_PP_CENTERS and cv.KMEANS_RANDOM_CENTERS.

Output parameters

  1. compactness : It is the sum of squared distance from each point to their corresponding centers.
  2. labels : This is the label array (same as ‘code’ in previous article) where each element marked ‘0’, ‘1’…..
  3. centers : This is array of centers of clusters.

Here, when we see the image primarily there are three main colors (green for trees, blue for the sea/lake and white to orange for the sky). So we have considered the the number of clusters to be 3

As a result, we gonna use three clusters for this image

  • labels stores cluster label for each pixel(0/1/2)
  • centers stores to the center points of the clusters
  • cv2.KMEANS_RANDOM_CENTERS just indicates OpenCV to randomly assign the values of the clusters initially.

Construct the segmented image

  • convert all pixels to the color of the centroids
  • reshape back to the original image dimension
  • display the image

Disabling some clusters to visualize the segment they represented.

Output:

Cluster 1 represented the Green color as disabling cluster 1 or making it black is evident in the image

Similarly trying with number of clusters to be segmented upon as 8 and visualizing the image

Output:

Count associated with each Cluster
Disabling Cluster 4, assign black color to the pixels representing segment 4

Output:

Similarly disabling Cluster 2

A massive value add of clustering based ML algorithms is that we can measure the quality of the segments that get generated by using several statistical parameters such as: Silhouette Coefficient, rand index (RI) etc.

Conclusion:

Image segmentation is a promising set of skills as it has an important role to play in Medical Imaging and various organizations are striving to build an effective system for proactive diagnosis from medical imagery.

Image processing in general has been implemented in various programming languages — Java, matplotlib, C++ etc . Python libraries like scikit-image, OpenCV, Mahotas, Pillow, matplotlib, SimplelTK etc. are famously used to implement image processing in general and image segmentation in particular.

Image Segmentation implementation using Python is widely sought after skills and much training is available for the same. Using python libraries are a simpler way of implementation and it doesn’t demand any complicated requirements prior to implantation — except of course a basic knowledge in Python programming and pandas.

connect with me

--

--