A K-means clustering based image compressor in OCaml
  • OCaml 89%
  • Standard ML 4.2%
  • Dune 3.6%
  • Makefile 3.2%
Find a file
2025-10-16 23:10:16 +02:00
.github/workflows fix(ci): explicitely install camlimages in deps step 2025-10-16 22:47:16 +02:00
examples refactor: delete unused image 2025-10-11 17:21:45 +02:00
lib feat: add implementation 2025-04-26 19:31:55 +02:00
src feat: add '--help' flag 2025-10-16 23:02:38 +02:00
.gitignore feat: handle image compression 2025-04-28 03:41:07 +02:00
dune-project feat: add implementation 2025-04-26 19:31:55 +02:00
imageCompressor.opam feat: add implementation 2025-04-26 19:31:55 +02:00
LICENSE feat: initial commit 2025-04-26 19:18:44 +02:00
Makefile feat: add implementation 2025-04-26 19:31:55 +02:00
README.md feat: add '--help' flag 2025-10-16 23:02:38 +02:00

Image Compressor

An implementation of the K-means clustering algorithm for image compression done in OCaml. Inspired by the same project done during the second year at Epitech, originally written in Haskell for the Functional Programming course.

This version reproduces the core pixel clustering functionality and also includes actual image compression, which was a bonus feature in the original assignment.

Original k = 10 k = 3

For other examples see this directory

Usage

Steps

  1. Clone this repository and open it

  2. Compile the program with the command

make
  1. Then you can use it as specified here :

For image compression

USAGE: ./imageCompressor [OPTIONS] -k K <input_image_filepath> [<output_filepath>]

    OPTIONS:
            -h | --help       display this message

    ARGS:
            K                       number of colors in the final image

            <output_filepath>       optional path/name for the compressed image
                                    Default: overwrites input image

Examples

$ ./imageCompressor -k 3 my_photo.jpg

$ ./imageCompressor -k 10 my_photo.png ~/Documents/compressed_photo


For pixel clusterization (the input file should contain points and colors).

Here are some input files examples

USAGE: ./imageCompressor [OPTIONS] -n N -l L -f F

    OPTIONS:
            -h | --help       display this message

    ARGS:
            N       number of colors in the final image
            L       convergence limit
            F       path to the file containing the colors of the pixels

Example

$ ./imageCompressor -n 3 -l 0.2 -f input.txt

Diagram

Here's a small flowchart representing how the program works.

graph LR

    subgraph Args[Argument handling]
        A[Parse args] --> B[Determine mode]
    end

    subgraph ImageCompressionPath[Image Compression]
        D[
            Read & Parse
            Image pixels
        ]
        H[Reconstruct image]
        G[Save compressed Image]
        B --> D
        H --> G
    end

    subgraph InputFilePath[Input File Clusterization]
        C[
            Read & Parse
            File pixels
        ]
        F[Print clusters]
        B --> C
    end

    subgraph KMeans[KMeans]
        E[K-MEANS Clusterization]
        C --> E
        D --> E
        E --> F
        E --> H
    end

    classDef args fill:#ffffff,stroke:#e6c229;
    classDef compression fill:#ffffff,stroke:#4CAF50;
    classDef clusterization fill:#ffffff,stroke:#673AB7;
    classDef kmeans fill:#ffffff,stroke:#F44336;

    class Args args
    class ImageCompressionPath compression
    class InputFilePath clusterization
    class KMeans kmeans