Skip to content

majdjamal/image_segmentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Image Segmentation with K-Means

This repository contains experiments with K-Means Clustering and image segmentation. All models and experiments are written from scratch, using mainly NumPy and SciPy.

K-Means

K-Means is an algorithm that partitions data into clusters. We start with (1) initializing centroids, (2) create clusters by assigning data points to their nearest centroid, and (3) move centroids towards the center of the clusters. See figure 1 for a demonstration. More about K-Means is found here.

(Figure 1. A demonstration of K-Means Algorithm.)

Image Segmentation

K-Means Clustering can be used for image segmentation. The goal is to represent images in formats that are easier to analyze, e.g., for edge detection. This is also known as image processing. Experiments in this section use two images downloaded from Google, one of oranges, and one of humans walking.

Oranges

An image of oranges is used for segmentation. The original image is shown in Figure 2, and the result in Figure 3.

(Figure 2. Image of oranges that are used for the segmentation)

(Figure 3. Oranges segmented with (A) 2 clusters, and (B) 3 clusters)

Humans walking

Consider the image in Figure 4. There are no clear edges because humans are walking. We can highlight these humans with image segmentation, and it becomes easier to detect edges. The segmentation result is demonstrated in Figure 5.

(Figure 4. Image of humans walking)

(Figure 5. Image of humans walking segmented with 2 clusters.)

Testing

Required packages: NumPy, SciPy, cv2, and Matplotlib

Test K-Means by navigating to the directory in your terminal and type,

python experiment.py

Run the image segmentation experiment by typing,

python image_segmentation.py

Releases

No releases published

Packages

No packages published

Languages