K-Means Clustering for Image Compression This repository contains the implementation of the K-means algorithm for image compression. The goal is to reduce the number of colors in an image by clustering similar colors together. This process not only helps in compressing the image but also provides an intuitive understanding of how the K-means clustering algorithm works.
Overview The project is structured as follows:
K-means Algorithm Implementation: Custom implementation of the K-means algorithm. Image Compression: Application of K-means to compress an image by reducing its color space. Dependencies numpy matplotlib scipy (optional for loading .mat files if experimenting with additional datasets) Ensure you have the required packages installed by running:
bash
pip install numpy matplotlib scipy Usage The project is divided into several key components:
Finding Closest Centroids: Implementing the function to find the closest centroid for each data point. Computing Centroid Means: Recalculating the centroids based on the mean of the points assigned to each cluster. Applying K-means on a Sample Dataset: Understanding the K-means clustering with a 2D dataset. Image Compression with K-means: Reducing the color space of an image from thousands of colors to a specified number (e.g., 16 colors). Sample Code Snippets Finding Closest Centroids: python
def find_closest_centroids(X, centroids): idx = np.zeros(X.shape[0], dtype=int) # Your implementation here return idx Computing New Centroids: python
def compute_centroids(X, idx, K): centroids = np.zeros((K, X.shape[1])) # Your implementation here return centroids Running K-Means Clustering: python
initial_centroids = kMeans_init_centroids(X, K) centroids, idx = run_kMeans(X, initial_centroids, max_iters) Image Compression: Load your image and preprocess it: python
original_img = plt.imread('path/to/your/image.png') X_img = np.reshape(original_img, (original_img.shape[0] * original_img.shape[1], 3)) Compress the image: python
K = 16 # Number of colors centroids, idx = run_kMeans(X_img, initial_centroids, max_iters) Recover the image based on the centroids assigned to each pixel: python
X_recovered = centroids[idx, :] X_recovered = np.reshape(X_recovered, original_img.shape) Visualizing the Results After compressing the image, you can visualize the original and compressed images side by side:
python
fig, ax = plt.subplots(1, 2, figsize=(12, 6)) ax[0].imshow(original_img) ax[0].set_title('Original Image') ax[1].imshow(X_recovered) ax[1].set_title('Compressed Image with K=%d' % K) plt.show() Contributions Feel free to fork this project and submit your contributions via pull requests.
Remember to replace placeholders (like 'path/to/your/image.png') with actual values or parameters as per your project setup. This README provides a basic structure and can be extended based on further project details or additional features you implement.