[][src]Function imageproc::morphology::erode

pub fn erode(image: &GrayImage, norm: Norm, k: u8) -> GrayImage

Sets all pixels within distance k of a background pixel to black.

A pixel is treated as belonging to the foreground if it has non-zero intensity.

Examples

use image::GrayImage;
use imageproc::morphology::erode;
use imageproc::distance_transform::Norm;

let image = gray_image!(
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0, 255, 255, 255, 255, 255, 255, 255,  0;
    0, 255, 255, 255, 255, 255, 255, 255,  0;
    0, 255, 255, 255, 255, 255, 255, 255,  0;
    0, 255, 255, 255,   0, 255, 255, 255,  0;
    0, 255, 255, 255, 255, 255, 255, 255,  0;
    0, 255, 255, 255, 255, 255, 255, 255,  0;
    0, 255, 255, 255, 255, 255, 255, 255,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0
);

// L1 norm - the outermost foreground pixels are eroded,
// as well as those horizontally and vertically adjacent
// to the centre background pixel.
let l1_eroded = gray_image!(
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0,   0, 255, 255, 255, 255, 255,   0,  0;
    0,   0, 255, 255,   0, 255, 255,   0,  0;
    0,   0, 255,   0,   0,   0, 255,   0,  0;
    0,   0, 255, 255,   0, 255, 255,   0,  0;
    0,   0, 255, 255, 255, 255, 255,   0,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0
);

assert_pixels_eq!(erode(&image, Norm::L1, 1), l1_eroded);

// LInf norm - all pixels eroded using the L1 norm are eroded,
// as well as the pixels diagonally adjacent to the centre pixel.
let linf_eroded = gray_image!(
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0,   0, 255, 255, 255, 255, 255,   0,  0;
    0,   0, 255,   0,   0,   0, 255,   0,  0;
    0,   0, 255,   0,   0,   0, 255,   0,  0;
    0,   0, 255,   0,   0,   0, 255,   0,  0;
    0,   0, 255, 255, 255, 255, 255,   0,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0;
    0,   0,   0,   0,   0,   0,   0,   0,  0
);

assert_pixels_eq!(erode(&image, Norm::LInf, 1), linf_eroded);