oxicuda-vision 0.2.0

Vision Transformer & CLIP primitives for OxiCUDA: ViT patch embedding, multi-head self-attention, CLIP contrastive learning, FPN, RoI align, DETR decoder — pure Rust, zero CUDA SDK dependency.
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Mathematical morphology on single-channel `f32` images.
//!
//! Operates on a **single-channel** (grayscale or binary) `[h × w]` row-major
//! `f32` image, matching the [`crate::imgproc::edges`] convention. Binary inputs
//! are simply images whose values are restricted to `{0.0, 1.0}`; because the
//! flat (non-weighted) morphological operators reduce to per-pixel `min` / `max`
//! over a neighbourhood, the same grayscale implementation handles both the
//! binary (set) and grayscale (function) cases.
//!
//! * [`StructuringElement`] — a binary neighbourhood mask with an anchor, built
//!   via [`StructuringElement::rect`], [`StructuringElement::square`],
//!   [`StructuringElement::cross`], or [`StructuringElement::new`].
//! * [`erode`] — local minimum over the structuring element `B`:
//!   `(f ⊖ B)(x) = min_{b∈B} f(x + b)`.
//! * [`dilate`] — local maximum over the *reflected* structuring element:
//!   `(f ⊕ B)(x) = max_{b∈B} f(x − b)`. A single foreground point therefore
//!   dilates to a translated copy of `B`.
//! * [`open`] — [`erode`] followed by [`dilate`]; removes small bright details
//!   (idempotent).
//! * [`close`] — [`dilate`] followed by [`erode`]; fills small dark holes
//!   (idempotent).
//! * [`morphological_gradient`] — `dilate − erode`; highlights object
//!   boundaries.
//!
//! Out-of-bounds samples use **replicate** (clamp-to-edge) border handling,
//! consistent with the Sobel / Canny operators in this crate.

use crate::error::{VisionError, VisionResult};

/// A binary structuring element (neighbourhood mask) with an anchor.
///
/// The mask is a row-major `[height × width]` boolean grid; `true` cells are
/// members of the neighbourhood. The anchor (`anchor_y`, `anchor_x`) marks the
/// cell that aligns with the output pixel — typically the geometric centre.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructuringElement {
    /// Element width (number of columns).
    pub width: usize,
    /// Element height (number of rows).
    pub height: usize,
    /// Anchor column (origin of the element), `< width`.
    pub anchor_x: usize,
    /// Anchor row (origin of the element), `< height`.
    pub anchor_y: usize,
    /// Row-major `[height × width]` membership mask (`true` = member).
    pub mask: Vec<bool>,
}

impl StructuringElement {
    /// Build a structuring element from an explicit `[height × width]` mask,
    /// anchored at its geometric centre (`height / 2`, `width / 2`).
    ///
    /// # Errors
    /// Returns [`VisionError::EmptyInput`] if the element is degenerate (zero
    /// dimension or no member cells) and [`VisionError::DimensionMismatch`] if
    /// `mask.len() != width * height`.
    pub fn new(width: usize, height: usize, mask: Vec<bool>) -> VisionResult<Self> {
        let se = Self {
            width,
            height,
            anchor_x: width / 2,
            anchor_y: height / 2,
            mask,
        };
        validate_se(&se)?;
        Ok(se)
    }

    /// Build a solid (all-member) `width × height` rectangular element anchored
    /// at its centre.
    ///
    /// # Errors
    /// Returns [`VisionError::EmptyInput`] if either dimension is zero.
    pub fn rect(width: usize, height: usize) -> VisionResult<Self> {
        if width == 0 || height == 0 {
            return Err(VisionError::EmptyInput("structuring element"));
        }
        Ok(Self {
            width,
            height,
            anchor_x: width / 2,
            anchor_y: height / 2,
            mask: vec![true; width * height],
        })
    }

