Image Segmentation Using Fuzzy C-Means Clustering
Last Updated :
12 Jun, 2024
This article delves into the process of image segmentation using Fuzzy C-Means (FCM) clustering, a powerful technique for partitioning images into meaningful regions. We'll explore the fundamentals of FCM, its advantages over traditional methods, and provide a step-by-step guide to implementing FCM for image segmentation using Python. By the end of this article, you'll understand how to apply FCM clustering to achieve precise and effective image segmentation.
Introduction to Fuzzy C-Means Clustering
Let us now, introduce you to Fuzzy C-Means Clustering, our superpower for picture segmentation. Consider that you have a variety of marbles some blue and some red. All of the blue marbles would be in one pile and all of the red marbles in another if clustering were done regularly. What happens, though if there's a stone that has a reddish-purple hue ?
Fuzzy C-Means Clustering recognizes this conundrum. With the fuzzy C-Means (FCM) clustering technique, every data point has a degree of membership in a cluster determined by a membership grade. The ambiguity and overlap of image regions are handled by this method, which makes it very helpful for image segmentation. An approach that facilitates image segmentation is known by the fancy name Fuzzy C-Means Clustering. Now let's analyze it:
- Fuzzy: This implies ambiguous or non-linear. It describes, how a pixel might be a part of multiple groups in our context.
- C: This represents the quantity of groups or clusters that we wish to form.
- Means: This is about finding the average or center point of each group.
- Clustering: This is the act of grouping similar things together.
In other words, fuzzy C-Means clustering is a technique for organizing picture pixels into clusters in which each pixel can, to a certain extent be a part of more than one cluster.
How FCM Works
- Initialization: Randomly initialize cluster centers.
- Membership Calculation: Determine each data point's degree of membership in each cluster.
- Cluster Center Update: Update cluster centers based on the membership values.
- Convergence Check: Continue steps 2 and 3 until convergence is reached, or until there is little to no change in the cluster centers.
Mathematical Formulation
The objective function for FCM is:
J_m=\Sigma^{N}_{i=1}\Sigma^{C}_{j=1}u_{ij}^{m}\|x_{i}-c_{j}\|^2
where:
- N is the number of data points.
- ? is the number of clusters.
- u_{ij} is the membership of data point i in cluster j.
- x_{i} is the i-th data point.
- c_{j} is the center of cluster j.
- m is the fuzziness exponent (typically m=2).
Implementation Steps of Fuzzy C-Means Clustering for Image Segmentation
- Open the Image: Use OpenCV to read the image.
- Prepare the image: If there is color in the image, convert it to grayscale.
- Restructure the Image: Make a 2D array out of the picture with, each row representing a pixel.
- Set up the FCM Parameters: Give the fuzziness parameter, and the number of clusters definitions.
- Utilize the FCM Algorithm: For picture segmentation use an FCM implementation.
- Rework the Output: Return the clustered data to the original shape of the image.
- Visualize the Segmented Image: Present the segmented image.
Here is a Python implementation of image segmentation using FCM clustering:
Step 1: Import Necessary Libraries
We must import all required libraries first. These comprise the GUI creation, clustering, and image processing libraries.
! pip install opencv-python numpy scikit-fuzzy ipywidgets requests
Python
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.color import rgb2gray
import skfuzzy as fuzz
from ipywidgets import interact, widgets
from IPython.display import display
Step 2: Load and Preprocess the Demo Image
We'll load a demo image from a public URL and convert it to grayscale. Grayscale images are easier to work with for clustering.
Python
# Load the demo image from a public URL
url = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'
image = io.imread(url)
# Handle images with an alpha channel by considering only the first three channels (RGB)
if image.shape[-1] == 4:
image = image[..., :3]
# Convert the image to grayscale
gray_image = rgb2gray(image)
# Display the original and grayscale images
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image)
plt.subplot(1, 2, 2)
plt.title('Grayscale Image')
plt.imshow(gray_image, cmap='gray')
plt.show()
Output:

