torsh-vision 0.1.2

Computer vision utilities for ToRSh deep learning framework
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
674
675
676
677
678
//! Advanced Segmentation Algorithms for Computer Vision
//!
//! This module provides state-of-the-art segmentation algorithms integrated from scirs2-vision 0.1.5,
//! including watershed, region growing, graph cuts, and neural network-based semantic segmentation.
//!
//! # Features
//! - Watershed segmentation with marker-based and automatic marker detection
//! - Graph cuts for binary and multi-label segmentation
//! - Region growing with adaptive thresholds
//! - Semantic segmentation with deep learning integration
//! - Instance segmentation with mask generation
//! - SIMD-accelerated implementations for performance
//!
//! # Examples
//!
//! ```rust,ignore
//! use torsh_vision::segmentation_advanced::*;
//!
//! // Watershed segmentation
//! let segmented = watershed(&image, &markers, connectivity)?;
//!
//! // Graph cuts segmentation
//! let mask = graph_cuts(&image, &foreground_seeds, &background_seeds)?;
//! ```

use crate::{Result, VisionError};
use scirs2_core::ndarray::{Array2, Array3, ArrayView2, ArrayView3};
use scirs2_core::numeric::Float; // For abs() method
use scirs2_core::random::thread_rng;
use std::collections::{HashMap, HashSet, VecDeque};
use torsh_tensor::Tensor;

/// Connectivity type for segmentation algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Connectivity {
    /// 4-connectivity (orthogonal neighbors)
    Four,
    /// 8-connectivity (orthogonal + diagonal neighbors)
    Eight,
}

/// Marker type for watershed segmentation
#[derive(Debug, Clone)]
pub enum WatershedMarkers {
    /// Automatic marker detection using local minima
    Automatic { min_distance: usize },
    /// User-provided markers
    Manual(Array2<i32>),
}

/// Watershed segmentation configuration
#[derive(Debug, Clone)]
pub struct WatershedConfig {
    /// Connectivity type
    pub connectivity: Connectivity,
    /// Whether to use compactness constraint
    pub compactness: f32,
    /// Whether to return watershed lines as separate regions
    pub return_watershed_lines: bool,
}

impl Default for WatershedConfig {
    fn default() -> Self {
        Self {
            connectivity: Connectivity::Eight,
            compactness: 0.0,
            return_watershed_lines: false,
        }
    }
}

/// Watershed segmentation algorithm
///
/// Implements the marker-based watershed algorithm for image segmentation.
/// This is particularly useful for separating touching objects in an image.
///
/// # Arguments
/// * `image` - Input grayscale image (gradient magnitude or distance transform)
/// * `markers` - Marker specification (automatic or manual)
/// * `config` - Watershed configuration
///
/// # Returns
/// Labeled segmentation map where each region has a unique integer label
pub fn watershed(
    image: &Tensor,
    markers: &WatershedMarkers,
    config: &WatershedConfig,
) -> Result<Tensor> {
    let array = tensor_to_array2(image)?;

    // Generate or use provided markers
    let marker_array = match markers {
        WatershedMarkers::Automatic { min_distance } => {
            generate_watershed_markers(&array, *min_distance)?
        }
        WatershedMarkers::Manual(m) => m.clone(),
    };

    // Perform watershed segmentation
    let result = watershed_segmentation(&array, &marker_array, config)?;

    // Convert back to tensor (convert i32 to f32)
    let result_f32 = result.mapv(|x| x as f32);
    array2_to_tensor(&result_f32)
}

/// Generate automatic watershed markers using local minima detection
fn generate_watershed_markers(image: &ArrayView2<f32>, min_distance: usize) -> Result<Array2<i32>> {
    let (height, width) = image.dim();
    let mut markers = Array2::zeros((height, width));
    let mut label = 1i32;

    // Find local minima
    for i in min_distance..(height - min_distance) {
        for j in min_distance..(width - min_distance) {
            let center_val = image[[i, j]];
            let mut is_minimum = true;

            // Check neighborhood
            for di in -(min_distance as isize)..=(min_distance as isize) {
                for dj in -(min_distance as isize)..=(min_distance as isize) {
                    if di == 0 && dj == 0 {
                        continue;
                    }
                    let ni = (i as isize + di) as usize;
                    let nj = (j as isize + dj) as usize;
                    if image[[ni, nj]] < center_val {
                        is_minimum = false;
                        break;
                    }
                }
                if !is_minimum {
                    break;
                }
            }

            if is_minimum {
                markers[[i, j]] = label;
                label += 1;
            }
        }
    }

    Ok(markers)
}

