[][src]Function imageproc::filter::median_filter

pub fn median_filter<P>(
    image: &Image<P>,
    x_radius: u32,
    y_radius: u32
) -> Image<P> where
    P: Pixel<Subpixel = u8> + 'static, 

Applies a median filter of given dimensions to an image. Each output pixel is the median of the pixels in a (2 * x_radius + 1) * (2 * y_radius + 1) kernel of pixels in the input image.

Pads by continuity. Performs O(max(x_radius, y_radius)) operations per pixel.

Examples

use imageproc::filter::median_filter;

let image = gray_image!(
    1,   2,   3;
  200,   6,   7;
    9, 100,  11
);

// Padding by continuity means that the values we use
// for computing medians of boundary pixels are:
//
//   1     1     2     3     3
//      -----------------
//   1 |   1     2     3 |   3
//
// 200 | 200     6     7 |   7
//
//   9 |   9   100    11 |  11
//      -----------------
//   9     9   100    11    11

let filtered = gray_image!(
    2,  3,  3;
    9,  7,  7;
    9, 11, 11
);

assert_pixels_eq!(median_filter(&image, 1, 1), filtered);
use imageproc::filter::median_filter;

// Image channels are handled independently.
// This example sets the red channel to have the same
// contents as the image from the grayscale example,
// the green channel to a vertically inverted copy of that
// image and the blue channel to be constant.
//
// See the grayscale image example for an explanation of how
// boundary conditions are handled.

let image = rgb_image!(
    [  1,   9, 10], [  2, 100,  10], [  3,  11,  10];
    [200, 200, 10], [  6,   6,  10], [  7,   7,  10];
    [  9,   1, 10], [100,   2,  10], [ 11,   3,  10]
);

let filtered = rgb_image!(
    [ 2,  9, 10], [ 3, 11, 10], [ 3, 11, 10];
    [ 9,  9, 10], [ 7,  7, 10], [ 7,  7, 10];
    [ 9,  2, 10], [11,  3, 10], [11,  3, 10]
);

assert_pixels_eq!(median_filter(&image, 1, 1), filtered);
use imageproc::filter::median_filter;

// This example uses a kernel with x_radius sets to 2
// and y_radius sets to 1, which leads to 5 * 3 kernel size.

let image = gray_image!(
    1, 2, 3, 4, 5;
    255, 200, 4, 11, 7;
    42, 17, 3, 2, 1;
    9, 100, 11, 13, 14;
    15, 87, 99, 21, 45
);
    
let filtered = gray_image!(
    2, 3, 4, 5, 5;
    17, 4, 4, 4, 4;
    42, 13, 11, 11, 7;
    15, 15, 15, 14, 14;
    15, 15, 21, 45, 45
);

assert_pixels_eq!(median_filter(&image, 2, 1), filtered);