2. Introduction
K-Means Clustering is an
iterative algorithm that divides
a dataset into K non-
overlapping clusters where each
data point belongs to the cluster
with the nearest mean. It’s used
in unsupervised learning,
where we don’t have predefined
labels.
3. How does it Works?
Let’s say you're a shop owner with data about your customers — their age and spending.
You want to group them into similar types of customers.
Step 1: Choose K
Decide how many groups (K) you want. Say K = 2 (maybe high spenders vs. low spenders).
Step 2: Pick K random points as starting centers (called centroids)
These centroids are just random guesses to start with.
Step 3: Assign each customer to the nearest centroid
• If a customer is closer to centroid 1 than to centroid 2 they go into
→ cluster 1.
• Do this for all customers.
4. Step 4: Move the centroids
• Find the average of all customers in each cluster.
• Move the centroid to that average position (like the new "center").
Step 5: Repeat!
• Reassign customers to their closest (new) centroids.
• Move centroids again.
• Keep repeating until nothing changes much — that's when the algorithm stops.
GOAL:
• Points in the same cluster are as close as possible to each other.
• Clusters are as far apart from each other as possible.
This is done by minimizing something called WCSS (Within-Cluster Sum of Squares)
5. Objective Function
Where:
• K is the number of clusters
• Ci is the set of points in cluster i
• i is the centroid of cluster i
μ
• ∥x i 2 is the Euclidean distance between a point x and the centroid.
−μ ∥
Distance Metrics
K-Means typically uses Euclidean Distance:
6. Image Segmentation
Goal: Divide an image into different regions (like separating background, objects, sky, etc.).
🔍 How K-Means helps:
• Each pixel is treated as a data point (with features like RGB values).
• K-Means clusters pixels with similar color values together.
• The result is an image divided into K segments, each with similar color or texture.
️
🖼️Example:
• A photo of a cat in a garden
• Background (grass)
• The cat's body
• Sky
• Other objects
7. Object Detection and Tracking
Goal: Group pixels or features belonging to the same object.
🔍 How K-Means helps:
• Cluster similar features (color, shape, motion) from video frames.
• Helpful as a preprocessing step to extract regions of interest (ROIs) before applying
deep learning.
Background Subtraction
Goal: Separate moving foreground objects from the static background in videos.
🔍 How K-Means helps:
• Cluster pixels based on movement or color.
• Identify which group stays consistent (background) and which changes (moving
object).
8. Benefits:
Simple and efficient
Works well with color-based tasks
Fast for smaller images
Easy to combine with other CV techniques
Limitations:
Doesn’t capture spatial relationships (just color-based)
Not suitable for complex object shapes
Sensitive to noise and outliers