graph_based_image_segmentation/segmentation/
node_merging_threshold.rs

1use crate::graph::{ImageEdge, ImageNode};
2use crate::segmentation::NodeMerging;
3use std::cell::Cell;
4
5/// The original criterion described in
6///
7/// > D. Stutz, A. Hermans, B. Leibe.
8/// > Superpixels: An Evaluation of the State-of-the-Art.
9/// > Computer Vision and Image Understanding, 2018.
10#[derive(Debug, Clone, Copy)]
11pub struct NodeMergingThreshold {
12    /// The threshold.
13    c: f32,
14}
15
16impl NodeMergingThreshold {
17    /// # Arguments
18    ///
19    /// * `c` - The threshold.
20    pub fn new(c: f32) -> Self {
21        Self { c }
22    }
23}
24
25impl NodeMerging for NodeMergingThreshold {
26    fn should_merge(&self, s_n: &Cell<ImageNode>, s_m: &Cell<ImageNode>, e: &ImageEdge) -> bool {
27        let s_n = s_n.get();
28        let s_m = s_m.get();
29        debug_assert_ne!(s_m.id, s_n.id);
30
31        let threshold_n = s_n.max_w + self.c / s_n.n as f32;
32        let threshold_m = s_m.max_w + self.c / s_m.n as f32;
33
34        // Edge weight muss be smaller than both thresholds.
35        let threshold = threshold_n.min(threshold_m);
36        e.w < threshold
37    }
38}