This repository implements image segmentation using two popular methods: K-Means clustering and the Normalized Cut algorithm. Image segmentation is the process of partitioning an image into meaningful regions for further analysis.
K-Means is an unsupervised clustering algorithm that partitions an image into
-
Initialization: Select
$K$ cluster centroids randomly. -
Assignment Step: Assign each pixel
$x_i$ to the nearest cluster centroid:
$$C_j = { x_i \mid \arg \min_k || x_i - \mu_k ||^2 },$$
where$\mu_k$ is the centroid of cluster$k$ . -
Update Step: Compute new centroids by taking the mean of all pixels assigned to each cluster:
$$\mu_k = \frac{1}{|C_k|} \sum_{x_i \in C_k} x_i.$$
- Repeat steps 2 and 3 until convergence (i.e., centroids do not change significantly).
K-Means is computationally efficient but may not always produce the best segmentation since it does not consider spatial relationships between pixels.
The Normalized Cut method formulates segmentation as a graph partitioning problem. The image is modeled as a graph
-
$W(A, B)$ is the total weight of edges connecting regions A and B, -
$W(A, A)$ and$W(B, B)$ are the total weights of edges within A and B, respectively.
The algorithm follows these steps:
- Construct a weighted graph based on pixel intensities or color features.
- Compute the graph Laplacian and solve the generalized eigenvalue problem:
$(D - W) v = \lambda D v$
where$W$ is the adjacency matrix and$D$ is the degree matrix. - Use the eigenvectors to find the optimal partition by minimizing the N-cut criterion.
N-cut provides better segmentation results compared to K-Means by incorporating spatial continuity but is computationally expensive.
refer to report.pdf for results
- J. Shi and J. Malik, "Normalized Cuts and Image Segmentation," IEEE Transactions on Pattern Analysis and Machine Intelligence, 2000.
- A. K. Jain, "Data Clustering: 50 Years Beyond K-Means," Pattern Recognition Letters, 2010.