This Java application was developed for a Data Structures and Algorithms 2 project on color quantization. It processes a BMP image, reduces its color palette, and generates both a quantized image and an array representation of the results.
- Java Development Kit (JDK) installed. You can download it from here.
-
Clone the Repository
git clone https://github.com/stanleytengg/Color-Quantizer.git cd Color-Quantizer -
Add the Image File
- Place the BMP image you want to quantize in the
resourcesfolder. - Update the code to reference the correct image file name.
- Modify the code to set the desired number of colors for quantization.
- Place the BMP image you want to quantize in the
-
Compile the Source Code
javac -d bin src/main/*.java src/main/colorquantizer/*.java
-
Run the Application on Terminal
java -cp bin src.main.Main
Given an input BMP image:
The application produces a quantized image and its corresponding array:

This project implements a custom clustering algorithm for color quantization, which is used to reduce the number of colors in an image. The algorithm works by iteratively finding color centroids and assigning pixels to their closest centroids, forming clusters of similar colors. Here's a breakdown of the key steps:
- Initial Color Selection:
- The algorithm begins by selecting a set of initial color centroids. The first centroid is simply the first pixel from the image. Subsequent centroids are selected based on the maximum distance from any previously chosen centroids to ensure a good spread of colors.
- Pixel Assignment:
- Once the centroids are chosen, each pixel in the image is assigned to the nearest centroid using the squared Euclidean distance. This forms clusters of pixels around each centroid.
- Centroid Recalculation:
- After all pixels have been assigned to centroids, the algorithm recalculates each centroid's position. The new position is determined by averaging the RGB values of all pixels in its cluster.
- Convergence Check:
- The algorithm checks if the centroids have converged, meaning that the new centroids are the same as the previous ones. If not, the process repeats with the updated centroids until convergence is reached.
- Output:
- Once convergence is achieved, the final centroids form the color palette for the image, and each pixel is mapped to its corresponding centroid. The quantized image is then generated using this reduced color palette.
This project utilizes the Util and Pixel classes provided by Dr. Farnan and Dr. Garrison, which served as the backbone of the application. These classes include essential utility functions, such as converting BMP images to pixel matrices and saving pixel matrices as BMP files. Their foundational work was instrumental in the successful development of this project.