DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. How to Cluster Images With the K-Means Algorithm

How to Cluster Images With the K-Means Algorithm

Learn how to read an image and cluster different regions of the image using the k-means algorithm and the SciPy library.

Sibanjan Das user avatar by
Sibanjan Das
CORE ·
Mar. 28, 18 · Tutorial
Like (7)
Save
Tweet
Share
55.78K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will explore a method to read an image and cluster different regions of the image. Image segmentation is an essential topic in an image processing framework. It is the process to classify an image into different groups. There are many different methods, and k-means is one of the most popular methods.

In this exercise, we will utilize the SciPy library to read the image as presented in the below code snippet.

%matplotlib inline
import matplotlib.pyplot as plt
from scipy import ndimage
from sklearn import cluster
image = ndimage.imread("sibanjan.jpg")
plt.figure(figsize = (15,8))
plt.imshow(image)

After we import the image, we print it on the screen to validate whether it was successfully uploaded. I uploaded my photograph. It is not the best one that I have, but it was something that I found it quickly on my desktop.

Image title

Next, we examine the dimensions of the image. As we can see, it is a three-dimensional array.

image.shape 

Image title

For clustering the image, we need to convert it into a two-dimensional array with the length being the 852*728 and width (3) as the RGB value.

x, y, z = image.shape
image_2d = image.reshape(x*y, z)
image_2d.shape

Next, we use scikit-learn's cluster method to create clusters. We pass n_clusters as 7 to form seven clusters. The clusters appear in the resulting image, dividing it into five parts with distinct colors. The number 7 was chosen heuristically for this demonstration. One can change the number of clusters to visually validate image with different colors and decide that closely matches the required number of clusters.

kmeans_cluster = cluster.KMeans(n_clusters=7)
kmeans_cluster.fit(image_2d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_

Once the clusters are formed, we can recreate the image with the cluster centers and labels to display the image with grouped patterns.

plt.figure(figsize = (15,8))
plt.imshow(cluster_centers[cluster_labels].reshape(x, y, z))

Image title

We can view from the above plot that the image is grouped into seven different regions based on the image density.

That's all for this piece! You can pull the code from my GitHub account.

clustering Algorithm

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Secrets Management
  • Apache Kafka vs. Memphis.dev
  • DevOps Roadmap for 2022
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: