DEV Community

Exploring conv-kmeans-lab: A C++ Tool for CIELAB Image Color Segmentation

Image segmentation is a fundamental task in computer vision, and grouping pixels by color is one of the most intuitive ways to achieve it. Enter conv-kmeans-lab, a specialized "Convolutional K-means Color Segmenter in CIELAB". Authored by Arpad K. (known by the GitHub handle @arpad1337) and copyrighted in 2025, this C++ tool provides an elegant software solution for reducing the color palette of an image using the K-means clustering algorithm. It is open-source and freely distributed under the MIT License.

Under the Hood: The CIELAB Advantage and Data Structures
A distinct advantage of conv-kmeans-lab is its use of the CIELAB color space, which is designed to be more perceptually uniform than traditional RGB or BGR spaces. The repository uses C++ and relies heavily on the popular OpenCV library (specifically utilizing cv::Mat and cv::Vec3b) to handle image matrices and color conversions.

To manage colors natively, the project introduces a dedicated PixelLAB class. Here are some of its core programmatic features:

  • Data Structure: It stores the l, a, and b channels as 8-bit unsigned characters (values ranging from 0 to 255).
  • Distance Calculations: The class includes static methods to calculate both 2D and 3D Euclidean distances, which are crucial mathematical steps for determining how "close" two colors are within the CIELAB space.
  • Operator Overloading: It includes beautifully overloaded operators, such as the subtraction operator (-) to instantly calculate the Euclidean distance between two pixels, and the division operator (/) to cleanly average the values of two pixels to create a new one.

The KMeansColorSegmenter Engine
The heavy lifting of the library is accomplished by the KMeansColorSegmenter class. The typical workflow follows a straightforward initialization, training, and conversion pipeline:

  1. Initialization: The segmenter is set up using a static create() method that takes an OpenCV image matrix, a desired number of colors to find (K), and an optional padding parameter. A strict initialization requirement of the algorithm is that the color count requested must possess an integer square root. A provided BGR image is then automatically converted to the LAB color space for processing.
  2. Training: By executing the train(epochs) method, the algorithm iteratively refines its color clusters. During each epoch, it computes the mean value of all pixels assigned to a centroid to determine new, more accurate centroid locations, and then updates the labels by reassigning all pixels in the image to their nearest new centroid.
  3. Conversion: The convert() method formally applies the trained centroids to the output matrix. It replaces every pixel in the matrix with the exact color of the trained cluster center that the pixel was categorized under.

Advanced Usage and Multi-Pass Segmentation
One of the most powerful features highlighted in the project's documentation is the ability to chain segmentations. Developers can take a processed image and immediately feed it back into the segmenter as the new input state using the setPreviousOutputAsInput() method.

This opens the door for iterative, multi-pass refinement. For example, a developer can:

  • Perform an initial training run on the original image.
  • Pass the output back in as the active input.
  • Set a new, smaller value for the desired number of colors using setK(colors2). In this scenario, the segmenter intelligently reduces the existing clusters by finding the closest existing centroids and merging them until the desired limit is reached.
  • Train and convert the image again for a more stylized, stepped color reduction.

Finally, the successfully segmented image can be retrieved either in its native LAB format using the getLABOutput() method, or it can be automatically converted back to the standard BGR format for viewing by calling the getOutput() method.

Conclusion
For developers working heavily with C++ and OpenCV, conv-kmeans-lab provides a robust, easy-to-use API for image segmentation. By leveraging the human-like perception of the CIELAB color space and allowing recursive clustering passes, it offers an effective mechanism for customized color processing and image manipulation tasks.

https://gemini.google.com/app/eb16357bd4828a8f

Top comments (0)