auto-palette 0.8.1

🎨 A Rust library that extracts prominent color palettes from images automatically.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::{
    cmp::{Ordering, Reverse},
    collections::BinaryHeap,
    marker::PhantomData,
};

use thiserror::Error;

use crate::{
    math::{
        clustering::{helper::gradient, Cluster, ClusteringAlgorithm, Initializer},
        matrix::{MatrixError, MatrixView},
        DistanceMetric,
        Point,
    },
    FloatNumber,
};

/// SNIC algorithm error type.
///
/// # Type Parameters
/// * `T` - The floating point type.
#[derive(Debug, PartialEq, Error)]
pub enum SNICError {
    /// Error when the shape is invalid.
    #[error("Invalid Shape: The shape must be > 0: {0}x{1}")]
    InvalidShape(usize, usize),

    /// Error when the number of segments is invalid.
    #[error("Invalid Segments: The number of segments must be > 0: {0}")]
    InvalidSegments(usize),

    /// Error when the points are empty.
    #[error("Empty Points: The points must be non-empty.")]
    EmptyPoints,

    /// Error when the points are not in the expected shape.
    #[error("Invalid Points: The points slice is not in the expected shape: {0}")]
    InvalidPoints(#[from] MatrixError),
}

/// SNIC (Simple Non-Iterative Clustering) algorithm.
///
/// This implementation is based on the following paper:
/// [Superpixels and Polygons using Simple Non-Iterative Clustering](https://openaccess.thecvf.com/content_cvpr_2017/papers/Achanta_Superpixels_and_Polygons_CVPR_2017_paper.pdf)
///
/// # Type Parameters
/// * `T` - The floating point type.
#[derive(Debug, PartialEq)]
#[allow(clippy::upper_case_acronyms)]
pub struct SNIC<T>
where
    T: FloatNumber,
{
    shape: (usize, usize),
    segments: usize,
    metric: DistanceMetric,
    _marker: PhantomData<T>,
}

impl<T> SNIC<T>
where
    T: FloatNumber,
{
    /// The label for unclassified points.
    const LABEL_UNCLASSIFIED: usize = usize::MAX;

    /// Creates a new `SNIC` instance.
    ///
    /// # Arguments
    /// * `shape` - The shape of the points to cluster as a tuple of (cols, rows).
    /// * `segments` - The number of segments to create.
    /// * `metric` - The distance metric to use.
    ///
    /// # Returns
    /// A new `SNIC` instance.
    pub fn new(
        shape: (usize, usize),
        segments: usize,
        metric: DistanceMetric,
    ) -> Result<Self, SNICError> {
        if shape.0 == 0 || shape.1 == 0 {
            return Err(SNICError::InvalidShape(shape.0, shape.1));
        }
        if segments == 0 {
            return Err(SNICError::InvalidSegments(segments));
        }
        Ok(Self {
            shape,
            segments,
            metric,
            _marker: PhantomData,
        })
    }

    /// Finds the index of the lowest gradient point in the matrix.
    ///
    /// # Type Parameters
    /// * `N` - The number of dimensions.
    ///
    /// # Arguments
    /// * `matrix` - The matrix of points.
    /// * `index` - The index of the point to check.
    ///
    /// # Returns
    /// The index of the point with the lowest gradient in the neighborhood if it exists.
    #[must_use]
    fn find_lowest_gradient_index<const N: usize>(
        &self,
        matrix: &MatrixView<T, N>,
        index: usize,
    ) -> Option<usize> {
        let col = index % matrix.cols;
        let row = index / matrix.cols;

        let mut lowest_score = T::max_value();
        let mut lowest_index = None;
        matrix.neighbors(col, row).for_each(|(neighbor_index, _)| {
            let neighbor_col = neighbor_index % matrix.cols;
            let neighbor_row = neighbor_index / matrix.cols;
            let score = gradient(matrix, neighbor_col, neighbor_row, self.metric);
            if score < lowest_score {
                lowest_score = score;
                lowest_index = Some(neighbor_index);
            }
        });
        lowest_index
    }
}

impl<T, const N: usize> ClusteringAlgorithm<T, N> for SNIC<T>
where
    T: FloatNumber,
{
    type Err = SNICError;

    fn fit(&self, points: &[Point<T, N>]) -> Result<Vec<Cluster<T, N>>, Self::Err> {
        if points.is_empty() {
            return Err(SNICError::EmptyPoints);
        }

        let (cols, rows) = self.shape;
        let matrix = MatrixView::new(cols, rows, points)?;

        // 1. Initialize the seeds using a grid pattern.
        let seeds = Initializer::Grid
            .initialize(&matrix, self.segments)
            .into_iter()
            .map(|seed_index| {
                let found = self.find_lowest_gradient_index(&matrix, seed_index);
                found.unwrap_or(seed_index)
            })
            .collect::<Vec<_>>();

        // 2. Initialize the priority queue with the seeds.
        let mut clusters = vec![Cluster::new(); seeds.len()];
        let mut queue = seeds.into_iter().enumerate().fold(
            BinaryHeap::with_capacity(matrix.size()),
            |mut heap, (cluster_label, point_index)| {
                let element = Element {
                    col: point_index % cols,
                    row: point_index / cols,
                    distance: T::zero(),
                    cluster_label,
                };
                heap.push(Reverse(element));
                heap
            },
        );

        // 3. Main clustering loop
        let mut labels = vec![Self::LABEL_UNCLASSIFIED; matrix.size()];
        while let Some(Reverse(element)) = queue.pop() {
            let point_index = element.col + element.row * cols;
            // Skip if the point is already assigned to a cluster.
            if labels[point_index] != Self::LABEL_UNCLASSIFIED {
                continue;
            }

            // Assign the nearest cluster label to the point.
            let cluster_label = element.cluster_label;
            labels[point_index] = cluster_label;

            let cluster = &mut clusters[cluster_label];
            cluster.add_member(point_index, &points[point_index]);

            // Traverse the neighbors of the point and add them as candidates for clustering.
            let centroid = cluster.centroid();
            matrix
                .neighbors(element.col, element.row)
                .filter(|(neighbor_index, _)| labels[*neighbor_index] == Self::LABEL_UNCLASSIFIED)
                .for_each(|(neighbor_index, neighbor_point)| {
                    let distance = self.metric.measure(centroid, neighbor_point);
                    let element = Element {
                        cluster_label,
                        col: neighbor_index % cols,
                        row: neighbor_index / cols,
                        distance,
                    };
                    queue.push(Reverse(element));
                });
        }
        Ok(clusters)
    }
}

/// An element representing a candidate for clustering.
///
/// # Type Parameters
/// * `T` - The floating point type.
#[derive(Debug)]
struct Element<T>
where
    T: FloatNumber,
{
    /// The column index of the element.
    col: usize,

    /// The row index of the element.
    row: usize,

    /// The distance of the element from the cluster centroid.
    distance: T,

    /// The cluster label of the element.
    cluster_label: usize,
}

impl<T> PartialEq for Element<T>
where
    T: FloatNumber,
{
    fn eq(&self, other: &Self) -> bool {
        self.cluster_label == other.cluster_label
            && self.distance == other.distance
            && self.col == other.col
            && self.row == other.row
    }
}

impl<T> Eq for Element<T> where T: FloatNumber {}

impl<T> PartialOrd for Element<T>
where
    T: FloatNumber,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T> Ord for Element<T>
where
    T: FloatNumber,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.distance
            .partial_cmp(&other.distance)
            .unwrap_or(Ordering::Less)
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    #[must_use]
    fn sample_points<T>(cols: usize, rows: usize) -> Vec<Point<T, 5>>
    where
        T: FloatNumber,
    {
        let half_cols = cols / 2;
        let half_rows = rows / 2;

        let mut points = vec![[T::zero(); 5]; cols * rows];
        for col in 0..cols {
            for row in 0..rows {
                let index = col + row * cols;
                let x = T::from_usize(col + 1) / T::from_usize(cols);
                let y = T::from_usize(row + 1) / T::from_usize(rows);
                points[index] = if col < half_cols && row < half_rows {
                    [T::one(), T::zero(), T::zero(), x, y]
                } else if col >= half_cols && row < half_rows {
                    [T::zero(), T::one(), T::zero(), x, y]
                } else if col < half_cols && row >= half_rows {
                    [T::zero(), T::zero(), T::one(), x, y]
                } else {
                    [T::zero(), T::zero(), T::zero(), x, y]
                };
            }
        }
        points
    }

