oximedia-cv 0.1.8

Computer vision for OxiMedia
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Saliency detection for content-aware scaling.
//!
//! This module provides various saliency detection algorithms to identify
//! important regions in images that should be preserved during scaling.

use crate::error::{CvError, CvResult};
use crate::image::{EdgeDetector, SobelEdge};
use std::f64::consts::PI;

/// Saliency detection method.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SaliencyMethod {
    /// Spectral residual method.
    #[default]
    SpectralResidual,
    /// Frequency-tuned salient region detection.
    FrequencyTuned,
    /// Edge-based saliency.
    EdgeBased,
    /// Color-based saliency.
    ColorBased,
    /// Combined method using multiple approaches.
    Combined,
}

impl SaliencyMethod {
    /// Compute saliency map.
    ///
    /// # Arguments
    ///
    /// * `image` - Grayscale image data
    /// * `width` - Image width
    /// * `height` - Image height
    ///
    /// # Returns
    ///
    /// Saliency map (0-255, higher values = more salient).
    ///
    /// # Errors
    ///
    /// Returns an error if dimensions are invalid.
    pub fn compute(&self, image: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
        match self {
            Self::SpectralResidual => compute_spectral_residual(image, width, height),
            Self::FrequencyTuned => compute_frequency_tuned(image, width, height),
            Self::EdgeBased => compute_edge_saliency(image, width, height),
            Self::ColorBased => compute_color_saliency(image, width, height),
            Self::Combined => compute_combined_saliency(image, width, height),
        }
    }
}

/// Saliency map.
#[derive(Debug, Clone)]
pub struct SaliencyMap {
    /// Saliency values (0-255).
    pub data: Vec<u8>,
    /// Map width.
    pub width: u32,
    /// Map height.
    pub height: u32,
}

impl SaliencyMap {
    /// Create a new saliency map.
    #[must_use]
    pub fn new(width: u32, height: u32) -> Self {
        let size = width as usize * height as usize;
        Self {
            data: vec![0u8; size],
            width,
            height,
        }
    }

    /// Create from raw data.
    pub fn from_data(data: Vec<u8>, width: u32, height: u32) -> CvResult<Self> {
        let expected = width as usize * height as usize;
        if data.len() != expected {
            return Err(CvError::insufficient_data(expected, data.len()));
        }
        Ok(Self {
            data,
            width,
            height,
        })
    }

    /// Get saliency at position.
    #[must_use]
    pub fn get(&self, x: u32, y: u32) -> u8 {
        if x >= self.width || y >= self.height {
            return 0;
        }
        self.data[y as usize * self.width as usize + x as usize]
    }

    /// Threshold to create binary mask.
    ///
    /// # Arguments
    ///
    /// * `threshold` - Threshold value (0-255)
    ///
    /// # Returns
    ///
    /// Binary mask (0 or 255).
    #[must_use]
    pub fn threshold(&self, threshold: u8) -> Vec<u8> {
        self.data
            .iter()
            .map(|&v| if v >= threshold { 255 } else { 0 })
            .collect()
    }

    /// Apply Gaussian blur to smooth saliency map.
    pub fn blur(&mut self, sigma: f64) {
        let kernel_size = (sigma * 3.0).ceil() as usize * 2 + 1;
        let kernel = create_gaussian_kernel(sigma, kernel_size);

        let blurred = separable_blur(
            &self.data,
            self.width as usize,
            self.height as usize,
            &kernel,
        );
        self.data = blurred;
    }

    /// Find salient regions above threshold.
    ///
    /// # Arguments
    ///
    /// * `threshold` - Minimum saliency value
    ///
    /// # Returns
    ///
    /// Vector of bounding boxes for salient regions.
    #[must_use]
    pub fn find_regions(&self, threshold: u8) -> Vec<SalientRegion> {
        let binary = self.threshold(threshold);
        find_connected_components(&binary, self.width, self.height)
    }
}

/// Salient region in an image.
#[derive(Debug, Clone)]
pub struct SalientRegion {
    /// Bounding box: (x, y, width, height).
    pub bbox: (u32, u32, u32, u32),
    /// Average saliency in region.
    pub avg_saliency: f64,
    /// Area in pixels.
    pub area: u32,
}

impl SalientRegion {
    /// Create a new salient region.
    #[must_use]
    pub const fn new(bbox: (u32, u32, u32, u32), avg_saliency: f64, area: u32) -> Self {
        Self {
            bbox,
            avg_saliency,
            area,
        }
    }