    /// Build a solid `size × size` square element.
    ///
    /// # Errors
    /// Returns [`VisionError::EmptyInput`] if `size == 0`.
    pub fn square(size: usize) -> VisionResult<Self> {
        Self::rect(size, size)
    }

    /// Build a plus-shaped (4-connected) cross element of the given `radius`.
    ///
    /// The element is `(2·radius + 1)²` in size with the central row and column
    /// set. A `radius` of `0` yields the `1 × 1` identity element.
    #[must_use]
    pub fn cross(radius: usize) -> Self {
        let size = 2 * radius + 1;
        let mut mask = vec![false; size * size];
        for i in 0..size {
            mask[radius * size + i] = true;
            mask[i * size + radius] = true;
        }
        Self {
            width: size,
            height: size,
            anchor_x: radius,
            anchor_y: radius,
            mask,
        }
    }

    /// Collect the active `(dy, dx)` offsets relative to the anchor.
    fn active_offsets(&self) -> Vec<(isize, isize)> {
        let mut offsets = Vec::new();
        for row in 0..self.height {
            for col in 0..self.width {
                if self.mask[row * self.width + col] {
                    offsets.push((
                        row as isize - self.anchor_y as isize,
                        col as isize - self.anchor_x as isize,
                    ));
                }
            }
        }
        offsets
    }
}

/// Validate a structuring element's shape and non-emptiness.
fn validate_se(se: &StructuringElement) -> VisionResult<()> {
    if se.width == 0 || se.height == 0 {
        return Err(VisionError::EmptyInput("structuring element"));
    }
    if se.mask.len() != se.width * se.height {
        return Err(VisionError::DimensionMismatch {
            expected: se.width * se.height,
            got: se.mask.len(),
        });
    }
    if se.anchor_x >= se.width || se.anchor_y >= se.height {
        return Err(VisionError::Internal(format!(
            "structuring-element anchor ({}, {}) out of bounds for {}×{}",
            se.anchor_y, se.anchor_x, se.height, se.width
        )));
    }
    if !se.mask.iter().any(|&m| m) {
        return Err(VisionError::EmptyInput("structuring element"));
    }
    Ok(())
}

/// Validate a single-channel image buffer.
#[inline]
fn validate_gray(img: &[f32], h: usize, w: usize) -> VisionResult<()> {
    if h == 0 || w == 0 {
        return Err(VisionError::InvalidImageSize {
            height: h,
            width: w,
            channels: 1,
        });
    }
    if img.len() != h * w {
        return Err(VisionError::DimensionMismatch {
            expected: h * w,
            got: img.len(),
        });
    }
    Ok(())
}

/// Validate the image and structuring element together for an operation.
fn validate_op(img: &[f32], h: usize, w: usize, se: &StructuringElement) -> VisionResult<()> {
    validate_gray(img, h, w)?;
    validate_se(se)?;
    if se.height > h || se.width > w {
        return Err(VisionError::Internal(format!(
            "structuring element {}×{} larger than image {}×{}",
            se.height, se.width, h, w
        )));
    }
    Ok(())
}

/// Clamp a signed coordinate into `[0, n)` (replicate / clamp-to-edge border).
#[inline]
fn clamp_idx(v: isize, n: usize) -> usize {
    if v < 0 {
        0
    } else if v as usize >= n {
        n - 1
    } else {
        v as usize
    }
}