    #[must_use]
    fn empty_points<T>() -> Vec<Point<T, 5>>
    where
        T: FloatNumber,
    {
        Vec::new()
    }

    #[test]
    fn test_new() {
        // Arrange
        let shape = (32, 18);
        let segments = 12;
        let metric = DistanceMetric::Euclidean;

        // Act
        let actual = SNIC::<f64>::new(shape, segments, metric);

        // Assert
        assert!(actual.is_ok());
        assert_eq!(
            actual.unwrap(),
            SNIC {
                shape,
                segments,
                metric,
                _marker: PhantomData,
            }
        );
    }

    #[rstest]
    #[case::invalid_shape_cols((0, 18), 12, DistanceMetric::Euclidean, SNICError::InvalidShape(0, 18))]
    #[case::invalid_shape_rows((32, 0), 12, DistanceMetric::Euclidean, SNICError::InvalidShape(32, 0))]
    #[case::invalid_segments((32, 18), 0, DistanceMetric::Euclidean, SNICError::InvalidSegments(0))]
    fn test_new_error(
        #[case] shape: (usize, usize),
        #[case] segments: usize,
        #[case] metric: DistanceMetric,
        #[case] expected: SNICError,
    ) {
        // Act
        let actual = SNIC::<f64>::new(shape, segments, metric);

        // Assert
        assert!(actual.is_err());
        assert_eq!(actual.unwrap_err(), expected);
    }