    /// Check if this region overlaps with another.
    #[must_use]
    pub fn overlaps(&self, other: &Self) -> bool {
        let (x1, y1, w1, h1) = self.bbox;
        let (x2, y2, w2, h2) = other.bbox;

        !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1)
    }

    /// Get center point.
    #[must_use]
    pub const fn center(&self) -> (u32, u32) {
        let (x, y, w, h) = self.bbox;
        (x + w / 2, y + h / 2)
    }
}

/// Compute spectral residual saliency.
///
/// Based on "Saliency Detection: A Spectral Residual Approach" (Hou & Zhang, 2007).
fn compute_spectral_residual(image: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
    if width == 0 || height == 0 {
        return Err(CvError::invalid_dimensions(width, height));
    }

    let size = width as usize * height as usize;
    if image.len() < size {
        return Err(CvError::insufficient_data(size, image.len()));
    }

    // Convert to float
    let mut float_image: Vec<f64> = image.iter().map(|&x| x as f64).collect();

    // Apply FFT (simplified - using DCT-like approach)
    let spectrum = compute_amplitude_spectrum(&float_image, width as usize, height as usize);

    // Compute log spectrum
    let mut log_spectrum: Vec<f64> = spectrum.iter().map(|&x| (x + 1.0).ln()).collect();

    // Apply average filter to get log spectrum average
    let avg_kernel = [1.0 / 9.0; 9]; // 3x3 average
    let log_avg = separable_blur_f64(
        &log_spectrum,
        width as usize,
        height as usize,
        &[1.0 / 3.0; 3],
    );

    // Compute spectral residual
    let mut residual = vec![0.0; size];
    for i in 0..size {
        residual[i] = log_spectrum[i] - log_avg[i];
    }

    // Inverse transform (simplified)
    let saliency_map = compute_inverse_spectrum(&residual, width as usize, height as usize);

    // Normalize to 0-255
    let mut max_val = 0.0f64;
    for &val in &saliency_map {
        max_val = max_val.max(val);
    }

    let result = if max_val > f64::EPSILON {
        saliency_map
            .iter()
            .map(|&x| ((x / max_val) * 255.0).round() as u8)
            .collect()
    } else {
        vec![0u8; size]
    };

    Ok(result)
}

/// Compute frequency-tuned salient region detection.
///
/// Based on "Frequency-tuned Salient Region Detection" (Achanta et al., 2009).
fn compute_frequency_tuned(image: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
    if width == 0 || height == 0 {
        return Err(CvError::invalid_dimensions(width, height));
    }

    let size = width as usize * height as usize;
    if image.len() < size {
        return Err(CvError::insufficient_data(size, image.len()));
    }

    // Compute mean pixel value
    let mean: f64 = image.iter().map(|&x| x as f64).sum::<f64>() / size as f64;

    // Blur image
    let kernel = create_gaussian_kernel(2.0, 5);
    let blurred = separable_blur(image, width as usize, height as usize, &kernel);

    // Compute saliency as difference from mean
    let mut saliency = vec![0.0; size];
    for i in 0..size {
        saliency[i] = (blurred[i] as f64 - mean).abs();
    }

    // Normalize
    let max_sal = saliency.iter().copied().fold(0.0f64, f64::max);
    let result = if max_sal > f64::EPSILON {
        saliency
            .iter()
            .map(|&x| ((x / max_sal) * 255.0).round() as u8)
            .collect()
    } else {
        vec![128u8; size]
    };

    Ok(result)
}

/// Compute edge-based saliency.
fn compute_edge_saliency(image: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
    let sobel = SobelEdge::new();
    sobel.detect(image, width, height)
}

/// Compute color-based saliency for grayscale (uses texture).
fn compute_color_saliency(image: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
    if width == 0 || height == 0 {
        return Err(CvError::invalid_dimensions(width, height));
    }

    let size = width as usize * height as usize;
    if image.len() < size {
        return Err(CvError::insufficient_data(size, image.len()));
    }

    // For grayscale, use local variance as proxy for "color" variation
    let w = width as usize;
    let h = height as usize;
    let mut saliency = vec![0.0; size];

    const WINDOW: i32 = 5;
    const HALF: i32 = WINDOW / 2;

    for y in 0..h {
        for x in 0..w {
            let mut sum = 0.0;
            let mut sum_sq = 0.0;
            let mut count = 0;

            for dy in -HALF..=HALF {
                for dx in -HALF..=HALF {
                    let nx = x as i32 + dx;
                    let ny = y as i32 + dy;

                    if nx >= 0 && nx < w as i32 && ny >= 0 && ny < h as i32 {
                        let val = image[ny as usize * w + nx as usize] as f64;
                        sum += val;
                        sum_sq += val * val;
                        count += 1;
                    }
                }
            }

            if count > 0 {
                let mean = sum / count as f64;
                let variance = (sum_sq / count as f64) - (mean * mean);
                saliency[y * w + x] = variance.sqrt();
            }
        }
    }

    // Normalize
    let max_sal = saliency.iter().copied().fold(0.0f64, f64::max);
    let result = if max_sal > f64::EPSILON {
        saliency
            .iter()
            .map(|&x| ((x / max_sal) * 255.0).round() as u8)
            .collect()
    } else {
        vec![128u8; size]
    };

    Ok(result)
}