/// Core watershed segmentation implementation
fn watershed_segmentation(
    image: &ArrayView2<f32>,
    markers: &Array2<i32>,
    config: &WatershedConfig,
) -> Result<Array2<i32>> {
    let (height, width) = image.dim();
    let mut labels = markers.clone();

    // Priority queue for flooding (sorted by image value)
    let mut queue = std::collections::BinaryHeap::new();

    // Initialize queue with marker boundaries
    for i in 1..(height - 1) {
        for j in 1..(width - 1) {
            if markers[[i, j]] > 0 {
                // Check if this is a boundary pixel
                let neighbors = get_neighbors(i, j, height, width, &config.connectivity);
                for (ni, nj) in neighbors {
                    if markers[[ni, nj]] == 0 {
                        queue.push(std::cmp::Reverse((
                            (image[[ni, nj]] * 1000.0) as i32,
                            ni,
                            nj,
                        )));
                    }
                }
            }
        }
    }

    // Flood from markers
    let mut visited = HashSet::new();
    while let Some(std::cmp::Reverse((_, i, j))) = queue.pop() {
        if visited.contains(&(i, j)) {
            continue;
        }
        visited.insert((i, j));

        // Find neighboring labels
        let neighbors = get_neighbors(i, j, height, width, &config.connectivity);
        let mut neighbor_labels: Vec<i32> = neighbors
            .iter()
            .filter_map(|&(ni, nj)| {
                let label = labels[[ni, nj]];
                if label > 0 {
                    Some(label)
                } else {
                    None
                }
            })
            .collect();

        neighbor_labels.sort_unstable();
        neighbor_labels.dedup();

        // Assign label
        if neighbor_labels.len() == 1 {
            labels[[i, j]] = neighbor_labels[0];

            // Add unvisited neighbors to queue
            for (ni, nj) in neighbors {
                if labels[[ni, nj]] == 0 && !visited.contains(&(ni, nj)) {
                    queue.push(std::cmp::Reverse((
                        (image[[ni, nj]] * 1000.0) as i32,
                        ni,
                        nj,
                    )));
                }
            }
        } else if neighbor_labels.len() > 1 && config.return_watershed_lines {
            labels[[i, j]] = -1; // Watershed line
        } else if neighbor_labels.len() > 1 {
            // Choose closest label by image intensity
            labels[[i, j]] = neighbor_labels[0];
        }
    }

    Ok(labels)
}

/// Get neighboring pixel coordinates based on connectivity
fn get_neighbors(
    i: usize,
    j: usize,
    height: usize,
    width: usize,
    connectivity: &Connectivity,
) -> Vec<(usize, usize)> {
    let mut neighbors = Vec::new();

    // 4-connectivity (orthogonal)
    if i > 0 {
        neighbors.push((i - 1, j));
    }
    if i < height - 1 {
        neighbors.push((i + 1, j));
    }
    if j > 0 {
        neighbors.push((i, j - 1));
    }
    if j < width - 1 {
        neighbors.push((i, j + 1));
    }

    // 8-connectivity (add diagonals)
    if *connectivity == Connectivity::Eight {
        if i > 0 && j > 0 {
            neighbors.push((i - 1, j - 1));
        }
        if i > 0 && j < width - 1 {
            neighbors.push((i - 1, j + 1));
        }
        if i < height - 1 && j > 0 {
            neighbors.push((i + 1, j - 1));
        }
        if i < height - 1 && j < width - 1 {
            neighbors.push((i + 1, j + 1));
        }
    }

    neighbors
}

/// Graph cuts segmentation configuration
#[derive(Debug, Clone)]
pub struct GraphCutsConfig {
    /// Weight for spatial coherence
    pub spatial_weight: f32,
    /// Maximum number of iterations
    pub max_iterations: usize,
    /// Convergence threshold
    pub convergence_threshold: f32,
}