/// Grayscale / binary **erosion**: per-pixel minimum over the structuring
/// element, `(f ⊖ B)(x) = min_{b∈B} f(x + b)`.
///
/// # Errors
/// Returns [`VisionError::InvalidImageSize`] / [`VisionError::DimensionMismatch`]
/// for shape problems, [`VisionError::EmptyInput`] for a degenerate element, and
/// [`VisionError::Internal`] if the element is larger than the image.
pub fn erode(img: &[f32], h: usize, w: usize, se: &StructuringElement) -> VisionResult<Vec<f32>> {
    validate_op(img, h, w, se)?;
    let offsets = se.active_offsets();
    let mut out = vec![0.0_f32; h * w];
    for y in 0..h {
        for x in 0..w {
            let mut acc = f32::INFINITY;
            for &(dy, dx) in &offsets {
                let sy = clamp_idx(y as isize + dy, h);
                let sx = clamp_idx(x as isize + dx, w);
                let v = img[sy * w + sx];
                if v < acc {
                    acc = v;
                }
            }
            out[y * w + x] = acc;
        }
    }
    Ok(out)
}

/// Grayscale / binary **dilation**: per-pixel maximum over the reflected
/// structuring element, `(f ⊕ B)(x) = max_{b∈B} f(x − b)`.
///
/// A single foreground point dilates to a translated copy of `B`, so the
/// operator "grows" bright regions by the element's shape.
///
/// # Errors
/// Same as [`erode`].
pub fn dilate(img: &[f32], h: usize, w: usize, se: &StructuringElement) -> VisionResult<Vec<f32>> {
    validate_op(img, h, w, se)?;
    let offsets = se.active_offsets();
    let mut out = vec![0.0_f32; h * w];
    for y in 0..h {
        for x in 0..w {
            let mut acc = f32::NEG_INFINITY;
            for &(dy, dx) in &offsets {
                // Reflected element: sample at x − b.
                let sy = clamp_idx(y as isize - dy, h);
                let sx = clamp_idx(x as isize - dx, w);
                let v = img[sy * w + sx];
                if v > acc {
                    acc = v;
                }
            }
            out[y * w + x] = acc;
        }
    }
    Ok(out)
}

/// Morphological **opening**: [`erode`] then [`dilate`]. Removes small bright
/// structures (smaller than the element) while preserving the shape and size of
/// larger ones. Opening is idempotent: `open(open(f)) = open(f)`.
///
/// # Errors
/// Same as [`erode`].
pub fn open(img: &[f32], h: usize, w: usize, se: &StructuringElement) -> VisionResult<Vec<f32>> {
    let eroded = erode(img, h, w, se)?;
    dilate(&eroded, h, w, se)
}

/// Morphological **closing**: [`dilate`] then [`erode`]. Fills small dark holes
/// and gaps (smaller than the element) while preserving larger structures.
/// Closing is idempotent: `close(close(f)) = close(f)`.
///
/// # Errors
/// Same as [`erode`].
pub fn close(img: &[f32], h: usize, w: usize, se: &StructuringElement) -> VisionResult<Vec<f32>> {
    let dilated = dilate(img, h, w, se)?;
    erode(&dilated, h, w, se)
}

