This project demonstrates image segmentation using K-Means clustering. The code resizes the input images, applies K-Means clustering to segment the images into different regions, and computes the silhouette score to evaluate the quality of the clusters.
Image segmentation is the process of partitioning an image into multiple segments or clusters to simplify or change the representation of an image into something more meaningful and easier to analyze. In this project, we use the K-Means clustering algorithm to achieve image segmentation. The K-Means algorithm partitions the image pixels into k
clusters based on their color similarity.
To run this project, you need to have Python installed along with the following libraries:
- numpy
- opencv-python
- scikit-learn
- matplotlib
You can install these dependencies using pip:
pip install numpy opencv-python scikit-learn matplotlib
-
Clone the repository:
git clone https://github.com/yourusername/image-segmentation-kmeans.git cd image-segmentation-kmeans
-
Place your images in a directory, e.g.,
images/
. -
Update the
image_paths
list in the script with the paths to your images:image_paths = ["images/image1.jpg", "images/image2.jpg"]
-
Run the script:
python segment_images.py
The script will output the original and clustered images side by side, along with the silhouette score for each clustered image. The silhouette score indicates how well each pixel has been clustered.