Step 3: Reshape the Image
Next, we reshape the grayscale image into a 1D array of pixel values. This format is required for the clustering algorithm.
Python
# Reshape the image into a 1D array
pixels = gray_image.reshape(-1, 1)
Step 4: Apply Fuzzy C-Means Clustering
We apply the Fuzzy C-Means clustering algorithm to the pixel data. This algorithm groups pixels into clusters based on their similarity.
Python
# Define the number of clusters
n_clusters = 3
# Apply Fuzzy C-Means clustering
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(
pixels.T, n_clusters, 2, error=0.005, maxiter=1000, init=None)
# Get the cluster membership for each pixel
cluster_membership = np.argmax(u, axis=0)
Step 5: Reshape the Clustered Data Back to Image Shape
We then reshape the clustered data back to the original image shape to visualize the segmented image.
Python
# Reshape the clustered data back to the original image shape
segmented_image = cluster_membership.reshape(gray_image.shape)
# Display the segmented image
plt.figure(figsize=(8, 8))
plt.title('Segmented Image')
plt.imshow(segmented_image, cmap='gray')
plt.show()
Output:
.png)
Step 6: Assign Colors to Each Cluster
To make the segmentation more visually appealing, we assign different colors to each cluster.
Python
# Create an empty image with the same shape as the original
colored_segmented_image = np.zeros((gray_image.shape[0], gray_image.shape[1], 3))
# Assign colors to each cluster
for i in range(n_clusters):
colored_segmented_image[segmented_image == i] = np.random.rand(3)
# Display the color-segmented image
plt.figure(figsize=(8, 8))
plt.title('Color-Segmented Image')
plt.imshow(colored_segmented_image)
plt.show()
Output:
.png)
Step 7: Save the Segmented Image
We can save the segmented image to a file for future use.
Python
# Save the segmented image
io.imsave('segmented_image.jpg', (colored_segmented_image * 255).astype(np.uint8))
Step 8: Create a GUI Interface with ipywidgets
Finally, we create a GUI interface using ipywidgets to allow users to test different images and adjust the number of clusters.
Python
def segment_image(url, n_clusters):
try:
# Load the image from the provided URL
image = io.imread(url)
# Handle images with an alpha channel by considering only the first three channels (RGB)
if image.shape[-1] == 4:
image = image[..., :3]
# Convert the image to grayscale
gray_image = rgb2gray(image)
# Reshape the image into a 1D array
pixels = gray_image.reshape(-1, 1)
# Apply Fuzzy C-Means clustering
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(
pixels.T, n_clusters, 2, error=0.005, maxiter=1000, init=None)
# Get the cluster membership for each pixel
cluster_membership = np.argmax(u, axis=0)
# Reshape the clustered data back to the original image shape
segmented_image = cluster_membership.reshape(gray_image.shape)
# Create an empty image with the same shape as the original
colored_segmented_image = np.zeros((gray_image.shape[0], gray_image.shape[1], 3))
# Assign colors to each cluster
for i in range(n_clusters):
colored_segmented_image[segmented_image == i] = np.random.rand(3)
# Display the original, grayscale, and color-segmented images
plt.figure(figsize=(18, 6))
plt.subplot(1, 3, 1)
plt.title('Original Image')
plt.imshow(image)
plt.subplot(1, 3, 2)
plt.title('Grayscale Image')
plt.imshow(gray_image, cmap='gray')
plt.subplot(1, 3, 3)
plt.title('Color-Segmented Image')
plt.imshow(colored_segmented_image)
plt.show()
except Exception as e:
print(f"Error loading image from URL: {e}")
# Create the widgets
url_widget = widgets.Text(
value='https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png',
description='Image URL:',
layout=widgets.Layout(width='80%')
)
clusters_widget = widgets.IntSlider(
value=3,
min=2,
max=10,
step=1,
description='Clusters:'
)
# Use the interact function to create the GUI
interact(segment_image, url=url_widget, n_clusters=clusters_widget)
Output:

Comparison of Image Segmentation Techniques
Technique
| Advantages
| Disadvantages
|
---|
FCM
| Handles overlapping data effectively
| May require careful selection of cluster number (C) and fuzziness coefficient (m)
|
---|
Thresholding
| Simple and fast
| Sensitive to noise and illumination variations
|
---|
Edge Detection
| Good for isolating objects with distinct edges
| May struggle with blurry edges or textured objects
|
---|
Region Growing
| Can handle complex shapes
| Sensitive to initial seed selection and parameter tuning
|
---|
Conclusion
Image segmentation using Fuzzy C-Means clustering is a powerful technique that helps in dividing an image into meaningful parts by allowing pixels to belong to multiple clusters. Because of its adaptability it is particularly helpful in situations where borders are ambiguous. You can begin investigating FCM applications in a variety of domains including as object identification, and medical imaging, once you have a fundamental understanding of fuzzy logic, clustering and how it operates.
Similar Reads
Image Segmentation using K Means Clustering
Image segmentation is a technique in computer vision that divides an image into different segments. This can help identify specific objects, boundaries or patterns in the image. Image is basically a set of given pixels and in image segmentation pixels with similar intensity are grouped together. Im
2 min read
Customer Segmentation using KMeans in R
Customer segmentation is one of unsupervised learning's most important applications. Employing clustering algorithms to identify the numerous customer subgroups enables businesses to target specific consumer groupings. In this machine learning project, K-means clustering, a critical method for clust
10 min read
Image Segmentation By Clustering
Segmentation By clustering It is a method to perform Image Segmentation of pixel-wise segmentation. In this type of segmentation, we try to cluster the pixels that are together. There are two approaches for performing the Segmentation by clustering. Clustering by MergingClustering by Divisive Cluste
4 min read
Fuzzy C-means Clustering in MATLAB
Fuzzy C-means (FCM) is a method of clustering that allows points to be more than one cluster. The (FCM) is a kind of data clustering technique in which the data set is grouped into N numbers of clusters with every data point corresponding to each cluster on the basis. which is to differentiate the d
4 min read
K-Means Clustering using PySpark Python
In this tutorial series, we are going to cover K-Means Clustering using Pyspark. K-means is a clustering algorithm that groups data points into K distinct clusters based on their similarity. It is an unsupervised learning technique that is widely used in data mining, machine learning, and pattern re
4 min read
Mean Shift Clustering using Sklearn
Clustering is a fundamental method in unsupervised device learning, and one powerful set of rules for this venture is Mean Shift clustering. Mean Shift is a technique for grouping comparable data factors into clusters primarily based on their inherent characteristics, with our previous understanding
9 min read
Image Segmentation using Python's scikit-image module
The process of splitting images into multiple layers, represented by a smart, pixel-wise mask is known as Image Segmentation. It involves merging, blocking, and separating an image from its integration level. Splitting a picture into a collection of Image Objects with comparable properties is the fi
14 min read
Clustering Text Documents using K-Means in Scikit Learn
Clustering text documents is a common problem in Natural Language Processing (NLP) where similar documents are grouped based on their content. K-Means clustering is a popular clustering technique used for this purpose. In this article we'll learn how to perform text document clustering using the K-M
3 min read
Image Segmentation Approaches and Techniques in Computer Vision
Image segmentation partitions an image into multiple segments that simplify the image's representation, making it more meaningful and easier to work with. This technique is essential for various applications, from medical imaging and autonomous driving to object detection and image editing. Effectiv
7 min read
K-Means vs K-Means++ Clustering Algorithm
Clustering is a fundamental technique in unsupervised learning, widely used for grouping data into clusters based on similarity. Among the clustering algorithms, K-Means and its improved version, K-Means++, are popular choices. This article explores how both algorithms work, their advantages and lim
6 min read