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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Object protection masks for content-aware scaling.
//!
//! This module provides functionality to create protection masks that
//! prevent important regions (faces, objects, etc.) from being removed
//! during seam carving.

use super::saliency::{SaliencyMap, SaliencyMethod, SalientRegion};
use crate::error::{CvError, CvResult};

/// Protection mask for content-aware scaling.
///
/// Protected regions have high values (255) and will be avoided
/// during seam removal.
#[derive(Debug, Clone)]
pub struct ProtectionMask {
    /// Mask data (0 = unprotected, 255 = fully protected).
    pub data: Vec<u8>,
    /// Mask width.
    pub width: u32,
    /// Mask height.
    pub height: u32,
}

impl ProtectionMask {
    /// Create a new empty protection mask.
    #[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 protection value 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]
    }

    /// Set protection value at position.
    pub fn set(&mut self, x: u32, y: u32, value: u8) {
        if x < self.width && y < self.height {
            self.data[y as usize * self.width as usize + x as usize] = value;
        }
    }

    /// Protect a rectangular region.
    ///
    /// # Arguments
    ///
    /// * `x` - Region x coordinate
    /// * `y` - Region y coordinate
    /// * `width` - Region width
    /// * `height` - Region height
    /// * `value` - Protection value (0-255)
    pub fn protect_rect(&mut self, x: u32, y: u32, width: u32, height: u32, value: u8) {
        let x1 = x.min(self.width);
        let y1 = y.min(self.height);
        let x2 = (x + width).min(self.width);
        let y2 = (y + height).min(self.height);

        for py in y1..y2 {
            for px in x1..x2 {
                self.set(px, py, value);
            }
        }
    }

    /// Protect a circular region.
    ///
    /// # Arguments
    ///
    /// * `cx` - Circle center x
    /// * `cy` - Circle center y
    /// * `radius` - Circle radius
    /// * `value` - Protection value (0-255)
    pub fn protect_circle(&mut self, cx: u32, cy: u32, radius: u32, value: u8) {
        let r_sq = radius * radius;

        let min_x = cx.saturating_sub(radius);
        let max_x = (cx + radius).min(self.width);
        let min_y = cy.saturating_sub(radius);
        let max_y = (cy + radius).min(self.height);

        for y in min_y..max_y {
            for x in min_x..max_x {
                let dx = x as i32 - cx as i32;
                let dy = y as i32 - cy as i32;
                let dist_sq = (dx * dx + dy * dy) as u32;

                if dist_sq <= r_sq {
                    self.set(x, y, value);
                }
            }
        }
    }

    /// Protect an elliptical region.
    ///
    /// # Arguments
    ///
    /// * `cx` - Ellipse center x
    /// * `cy` - Ellipse center y
    /// * `rx` - X-axis radius
    /// * `ry` - Y-axis radius
    /// * `value` - Protection value (0-255)
    pub fn protect_ellipse(&mut self, cx: u32, cy: u32, rx: u32, ry: u32, value: u8) {
        let min_x = cx.saturating_sub(rx);
        let max_x = (cx + rx).min(self.width);
        let min_y = cy.saturating_sub(ry);
        let max_y = (cy + ry).min(self.height);

        for y in min_y..max_y {
            for x in min_x..max_x {
                let dx = (x as i32 - cx as i32) as f64;
                let dy = (y as i32 - cy as i32) as f64;

                // Ellipse equation: (dx/rx)^2 + (dy/ry)^2 <= 1
                let norm = (dx * dx) / (rx * rx) as f64 + (dy * dy) / (ry * ry) as f64;

                if norm <= 1.0 {
                    self.set(x, y, value);
                }
            }
        }
    }

    /// Dilate the protection mask to expand protected regions.
    ///
    /// # Arguments
    ///
    /// * `iterations` - Number of dilation iterations
    pub fn dilate(&mut self, iterations: u32) {
        for _ in 0..iterations {
            self.dilate_once();
        }
    }

    /// Single dilation iteration.
    fn dilate_once(&mut self) {
        let w = self.width as usize;
        let h = self.height as usize;
        let mut new_data = self.data.clone();

        for y in 0..h {
            for x in 0..w {
                let mut max_val = self.data[y * w + x];

                // Check 8-connected neighbors
                for dy in -1i32..=1 {
                    for dx in -1i32..=1 {
                        if dx == 0 && dy == 0 {
                            continue;
                        }

                        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 {
                            max_val = max_val.max(self.data[ny as usize * w + nx as usize]);
                        }
                    }
                }

                new_data[y * w + x] = max_val;
            }
        }

        self.data = new_data;
    }

    /// Erode the protection mask to shrink protected regions.
    ///
    /// # Arguments
    ///
    /// * `iterations` - Number of erosion iterations
    pub fn erode(&mut self, iterations: u32) {
        for _ in 0..iterations {
            self.erode_once();
        }
    }

    /// Single erosion iteration.
    fn erode_once(&mut self) {
        let w = self.width as usize;
        let h = self.height as usize;
        let mut new_data = self.data.clone();

        for y in 0..h {
            for x in 0..w {
                let mut min_val = self.data[y * w + x];

                // Check 8-connected neighbors
                for dy in -1i32..=1 {
                    for dx in -1i32..=1 {
                        if dx == 0 && dy == 0 {
                            continue;
                        }

                        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 {
                            min_val = min_val.min(self.data[ny as usize * w + nx as usize]);
                        }
                    }
                }

                new_data[y * w + x] = min_val;
            }
        }

        self.data = new_data;
    }

    /// Combine with another protection mask using maximum.
    pub fn merge(&mut self, other: &Self) {
        let size = self.data.len().min(other.data.len());
        for i in 0..size {
            self.data[i] = self.data[i].max(other.data[i]);
        }
    }

    /// Apply Gaussian blur to smooth the protection mask.
    ///
    /// # Arguments
    ///
    /// * `sigma` - Gaussian blur sigma
    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;
    }

    /// Create protection mask from face regions.
    ///
    /// # Arguments
    ///
    /// * `width` - Image width
    /// * `height` - Image height
    /// * `faces` - Face bounding boxes: (x, y, width, height)
    /// * `padding` - Padding around face boxes
    ///
    /// # Returns
    ///
    /// Protection mask with faces protected.
    #[must_use]
    pub fn from_faces(
        width: u32,
        height: u32,
        faces: &[(u32, u32, u32, u32)],
        padding: u32,
    ) -> Self {
        let mut mask = Self::new(width, height);

        for &(fx, fy, fw, fh) in faces {
            // Add padding
            let x = fx.saturating_sub(padding);
            let y = fy.saturating_sub(padding);
            let w = fw + 2 * padding;
            let h = fh + 2 * padding;

            mask.protect_rect(x, y, w, h, 255);
        }

        mask
    }

    /// Create protection mask from saliency map.
    ///
    /// # Arguments
    ///
    /// * `saliency` - Saliency map
    /// * `threshold` - Saliency threshold (0-255)
    ///
    /// # Returns
    ///
    /// Protection mask based on salient regions.
    #[must_use]
    pub fn from_saliency(saliency: &SaliencyMap, threshold: u8) -> Self {
        let binary = saliency.threshold(threshold);
        Self {
            data: binary,
            width: saliency.width,
            height: saliency.height,
        }
    }

    /// Create protection mask from salient regions.
    ///
    /// # Arguments
    ///
    /// * `width` - Image width
    /// * `height` - Image height
    /// * `regions` - Salient regions
    /// * `padding` - Padding around regions
    ///
    /// # Returns
    ///
    /// Protection mask with regions protected.
    #[must_use]
    pub fn from_regions(width: u32, height: u32, regions: &[SalientRegion], padding: u32) -> Self {
        let mut mask = Self::new(width, height);

        for region in regions {
            let (rx, ry, rw, rh) = region.bbox;
            let x = rx.saturating_sub(padding);
            let y = ry.saturating_sub(padding);
            let w = rw + 2 * padding;
            let h = rh + 2 * padding;

            // Protection value based on region saliency
            let value = (region.avg_saliency * 255.0).round() as u8;
            mask.protect_rect(x, y, w, h, value);
        }

        mask
    }

    /// Create protection mask from image gradient.
    ///
    /// High-gradient regions are protected.
    ///
    /// # Arguments
    ///
    /// * `image` - Grayscale image
    /// * `width` - Image width
    /// * `height` - Image height
    /// * `threshold` - Gradient threshold
    ///
    /// # Returns
    ///
    /// Protection mask based on gradients.
    pub fn from_gradient(image: &[u8], width: u32, height: u32, threshold: f64) -> CvResult<Self> {
        use crate::image::SobelEdge;

        let sobel = SobelEdge::new();
        let (magnitude, _) = sobel.gradient_with_direction(image, width, height)?;

        let mut mask_data = vec![0u8; magnitude.len()];
        for (i, &mag) in magnitude.iter().enumerate() {
            if mag > threshold {
                mask_data[i] = ((mag / threshold) * 255.0).min(255.0) as u8;
            }
        }

        Self::from_data(mask_data, width, height)
    }

    /// Invert the protection mask.
    pub fn invert(&mut self) {
        for value in &mut self.data {
            *value = 255 - *value;
        }
    }

    /// Scale protection values.
    ///
    /// # Arguments
    ///
    /// * `scale` - Scale factor (1.0 = no change)
    pub fn scale_values(&mut self, scale: f64) {
        for value in &mut self.data {
            let scaled = (*value as f64 * scale).round().clamp(0.0, 255.0) as u8;
            *value = scaled;
        }
    }
}