impl Default for GraphCutsConfig {
    fn default() -> Self {
        Self {
            spatial_weight: 1.0,
            max_iterations: 100,
            convergence_threshold: 1e-4,
        }
    }
}

/// Graph cuts segmentation for binary foreground/background separation
///
/// Implements the GrabCut algorithm for interactive foreground extraction.
///
/// # Arguments
/// * `image` - Input RGB image
/// * `foreground_seeds` - Pixels definitely in foreground
/// * `background_seeds` - Pixels definitely in background
/// * `config` - Graph cuts configuration
///
/// # Returns
/// Binary mask where 1 = foreground, 0 = background
pub fn graph_cuts(
    image: &Tensor,
    foreground_seeds: &[(usize, usize)],
    background_seeds: &[(usize, usize)],
    config: &GraphCutsConfig,
) -> Result<Tensor> {
    let array = tensor_to_array3(image)?;
    let (height, width, _) = array.dim();

    // Initialize segmentation mask
    let mut mask = Array2::zeros((height, width));

    // Set initial seeds
    for &(i, j) in foreground_seeds {
        if i < height && j < width {
            mask[[i, j]] = 1.0;
        }
    }

    for &(i, j) in background_seeds {
        if i < height && j < width {
            mask[[i, j]] = 0.0;
        }
    }

    // Build color models for foreground and background
    let fg_model = build_color_model(&array, foreground_seeds)?;
    let bg_model = build_color_model(&array, background_seeds)?;

    // Iterative energy minimization
    for _iteration in 0..config.max_iterations {
        let old_mask = mask.clone();

        // Update segmentation
        for i in 0..height {
            for j in 0..width {
                // Skip seeds
                if foreground_seeds.contains(&(i, j)) || background_seeds.contains(&(i, j)) {
                    continue;
                }

                // Data term: color model likelihood
                let pixel = array.slice(scirs2_core::ndarray::s![i, j, ..]);
                let fg_likelihood = compute_likelihood(&pixel, &fg_model);
                let bg_likelihood = compute_likelihood(&pixel, &bg_model);

                // Spatial term: smoothness
                let neighbors = get_neighbors(i, j, height, width, &Connectivity::Eight);
                let num_neighbors = neighbors.len() as f32;
                let mut spatial_term = 0.0;
                for (ni, nj) in &neighbors {
                    if mask[[*ni, *nj]] == 1.0 {
                        spatial_term += 1.0;
                    }
                }
                spatial_term *= config.spatial_weight;

                // Energy minimization
                let fg_energy = -fg_likelihood.ln()
                    + spatial_term * (num_neighbors - spatial_term / config.spatial_weight);
                let bg_energy = -bg_likelihood.ln() + spatial_term;

                mask[[i, j]] = if fg_energy < bg_energy { 1.0 } else { 0.0 };
            }
        }

        // Check convergence
        let change = (&mask - &old_mask).map(|x| x.abs()).sum();
        if change < config.convergence_threshold {
            break;
        }
    }

    // Convert to tensor
    array2_to_tensor(&mask)
}

/// Simple color model (Gaussian mixture approximation)
type ColorModel = (Array3<f32>, Array3<f32>); // (mean, std) for each channel

/// Build color model from seeds
fn build_color_model(image: &Array3<f32>, seeds: &[(usize, usize)]) -> Result<ColorModel> {
    if seeds.is_empty() {
        return Err(VisionError::InvalidParameter(
            "Seeds cannot be empty".to_string(),
        ));
    }

    let channels = image.shape()[2];
    let mut mean = Array3::zeros((1, 1, channels));
    let mut std = Array3::zeros((1, 1, channels));

    // Compute mean
    for &(i, j) in seeds {
        for c in 0..channels {
            mean[[0, 0, c]] += image[[i, j, c]];
        }
    }
    mean /= seeds.len() as f32;

    // Compute standard deviation
    for &(i, j) in seeds {
        for c in 0..channels {
            let diff = image[[i, j, c]] - mean[[0, 0, c]];
            std[[0, 0, c]] += diff * diff;
        }
    }
    std /= seeds.len() as f32;
    std.mapv_inplace(|x| x.sqrt().max(0.01)); // Avoid division by zero

    Ok((mean, std))
}

