An implementation of K-means clustering for image compression using C++ and no external ML libraries. This project, again, is a pure demonstration of my appreciation for machine learning and is not intended for production use. I still love raw pointers, but I'll start implementing smart ones in my next projects.
- K-means++ initialization for better clustering
- Customizable number of clusters using a spin control
- Image loading and saving with custom .dat formats that displays color metadata
- Simple GUI design with wxWidgets for real-time compression, interaction and visualization
- CMake ≥ 3.16
- A C++17 compiler
- OpenMP (optional; will use system version if available)
- wxWidgets v3.2.x (must be installed on your system)
Important: Users must install wxWidgets manually. The short setup guide for it is provided below.
-
Clone the repository:
git clone https://github.com/JazzJE/k-means-image-compression
-
Install dependencies
sudo apt update && sudo apt install -y build-essential cmake libwxgtk3.0-gtk3-dev libgtk-3-devTip: If
libwxgtk3.2-devisn’t available, try:sudo apt install -y libwxgtk3.2-gtk3-dev libwxbase3.2-dev
brew update && brew install cmake wxwidgetsNote: You may need to add wxWidgets to your PATH on macOS:
export PATH="/opt/homebrew/opt/wxwidgets/bin:$PATH"git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
.\vcpkg install wxwidgets:x64-windowsThen configure CMake to use vcpkg:
cmake .. -DCMAKE_TOOLCHAIN_FILE=path\to\vcpkg\scripts\buildsystems\vcpkg.cmake-
Build the project:
mkdir build cd build cmake .. cmake --build .
-
Run the program
./k_means_image_compression
- Use the GUI to load an image, set the number of clusters, and compress an image
- Save any compressed images that you want to keep in a
.PNGor custom.DATformat
(the.DATfile can only be opened using the display option of the program)
Original - 3.78 MB Compressed (K-Means) - 2.55 MB
Compression reduced file size by approximately 33% with minimal quality loss.