/// 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)
}

/// 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
}

/// Builder for creating complex protection masks.
#[derive(Debug)]
pub struct ProtectionMaskBuilder {
    width: u32,
    height: u32,
    faces: Vec<(u32, u32, u32, u32)>,
    regions: Vec<SalientRegion>,
    saliency_threshold: Option<u8>,
    gradient_threshold: Option<f64>,
    padding: u32,
    blur_sigma: Option<f64>,
    dilation: u32,
}

impl ProtectionMaskBuilder {
    /// Create a new protection mask builder.
    #[must_use]
    pub const fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
            faces: Vec::new(),
            regions: Vec::new(),
            saliency_threshold: None,
            gradient_threshold: None,
            padding: 0,
            blur_sigma: None,
            dilation: 0,
        }
    }

    /// Add face regions to protect.
    #[must_use]
    pub fn with_faces(mut self, faces: Vec<(u32, u32, u32, u32)>) -> Self {
        self.faces = faces;
        self
    }

    /// Add salient regions to protect.
    #[must_use]
    pub fn with_regions(mut self, regions: Vec<SalientRegion>) -> Self {
        self.regions = regions;
        self
    }

    /// Set saliency threshold.
    #[must_use]
    pub const fn with_saliency_threshold(mut self, threshold: u8) -> Self {
        self.saliency_threshold = Some(threshold);
        self
    }

    /// Set gradient threshold.
    #[must_use]
    pub const fn with_gradient_threshold(mut self, threshold: f64) -> Self {
        self.gradient_threshold = Some(threshold);
        self
    }

    /// Set padding around protected regions.
    #[must_use]
    pub const fn with_padding(mut self, padding: u32) -> Self {
        self.padding = padding;
        self
    }

    /// Set blur sigma for smoothing.
    #[must_use]
    pub const fn with_blur(mut self, sigma: f64) -> Self {
        self.blur_sigma = Some(sigma);
        self
    }

    /// Set dilation iterations.
    #[must_use]
    pub const fn with_dilation(mut self, iterations: u32) -> Self {
        self.dilation = iterations;
        self
    }

    /// Build the protection mask.
    #[must_use]
    pub fn build(self) -> ProtectionMask {
        let mut mask = ProtectionMask::new(self.width, self.height);

        // Add face protections
        if !self.faces.is_empty() {
            let face_mask =
                ProtectionMask::from_faces(self.width, self.height, &self.faces, self.padding);
            mask.merge(&face_mask);
        }

        // Add region protections
        if !self.regions.is_empty() {
            let region_mask =
                ProtectionMask::from_regions(self.width, self.height, &self.regions, self.padding);
            mask.merge(&region_mask);
        }

        // Apply dilation if specified
        if self.dilation > 0 {
            mask.dilate(self.dilation);
        }

        // Apply blur if specified
        if let Some(sigma) = self.blur_sigma {
            mask.blur(sigma);
        }

        mask
    }
}

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

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

    #[test]
    fn test_protect_rect() {
        let mut mask = ProtectionMask::new(10, 10);
        mask.protect_rect(2, 2, 4, 4, 255);
        assert_eq!(mask.get(3, 3), 255);
        assert_eq!(mask.get(0, 0), 0);
    }

    #[test]
    fn test_protect_circle() {
        let mut mask = ProtectionMask::new(10, 10);
        mask.protect_circle(5, 5, 3, 255);
        assert_eq!(mask.get(5, 5), 255); // Center
        assert_eq!(mask.get(0, 0), 0); // Outside
    }

    #[test]
    fn test_protect_ellipse() {
        let mut mask = ProtectionMask::new(20, 20);
        mask.protect_ellipse(10, 10, 5, 3, 255);
        assert_eq!(mask.get(10, 10), 255); // Center
    }

    #[test]
    fn test_dilate() {
        let mut mask = ProtectionMask::new(10, 10);
        mask.set(5, 5, 255);
        mask.dilate(1);
        // Check that neighbors are now protected
        assert!(mask.get(4, 5) > 0);
        assert!(mask.get(6, 5) > 0);
    }

    #[test]
    fn test_erode() {
        let mut mask = ProtectionMask::new(10, 10);
        mask.protect_rect(3, 3, 4, 4, 255);
        mask.erode(1);
        // Edges should be eroded
        let center_val = mask.get(5, 5);
        assert!(center_val > 0);
    }

    #[test]
    fn test_merge() {
        let mut mask1 = ProtectionMask::new(10, 10);
        let mut mask2 = ProtectionMask::new(10, 10);
        mask1.set(0, 0, 100);
        mask2.set(0, 0, 200);
        mask1.merge(&mask2);
        assert_eq!(mask1.get(0, 0), 200);
    }

    #[test]
    fn test_from_faces() {
        let faces = vec![(10, 10, 20, 20)];
        let mask = ProtectionMask::from_faces(100, 100, &faces, 5);
        assert!(mask.get(15, 15) > 0);
    }

    #[test]
    fn test_invert() {
        let mut mask = ProtectionMask::new(10, 10);
        mask.data.fill(100);
        mask.invert();
        assert_eq!(mask.get(0, 0), 155);
    }

    #[test]
    fn test_scale_values() {
        let mut mask = ProtectionMask::new(10, 10);
        mask.data.fill(100);
        mask.scale_values(2.0);
        assert_eq!(mask.get(0, 0), 200);
    }

    #[test]
    fn test_builder() {
        let mask = ProtectionMaskBuilder::new(100, 100)
            .with_faces(vec![(10, 10, 20, 20)])
            .with_padding(5)
            .with_dilation(1)
            .build();

        assert!(mask.get(15, 15) > 0);
    }
}