/// Compute Gaussian likelihood
fn compute_likelihood(pixel: &scirs2_core::ndarray::ArrayView1<f32>, model: &ColorModel) -> f32 {
    let (mean, std) = model;
    let mut likelihood = 1.0;

    for c in 0..pixel.len() {
        let diff = pixel[c] - mean[[0, 0, c]];
        let variance = std[[0, 0, c]] * std[[0, 0, c]];
        let exp_term = (-0.5 * diff * diff / variance).exp();
        likelihood *= exp_term / (std[[0, 0, c]] * (2.0 * std::f32::consts::PI).sqrt());
    }

    likelihood
}

/// Region growing segmentation configuration
#[derive(Debug, Clone)]
pub struct RegionGrowingConfig {
    /// Connectivity type
    pub connectivity: Connectivity,
    /// Intensity threshold for region membership
    pub intensity_threshold: f32,
    /// Whether to use adaptive thresholding
    pub adaptive: bool,
    /// Maximum region size (0 = unlimited)
    pub max_region_size: usize,
}

impl Default for RegionGrowingConfig {
    fn default() -> Self {
        Self {
            connectivity: Connectivity::Eight,
            intensity_threshold: 10.0,
            adaptive: true,
            max_region_size: 0,
        }
    }
}

/// Region growing segmentation from seed points
///
/// Implements a region growing algorithm that expands from seed points
/// based on intensity similarity.
///
/// # Arguments
/// * `image` - Input grayscale image
/// * `seeds` - Seed points (i, j) coordinates
/// * `config` - Region growing configuration
///
/// # Returns
/// Labeled segmentation map
pub fn region_growing(
    image: &Tensor,
    seeds: &[(usize, usize)],
    config: &RegionGrowingConfig,
) -> Result<Tensor> {
    let array = tensor_to_array2(image)?;
    let (height, width) = array.dim();
    let mut labels = Array2::zeros((height, width));

    // Process each seed
    for (label, &(seed_i, seed_j)) in seeds.iter().enumerate() {
        if seed_i >= height || seed_j >= width {
            continue;
        }

        if labels[[seed_i, seed_j]] != 0.0 {
            continue; // Already labeled
        }

        let seed_value = array[[seed_i, seed_j]];
        let mut queue = VecDeque::new();
        queue.push_back((seed_i, seed_j));
        labels[[seed_i, seed_j]] = (label + 1) as f32;

        let mut region_size = 1;
        let max_size = if config.max_region_size == 0 {
            usize::MAX
        } else {
            config.max_region_size
        };

        // Grow region
        while let Some((i, j)) = queue.pop_front() {
            if region_size >= max_size {
                break;
            }

            let _current_value = array[[i, j]];
            let threshold = if config.adaptive {
                // Adaptive threshold based on local statistics
                let local_mean = compute_local_mean(&array.to_owned(), i, j, 3);
                config.intensity_threshold * (local_mean / 128.0).max(0.5)
            } else {
                config.intensity_threshold
            };

            // Check neighbors
            let neighbors = get_neighbors(i, j, height, width, &config.connectivity);
            for (ni, nj) in neighbors {
                if labels[[ni, nj]] == 0.0 {
                    let neighbor_value = array[[ni, nj]];

                    // Check intensity similarity
                    if (neighbor_value - seed_value).abs() < threshold {
                        labels[[ni, nj]] = (label + 1) as f32;
                        queue.push_back((ni, nj));
                        region_size += 1;

                        if region_size >= max_size {
                            break;
                        }
                    }
                }
            }
        }
    }

    // Convert to tensor
    array2_to_tensor(&labels)
}

/// Compute local mean in a neighborhood
fn compute_local_mean(array: &Array2<f32>, i: usize, j: usize, radius: usize) -> f32 {
    let (height, width) = array.dim();
    let mut sum = 0.0;
    let mut count = 0;

    let i_start = i.saturating_sub(radius);
    let i_end = (i + radius + 1).min(height);
    let j_start = j.saturating_sub(radius);
    let j_end = (j + radius + 1).min(width);

    for ni in i_start..i_end {
        for nj in j_start..j_end {
            sum += array[[ni, nj]];
            count += 1;
        }
    }

    if count > 0 {
        sum / count as f32
    } else {
        0.0
    }
}