/// Morphological **gradient**: `dilate(f) − erode(f)`. Produces a thin response
/// along object boundaries (the difference between the grown and shrunk image).
///
/// # Errors
/// Same as [`erode`].
pub fn morphological_gradient(
    img: &[f32],
    h: usize,
    w: usize,
    se: &StructuringElement,
) -> VisionResult<Vec<f32>> {
    let dilated = dilate(img, h, w, se)?;
    let eroded = erode(img, h, w, se)?;
    let out = dilated
        .iter()
        .zip(eroded.iter())
        .map(|(&d, &e)| d - e)
        .collect();
    Ok(out)
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    /// Image with a single foreground pixel at `(y, x)`.
    fn single_pixel(h: usize, w: usize, y: usize, x: usize) -> Vec<f32> {
        let mut img = vec![0.0_f32; h * w];
        img[y * w + x] = 1.0;
        img
    }

    /// Image with a solid `[r0..r1] × [c0..c1]` block of foreground.
    fn block(h: usize, w: usize, r0: usize, r1: usize, c0: usize, c1: usize) -> Vec<f32> {
        let mut img = vec![0.0_f32; h * w];
        for y in r0..r1 {
            for x in c0..c1 {
                img[y * w + x] = 1.0;
            }
        }
        img
    }

    #[test]
    fn erode_removes_isolated_pixel() {
        let img = single_pixel(7, 7, 3, 3);
        let se = StructuringElement::square(3).expect("se");
        let out = erode(&img, 7, 7, &se).expect("erode");
        let total: f32 = out.iter().sum();
        assert_eq!(total, 0.0, "erosion must remove an isolated pixel");
    }

    #[test]
    fn dilate_grows_single_pixel_to_se() {
        let img = single_pixel(5, 5, 2, 2);
        let se = StructuringElement::square(3).expect("se");
        let out = dilate(&img, 5, 5, &se).expect("dilate");
        for y in 0..5 {
            for x in 0..5 {
                let expected = if (1..=3).contains(&y) && (1..=3).contains(&x) {
                    1.0
                } else {
                    0.0
                };
                assert_eq!(
                    out[y * 5 + x],
                    expected,
                    "dilation of a point must reproduce the 3×3 element at ({y},{x})"
                );
            }
        }
    }

    #[test]
    fn dilate_constant_image_unchanged() {
        let img = vec![0.37_f32; 6 * 6];
        let se = StructuringElement::square(3).expect("se");
        let out = dilate(&img, 6, 6, &se).expect("dilate");
        assert!(out.iter().all(|&v| (v - 0.37).abs() < 1e-6));
    }

    #[test]
    fn erode_constant_image_unchanged() {
        let img = vec![0.42_f32; 6 * 6];
        let se = StructuringElement::square(3).expect("se");
        let out = erode(&img, 6, 6, &se).expect("erode");
        assert!(out.iter().all(|&v| (v - 0.42).abs() < 1e-6));
    }

    #[test]
    fn cross_se_dilation_shape() {
        let img = single_pixel(5, 5, 2, 2);
        let se = StructuringElement::cross(1);
        let out = dilate(&img, 5, 5, &se).expect("dilate");
        let total: f32 = out.iter().sum();
        assert_eq!(total, 5.0, "plus-shaped element has 5 members");
        // The plus arms.
        for &(y, x) in &[(2usize, 2usize), (1, 2), (3, 2), (2, 1), (2, 3)] {
            assert_eq!(out[y * 5 + x], 1.0);
        }
        // Diagonal corner must remain background.
        assert_eq!(out[3 * 5 + 3], 0.0);
    }

    #[test]
    fn opening_removes_noise_keeps_blob() {
        // 8×8: a solid 4×4 block (rows 2..6, cols 2..6) plus a single noise pixel.
        let mut img = block(8, 8, 2, 6, 2, 6);
        img[0] = 1.0; // isolated noise at (0, 0)
        let se = StructuringElement::square(3).expect("se");
        let out = open(&img, 8, 8, &se).expect("open");
        // Noise gone.
        assert_eq!(out[0], 0.0, "opening must remove the isolated noise pixel");
        // Block restored exactly (16 pixels, rows/cols 2..6).
        let total: f32 = out.iter().sum();
        assert_eq!(total, 16.0, "opening must preserve the 4×4 blob");
        for y in 2..6 {
            for x in 2..6 {
                assert_eq!(out[y * 8 + x], 1.0);
            }
        }
    }

    #[test]
    fn closing_fills_small_hole() {
        // Solid 4×4 block with a single-pixel hole punched at its centre.
        let mut img = block(8, 8, 2, 6, 2, 6);
        img[3 * 8 + 3] = 0.0; // hole
        let se = StructuringElement::square(3).expect("se");
        let out = close(&img, 8, 8, &se).expect("close");
        assert_eq!(out[3 * 8 + 3], 1.0, "closing must fill the interior hole");
        // The block is fully solid afterwards, no spill outside.
        let total: f32 = out.iter().sum();
        assert_eq!(total, 16.0);
    }

    #[test]
    fn opening_is_idempotent() {
        // Interior features with generous margin so border effects vanish.
        let mut img = block(12, 12, 5, 9, 5, 9);
        img[3 * 12 + 3] = 1.0; // interior noise, margin ≥ 3 from every border
        let se = StructuringElement::square(3).expect("se");
        let once = open(&img, 12, 12, &se).expect("open");
        let twice = open(&once, 12, 12, &se).expect("open");
        for (a, b) in once.iter().zip(twice.iter()) {
            assert!((a - b).abs() < 1e-6, "opening must be idempotent");
        }
    }

    #[test]
    fn closing_is_idempotent() {
        let mut img = block(12, 12, 4, 9, 4, 9);
        img[6 * 12 + 6] = 0.0; // interior hole
        let se = StructuringElement::square(3).expect("se");
        let once = close(&img, 12, 12, &se).expect("close");
        let twice = close(&once, 12, 12, &se).expect("close");
        for (a, b) in once.iter().zip(twice.iter()) {
            assert!((a - b).abs() < 1e-6, "closing must be idempotent");
        }
    }

    #[test]
    fn morph_gradient_outlines_point() {
        let img = single_pixel(5, 5, 2, 2);
        let se = StructuringElement::square(3).expect("se");
        let grad = morphological_gradient(&img, 5, 5, &se).expect("gradient");
        // erode → all zero, dilate → 3×3 block ⇒ gradient is the 3×3 block.
        let total: f32 = grad.iter().sum();
        assert_eq!(total, 9.0);
        assert!(grad.iter().all(|&v| v >= 0.0));
    }

    #[test]
    fn se_larger_than_image_errors() {
        let img = vec![0.0_f32; 4 * 4];
        let se = StructuringElement::square(7).expect("se");
        assert!(matches!(
            erode(&img, 4, 4, &se),
            Err(VisionError::Internal(_))
        ));
        assert!(matches!(
            dilate(&img, 4, 4, &se),
            Err(VisionError::Internal(_))
        ));
    }

    #[test]
    fn empty_se_errors() {
        // All-false mask → no members.
        let r = StructuringElement::new(3, 3, vec![false; 9]);
        assert!(matches!(r, Err(VisionError::EmptyInput(_))));
        // Zero dimension.
        assert!(matches!(
            StructuringElement::rect(0, 3),
            Err(VisionError::EmptyInput(_))
        ));
    }

    #[test]
    fn se_mask_length_mismatch_errors() {
        let r = StructuringElement::new(3, 3, vec![true; 8]);
        assert!(matches!(r, Err(VisionError::DimensionMismatch { .. })));
    }

    #[test]
    fn wrong_image_size_errors() {
        let img = vec![0.0_f32; 10];
        let se = StructuringElement::square(3).expect("se");
        assert!(matches!(
            erode(&img, 8, 8, &se),
            Err(VisionError::DimensionMismatch { .. })
        ));
    }

    #[test]
    fn zero_dim_image_errors() {
        let img: Vec<f32> = vec![];
        let se = StructuringElement::square(3).expect("se");
        assert!(matches!(
            dilate(&img, 0, 8, &se),
            Err(VisionError::InvalidImageSize { .. })
        ));
    }

    #[test]
    fn erode_is_anti_extensive_dilate_extensive() {
        // For any input, erosion ≤ f ≤ dilation pointwise (with a centred SE).
        let img = block(8, 8, 2, 6, 1, 5);
        let se = StructuringElement::square(3).expect("se");
        let er = erode(&img, 8, 8, &se).expect("erode");
        let di = dilate(&img, 8, 8, &se).expect("dilate");
        for ((&e, &f), &d) in er.iter().zip(img.iter()).zip(di.iter()) {
            assert!(e <= f + 1e-6, "erosion must be anti-extensive");
            assert!(d + 1e-6 >= f, "dilation must be extensive");
        }
    }
}