/// Compute combined saliency using multiple methods.
fn compute_combined_saliency(image: &[u8], width: u32, height: u32) -> CvResult<Vec<u8>> {
    let edge = compute_edge_saliency(image, width, height)?;
    let freq = compute_frequency_tuned(image, width, height)?;

    // Combine with equal weights
    let size = width as usize * height as usize;
    let mut combined = vec![0u8; size];

    for i in 0..size {
        let val = (edge[i] as u16 + freq[i] as u16) / 2;
        combined[i] = val as u8;
    }

    Ok(combined)
}

/// Compute amplitude spectrum (simplified FFT approximation).
fn compute_amplitude_spectrum(image: &[f64], width: usize, height: usize) -> Vec<f64> {
    let size = width * height;
    let mut spectrum = vec![0.0; size];

    // Simple approximation using DCT-like transform
    for v in 0..height {
        for u in 0..width {
            let mut sum = 0.0;
            for y in 0..height {
                for x in 0..width {
                    let val = image[y * width + x];
                    let angle = PI * ((2 * x + 1) * u) as f64 / (2.0 * width as f64)
                        + PI * ((2 * y + 1) * v) as f64 / (2.0 * height as f64);
                    sum += val * angle.cos();
                }
            }
            spectrum[v * width + u] = sum.abs();
        }
    }

    spectrum
}

/// Compute inverse spectrum (simplified).
fn compute_inverse_spectrum(spectrum: &[f64], width: usize, height: usize) -> Vec<f64> {
    let size = width * height;
    let mut image = vec![0.0; size];

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;
            for v in 0..height {
                for u in 0..width {
                    let val = spectrum[v * width + u].exp();
                    let angle = PI * ((2 * x + 1) * u) as f64 / (2.0 * width as f64)
                        + PI * ((2 * y + 1) * v) as f64 / (2.0 * height as f64);
                    sum += val * angle.cos();
                }
            }
            image[y * width + x] = sum * sum; // Square for intensity
        }
    }

    image
}

/// Create a 1D Gaussian kernel.
fn create_gaussian_kernel(sigma: f64, size: usize) -> Vec<f64> {
    let half = size / 2;
    let mut kernel = Vec::with_capacity(size);
    let mut sum = 0.0;

    let two_sigma_sq = 2.0 * sigma * sigma;

    for i in 0..size {
        let x = i as f64 - half as f64;
        let value = (-x * x / two_sigma_sq).exp();
        kernel.push(value);
        sum += value;
    }

    // Normalize
    for v in &mut kernel {
        *v /= sum;
    }

    kernel
}

/// Apply separable Gaussian blur.
fn separable_blur(image: &[u8], width: usize, height: usize, kernel: &[f64]) -> Vec<u8> {
    let temp = blur_horizontal(image, width, height, kernel);
    blur_vertical(&temp, width, height, kernel)
}

/// Apply separable Gaussian blur on f64 data.
fn separable_blur_f64(image: &[f64], width: usize, height: usize, kernel: &[f64]) -> Vec<f64> {
    let temp = blur_horizontal_f64(image, width, height, kernel);
    blur_vertical_f64(&temp, width, height, kernel)
}

/// Horizontal blur pass.
fn blur_horizontal(image: &[u8], width: usize, height: usize, kernel: &[f64]) -> Vec<f64> {
    let half = kernel.len() / 2;
    let mut result = vec![0.0; width * height];

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;
            for (i, &k) in kernel.iter().enumerate() {
                let xi = (x as i32 + i as i32 - half as i32).clamp(0, width as i32 - 1) as usize;
                sum += image[y * width + xi] as f64 * k;
            }
            result[y * width + x] = sum;
        }
    }

    result
}

/// Vertical blur pass.
fn blur_vertical(image: &[f64], width: usize, height: usize, kernel: &[f64]) -> Vec<u8> {
    let half = kernel.len() / 2;
    let mut result = vec![0u8; width * height];

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;
            for (i, &k) in kernel.iter().enumerate() {
                let yi = (y as i32 + i as i32 - half as i32).clamp(0, height as i32 - 1) as usize;
                sum += image[yi * width + x] * k;
            }
            result[y * width + x] = sum.round().clamp(0.0, 255.0) as u8;
        }
    }

    result
}