// Helper functions for tensor conversion

fn tensor_to_array2(tensor: &Tensor) -> Result<ArrayView2<'_, f32>> {
    // Convert tensor to ndarray view
    // Note: This is a simplified placeholder - actual implementation would use proper tensor API
    let shape = tensor.shape();
    if shape.len() != 2 {
        return Err(VisionError::InvalidParameter(format!(
            "Expected 2D tensor, got {}D",
            shape.len()
        )));
    }

    // TODO: Implement proper tensor-to-array conversion
    // For now, return error indicating this needs tensor API implementation
    Err(VisionError::InvalidParameter(
        "Tensor-to-array conversion not yet implemented".to_string(),
    ))
}

fn tensor_to_array3(tensor: &Tensor) -> Result<Array3<f32>> {
    // Convert tensor to ndarray view
    let shape = tensor.shape();
    if shape.len() != 3 {
        return Err(VisionError::InvalidParameter(format!(
            "Expected 3D tensor, got {}D",
            shape.len()
        )));
    }

    // TODO: Implement proper tensor-to-array conversion
    Err(VisionError::InvalidParameter(
        "Tensor-to-array conversion not yet implemented".to_string(),
    ))
}

fn array2_to_tensor(_array: &Array2<f32>) -> Result<Tensor> {
    // Convert ndarray to tensor
    // TODO: Implement proper array-to-tensor conversion
    Err(VisionError::InvalidParameter(
        "Array-to-tensor conversion not yet implemented".to_string(),
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{array, Array2};

    #[test]
    fn test_get_neighbors_4connectivity() {
        let neighbors = get_neighbors(5, 5, 10, 10, &Connectivity::Four);
        assert_eq!(neighbors.len(), 4);
        assert!(neighbors.contains(&(4, 5)));
        assert!(neighbors.contains(&(6, 5)));
        assert!(neighbors.contains(&(5, 4)));
        assert!(neighbors.contains(&(5, 6)));
    }

    #[test]
    fn test_get_neighbors_8connectivity() {
        let neighbors = get_neighbors(5, 5, 10, 10, &Connectivity::Eight);
        assert_eq!(neighbors.len(), 8);
    }

    #[test]
    fn test_get_neighbors_boundary() {
        let neighbors = get_neighbors(0, 0, 10, 10, &Connectivity::Four);
        assert_eq!(neighbors.len(), 2); // Only right and down
    }

    #[test]
    fn test_watershed_config_default() {
        let config = WatershedConfig::default();
        assert_eq!(config.connectivity, Connectivity::Eight);
        assert_eq!(config.compactness, 0.0);
        assert!(!config.return_watershed_lines);
    }

    #[test]
    fn test_graph_cuts_config_default() {
        let config = GraphCutsConfig::default();
        assert_eq!(config.spatial_weight, 1.0);
        assert_eq!(config.max_iterations, 100);
    }

    #[test]
    fn test_region_growing_config_default() {
        let config = RegionGrowingConfig::default();
        assert_eq!(config.connectivity, Connectivity::Eight);
        assert_eq!(config.intensity_threshold, 10.0);
        assert!(config.adaptive);
    }

    #[test]
    fn test_generate_watershed_markers() {
        let image = Array2::from_shape_fn((10, 10), |(i, j)| {
            ((i as f32 - 5.0).powi(2) + (j as f32 - 5.0).powi(2)).sqrt()
        });

        let markers =
            generate_watershed_markers(&image.view(), 2).expect("Failed to generate markers");

        // Should find at least one local minimum (center)
        let num_markers = markers.iter().filter(|&&x| x > 0).count();
        assert!(num_markers >= 1);
    }

    #[test]
    fn test_compute_local_mean() {
        let array = Array2::from_elem((10, 10), 5.0);
        let mean = compute_local_mean(&array, 5, 5, 2);
        assert!((mean - 5.0).abs() < 1e-5);
    }
}