• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xd009642 / ndarray-vision / #58

pending completion
#58

push

web-flow
Fixed threshold docs (#55)

* Fix formatting for Otsu

* Fix Mean thresholding docs

* Fix capitalization

* Fix formatting

This might need to be changed to a `//` comment instead of documentation
since the function is private.

Co-authored-by: Christopher Field <chris.field@theiascientific.com>

774 of 1090 relevant lines covered (71.01%)

1.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/processing/filter.rs
1
use crate::core::{ColourModel, Image, ImageBase};
2
use ndarray::prelude::*;
3
use ndarray::{Data, IntoDimension, OwnedRepr, Zip};
4
use ndarray_stats::interpolate::*;
5
use ndarray_stats::Quantile1dExt;
6
use noisy_float::types::n64;
7
use num_traits::{FromPrimitive, Num, ToPrimitive};
8
use std::marker::PhantomData;
9

10
/// Median filter, given a region to move over the image, each pixel is given
11
/// the median value of itself and it's neighbours
12
pub trait MedianFilterExt {
13
    type Output;
14
    /// Run the median filter given the region. Median is assumed to be calculated
15
    /// independently for each channel.
16
    fn median_filter<E>(&self, region: E) -> Self::Output
17
    where
18
        E: IntoDimension<Dim = Ix2>;
19
}
20

21
impl<T, U> MedianFilterExt for ArrayBase<U, Ix3>
22
where
23
    U: Data<Elem = T>,
24
    T: Copy + Clone + FromPrimitive + ToPrimitive + Num + Ord,
25
{
26
    type Output = ArrayBase<OwnedRepr<T>, Ix3>;
27

28
    fn median_filter<E>(&self, region: E) -> Self::Output
2✔
29
    where
30
        E: IntoDimension<Dim = Ix2>,
31
    {
32
        let shape = region.into_dimension();
×
33
        let r_offset = shape[0] / 2;
2✔
34
        let c_offset = shape[1] / 2;
2✔
35
        let region = (shape[0], shape[1], 1);
4✔
36
        let mut result = Array3::<T>::zeros(self.dim());
2✔
37
        Zip::indexed(self.windows(region)).for_each(|(i, j, k), window| {
8✔
38
            let mut flat_window = Array::from_iter(window.iter()).mapv(|x| *x);
6✔
39
            if let Ok(v) = flat_window.quantile_mut(n64(0.5f64), &Linear {}) {
2✔
40
                result
6✔
41
                    .get_mut([i + r_offset, j + c_offset, k])
4✔
42
                    .map(|r| *r = v);
6✔
43
            }
44
        });
45
        result
×
46
    }
47
}
48

49
impl<T, U, C> MedianFilterExt for ImageBase<U, C>
50
where
51
    U: Data<Elem = T>,
52
    T: Copy + Clone + FromPrimitive + ToPrimitive + Num + Ord,
53
    C: ColourModel,
54
{
55
    type Output = Image<T, C>;
56

57
    fn median_filter<E>(&self, region: E) -> Self::Output
2✔
58
    where
59
        E: IntoDimension<Dim = Ix2>,
60
    {
61
        let data = self.data.median_filter(region);
2✔
62
        Image {
63
            data,
64
            model: PhantomData,
65
        }
66
    }
67
}
68

69
#[cfg(test)]
70
mod tests {
71
    use super::*;
72
    use crate::core::colour_models::{Gray, RGB};
73

74
    #[test]
75
    fn simple_median() {
76
        let mut pixels = Vec::<u8>::new();
77
        for i in 0..9 {
78
            pixels.extend_from_slice(&[i, i + 1, i + 2]);
79
        }
80
        let image = Image::<_, RGB>::from_shape_data(3, 3, pixels);
81

82
        let image = image.median_filter((3, 3));
83

84
        let mut expected = Image::<u8, RGB>::new(3, 3);
85
        expected.pixel_mut(1, 1).assign(&arr1(&[4, 5, 6]));
86

87
        assert_eq!(image, expected);
88
    }
89

90
    #[test]
91
    fn row_median() {
92
        let pixels = vec![1, 2, 3, 4, 5, 6, 7];
93
        let image = Image::<_, Gray>::from_shape_data(7, 1, pixels);
94
        let image = image.median_filter((3, 1));
95

96
        let expected_pixels = vec![0, 2, 3, 4, 5, 6, 0];
97
        let expected = Image::<_, Gray>::from_shape_data(7, 1, expected_pixels);
98

99
        assert_eq!(image, expected);
100
    }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc