pub fn close(image: &GrayImage, norm: Norm, k: u8) -> GrayImage
Expand description
Dilation followed by erosion.
See the erode
and dilate
documentation for definitions of dilation and erosion.
ยงExamples
use imageproc::morphology::close;
use imageproc::distance_transform::Norm;
// Small holes are closed - hence the name.
let small_hole = gray_image!(
255, 255, 255, 255;
255, 0, 0, 255;
255, 0, 0, 255;
255, 255, 255, 255
);
let closed_small_hole = gray_image!(
255, 255, 255, 255;
255, 255, 255, 255;
255, 255, 255, 255;
255, 255, 255, 255
);
assert_pixels_eq!(
close(&small_hole, Norm::LInf, 1),
closed_small_hole
);
// Large holes survive unchanged.
let large_hole = gray_image!(
255, 255, 255, 255, 255;
255, 0, 0, 0, 255;
255, 0, 0, 0, 255;
255, 0, 0, 0, 255;
255, 255, 255, 255, 255
);
assert_pixels_eq!(
close(&large_hole, Norm::LInf, 1),
large_hole
);
// A dot gains a layer of foreground pixels
// when dilated and loses them again when eroded,
// resulting in no change.
let dot = gray_image!(
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 255, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0
);
assert_pixels_eq!(
close(&dot, Norm::LInf, 1),
dot
);
// A dot near the boundary gains pixels in the top-left
// of the image which are not within distance 1 of any
// background pixels, so are not removed by erosion.
let dot_near_boundary = gray_image!(
0, 0, 0, 0, 0;
0, 255, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0
);
let closed_dot_near_boundary = gray_image!(
255, 255, 0, 0, 0;
255, 255, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0
);
assert_pixels_eq!(
close(&dot_near_boundary, Norm::LInf, 1),
closed_dot_near_boundary
);