Expand description
Image segmentation based on clustering methods.
Currently supported algorithms are the SLIC (simple linear iterative clustering) and SNIC (simple non-iterative clustering) superpixel algorithms. The crate also supports drawing basic contours around the image segments.
The library uses the palette
crate for some of its color types. The
current version used is palette 0.6
.
§Usage
Note that the convenience methods slic_from_bytes
and
snic_from_bytes
also exist to allow for calculation of superpixel labels
without having to convert to Lab
.
§SNIC
use palette::{cast, FromColor, Lab, Srgb};
use simple_clustering::snic;
let lab_buffer: Vec<Lab<_, f64>> = cast::from_component_slice::<Srgb<u8>>(&image)
.iter()
.map(|&c| Lab::from_color(c.into_format()))
.collect();
let labels = snic(k, m, width, height, &lab_buffer)?;
§SLIC
use palette::{cast, FromColor, Lab, Srgb};
use simple_clustering::slic;
let lab_buffer: Vec<Lab<_, f64>> = cast::from_component_slice::<Srgb<u8>>(&image)
.iter()
.map(|&c| Lab::from_color(c.into_format()))
.collect();
let labels = slic(k, m, width, height, None, &lab_buffer)?;
§Mean color segments and drawing segment contours
Using the labels from SNIC or SLIC, the mean colors can be found of each segment and output as an RGB image buffer. Contours can also be drawn around those segments.
use simple_clustering::image::{mean_colors, segment_contours};
let _ = mean_colors(&mut output_buffer, k, &labels, &lab_buffer)?;
segment_contours(&mut output_buffer, width, height, &labels, [0; 3])?;
Modules§
- error
- Superpixel error enums.
- image
- Functions for interacting with image labels and manipulating images.
- seed
- Functions for initializing superpixel seeds.
Structs§
- Superpixel
- Struct containing a superpixel’s color, X-coordinate, and Y-coordinate in an image.
Functions§
- slic
- Calculate SLIC.
- slic_
from_ bytes - Calculate SLIC by providing a buffer of RGB component bytes as
&[u8]
. - snic
- Calculate SNIC.
- snic_
from_ bytes - Calculate SNIC by providing a buffer of RGB component bytes as
&[u8]
.