/// Horizontal blur pass for f64 data.
fn blur_horizontal_f64(image: &[f64], width: usize, height: usize, kernel: &[f64]) -> Vec<f64> {
    let half = kernel.len() / 2;
    let mut result = vec![0.0; width * height];

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;
            for (i, &k) in kernel.iter().enumerate() {
                let xi = (x as i32 + i as i32 - half as i32).clamp(0, width as i32 - 1) as usize;
                sum += image[y * width + xi] * k;
            }
            result[y * width + x] = sum;
        }
    }

    result
}

/// Vertical blur pass for f64 data.
fn blur_vertical_f64(image: &[f64], width: usize, height: usize, kernel: &[f64]) -> Vec<f64> {
    let half = kernel.len() / 2;
    let mut result = vec![0.0; width * height];

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;
            for (i, &k) in kernel.iter().enumerate() {
                let yi = (y as i32 + i as i32 - half as i32).clamp(0, height as i32 - 1) as usize;
                sum += image[yi * width + x] * k;
            }
            result[y * width + x] = sum;
        }
    }

    result
}

/// Find connected components in binary image.
fn find_connected_components(binary: &[u8], width: u32, height: u32) -> Vec<SalientRegion> {
    let w = width as usize;
    let h = height as usize;
    let mut labels = vec![0i32; w * h];
    let mut next_label = 1i32;

    // Two-pass connected components
    // First pass: assign initial labels
    for y in 0..h {
        for x in 0..w {
            if binary[y * w + x] > 0 {
                let mut neighbors = Vec::new();

                if x > 0 && labels[y * w + x - 1] > 0 {
                    neighbors.push(labels[y * w + x - 1]);
                }
                if y > 0 && labels[(y - 1) * w + x] > 0 {
                    neighbors.push(labels[(y - 1) * w + x]);
                }

                if neighbors.is_empty() {
                    labels[y * w + x] = next_label;
                    next_label += 1;
                } else {
                    labels[y * w + x] = neighbors.iter().copied().min().unwrap_or(next_label);
                }
            }
        }
    }

    // Extract regions
    let mut regions = Vec::new();
    for label in 1..next_label {
        let mut min_x = w;
        let mut min_y = h;
        let mut max_x = 0;
        let mut max_y = 0;
        let mut area = 0;

        for y in 0..h {
            for x in 0..w {
                if labels[y * w + x] == label {
                    min_x = min_x.min(x);
                    min_y = min_y.min(y);
                    max_x = max_x.max(x);
                    max_y = max_y.max(y);
                    area += 1;
                }
            }
        }

        if area > 0 {
            let bbox = (
                min_x as u32,
                min_y as u32,
                (max_x - min_x + 1) as u32,
                (max_y - min_y + 1) as u32,
            );
            regions.push(SalientRegion::new(bbox, 255.0, area));
        }
    }

    regions
}

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

    #[test]
    fn test_saliency_map_new() {
        let map = SaliencyMap::new(10, 10);
        assert_eq!(map.width, 10);
        assert_eq!(map.height, 10);
        assert_eq!(map.data.len(), 100);
    }

    #[test]
    fn test_saliency_threshold() {
        let mut map = SaliencyMap::new(10, 10);
        map.data.fill(200);
        let binary = map.threshold(128);
        assert!(binary.iter().all(|&v| v == 255));
    }

    #[test]
    fn test_edge_saliency() {
        let image = vec![128u8; 100];
        let result =
            compute_edge_saliency(&image, 10, 10).expect("compute_edge_saliency should succeed");
        assert_eq!(result.len(), 100);
    }

    #[test]
    fn test_frequency_tuned() {
        let image = vec![128u8; 100];
        let result = compute_frequency_tuned(&image, 10, 10)
            .expect("compute_frequency_tuned should succeed");
        assert_eq!(result.len(), 100);
    }

    #[test]
    fn test_combined_saliency() {
        let image = vec![128u8; 100];
        let result = compute_combined_saliency(&image, 10, 10)
            .expect("compute_combined_saliency should succeed");
        assert_eq!(result.len(), 100);
    }

    #[test]
    fn test_salient_region() {
        let region = SalientRegion::new((10, 20, 30, 40), 0.8, 100);
        assert_eq!(region.center(), (25, 40));
        assert_eq!(region.area, 100);
    }

    #[test]
    fn test_region_overlap() {
        let r1 = SalientRegion::new((0, 0, 10, 10), 0.8, 100);
        let r2 = SalientRegion::new((5, 5, 10, 10), 0.8, 100);
        let r3 = SalientRegion::new((20, 20, 10, 10), 0.8, 100);

        assert!(r1.overlaps(&r2));
        assert!(!r1.overlaps(&r3));
    }
}