    #[test]
    fn test_fit() {
        // Arrange
        let cols = 32;
        let rows = 18;
        let segments = 12;
        let snic =
            SNIC::<f64>::new((cols, rows), segments, DistanceMetric::SquaredEuclidean).unwrap();

        // Act
        let points = sample_points::<f64>(cols, rows);
        let actual = snic.fit(&points);

        // Assert
        assert!(actual.is_ok());
        let clusters = actual.unwrap();
        assert_eq!(clusters.len(), segments);
    }

    #[test]
    fn test_fit_empty_points() {
        // Arrange
        let snic = SNIC::<f64>::new((32, 18), 12, DistanceMetric::SquaredEuclidean).unwrap();

        // Act
        let points = empty_points::<f64>();
        let actual = snic.fit(&points);

        // Assert
        assert!(actual.is_err());
        assert_eq!(actual.unwrap_err(), SNICError::EmptyPoints);
    }

    #[test]
    fn test_fit_invalid_points() {
        // Arrange
        let cols = 32;
        let rows = 18;
        let snic = SNIC::<f64>::new((cols, rows), 12, DistanceMetric::SquaredEuclidean).unwrap();

        // Act
        let points = sample_points(cols - 1, rows);
        let actual = snic.fit(&points);

        // Assert
        assert!(actual.is_err());
        assert_eq!(
            actual.unwrap_err(),
            SNICError::InvalidPoints(MatrixError::InvalidPoints(cols, rows))
        );
    }

    #[test]
    fn test_element_eq() {
        // Arrange
        let element1 = Element {
            col: 1,
            row: 2,
            distance: 3.0,
            cluster_label: 4,
        };
        let element2 = Element {
            col: 1,
            row: 2,
            distance: 3.0,
            cluster_label: 4,
        };

        // Act & Assert
        assert_eq!(element1, element2);
    }

    #[test]
    fn test_element_cmp() {
        // Arrange
        let element1 = Element {
            col: 1,
            row: 2,
            distance: 3.0,
            cluster_label: 4,
        };
        let element2 = Element {
            col: 1,
            row: 2,
            distance: 4.0,
            cluster_label: 4,
        };

        // Act & Assert
        assert_eq!(element1.cmp(&element1), Ordering::Equal);
        assert_eq!(element2.cmp(&element2), Ordering::Equal);
        assert_eq!(element1.cmp(&element2), Ordering::Less);
        assert_eq!(element2.cmp(&element1), Ordering::Greater);
    }

    #[test]
    fn test_element_partial_cmp() {
        // Arrange
        let element1 = Element {
            col: 1,
            row: 2,
            distance: 3.0,
            cluster_label: 4,
        };
        let element2 = Element {
            col: 1,
            row: 2,
            distance: 4.0,
            cluster_label: 4,
        };

        // Act & Assert
        assert_eq!(element1.partial_cmp(&element1), Some(Ordering::Equal));
        assert_eq!(element2.partial_cmp(&element2), Some(Ordering::Equal));
        assert_eq!(element1.partial_cmp(&element2), Some(Ordering::Less));
        assert_eq!(element2.partial_cmp(&element1), Some(Ordering::Greater));
    }

    #[test]
    fn test_cmp_nan() {
        // Arrange
        let element1 = Element {
            col: 1,
            row: 2,
            distance: 3.0,
            cluster_label: 4,
        };
        let element2 = Element {
            col: 1,
            row: 2,
            distance: f64::NAN,
            cluster_label: 4,
        };

        // Act & Assert
        assert_eq!(element1.cmp(&element2), Ordering::Less);
        assert_eq!(element2.cmp(&element1), Ordering::Less);
    }
}