Implementation Steps of Fuzzy C-Means Clustering for Image Segmentation

  1. Open the Image: Use OpenCV to read the image.
  2. Prepare the image: If there is color in the image, convert it to grayscale.
  3. Restructure the Image: Make a 2D array out of the picture with, each row representing a pixel.
  4. Set up the FCM Parameters: Give the fuzziness parameter, and the number of clusters definitions.
  5. Utilize the FCM Algorithm: For picture segmentation use an FCM implementation.
  6. Rework the Output: Return the clustered data to the original shape of the image.
  7. 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:

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:


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:

Image Segmentation Using Fuzzy C-Means Clustering

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.

Similar Reads

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 ?...

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....

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....

Contact Us