Skip to main content

lib_jsl_dsp/discrete/image/
boundary.rs

1//! Boundary modes for the `ndimage`-style routines (rank filters, morphology, derivative filters
2//! and geometric transforms), with SciPy's names and meanings.
3//!
4//! For a row `a b c d`, the modes extend it as:
5//!
6//! - `Reflect`: `d c b a` before and `d c b a` after (the edge sample is repeated);
7//! - `Mirror`: `d c b` before and `c b a` after (reflection about the centre of the edge sample);
8//! - `Nearest`: `a a a` before and `d d d` after;
9//! - `Constant(k)`: `k k k` on both sides;
10//! - `Wrap`: `b c d` before and `a b c` after.
11
12/// How values beyond the edge of an image are supplied (SciPy's `mode`).
13#[derive(Clone, Copy, Debug, PartialEq)]
14pub enum Mode {
15    /// Reflect about the edge, repeating the edge sample (SciPy's default for filters).
16    Reflect,
17    /// Reflect about the centre of the edge sample, without repeating it.
18    Mirror,
19    /// Repeat the edge sample.
20    Nearest,
21    /// A constant value.
22    Constant(f64),
23    /// Periodic extension.
24    Wrap,
25}
26
27/// Maps an index into `0..n` for a mode; `None` means "outside, use the constant".
28pub fn mode_index(i: isize, n: usize, mode: Mode) -> Option<usize> {
29    let ni = n as isize;
30    if (0..ni).contains(&i) {
31        return Some(i as usize);
32    }
33    match mode {
34        Mode::Constant(_) => None,
35        Mode::Nearest => Some(i.clamp(0, ni - 1) as usize),
36        Mode::Wrap => Some(i.rem_euclid(ni) as usize),
37        Mode::Reflect => {
38            let m = i.rem_euclid(2 * ni);
39            Some(if m < ni { m as usize } else { (2 * ni - 1 - m) as usize })
40        }
41        Mode::Mirror => {
42            if n == 1 {
43                return Some(0);
44            }
45            let period = 2 * ni - 2;
46            let m = i.rem_euclid(period);
47            Some(if m < ni { m as usize } else { (period - m) as usize })
48        }
49    }
50}
51
52/// The value at `(r, c)` of an image extended by `mode`.
53pub fn sample(image: &ndarray::Array2<f64>, r: isize, c: isize, mode: Mode) -> f64 {
54    let (rows, cols) = image.dim();
55    match (mode_index(r, rows, mode), mode_index(c, cols, mode)) {
56        (Some(i), Some(j)) => image[(i, j)],
57        _ => match mode {
58            Mode::Constant(k) => k,
59            _ => unreachable!("only the constant mode leaves the image"),
60        },
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn the_extensions_of_a_b_c_d() {
70        let ext = |mode| (-3..7).map(|i| mode_index(i, 4, mode).map_or(-1, |j| j as i32)).collect::<Vec<i32>>();
71        assert_eq!(ext(Mode::Reflect), vec![2, 1, 0, 0, 1, 2, 3, 3, 2, 1]);
72        assert_eq!(ext(Mode::Mirror), vec![3, 2, 1, 0, 1, 2, 3, 2, 1, 0]);
73        assert_eq!(ext(Mode::Nearest), vec![0, 0, 0, 0, 1, 2, 3, 3, 3, 3]);
74        assert_eq!(ext(Mode::Wrap), vec![1, 2, 3, 0, 1, 2, 3, 0, 1, 2]);
75        assert_eq!(ext(Mode::Constant(0.0)), vec![-1, -1, -1, 0, 1, 2, 3, -1, -1, -1]);
76    }
77}