Learn R Programming

imager (version 0.45.8)

clean: Clean up and fill in pixel sets (morphological opening and closing)

Description

Cleaning up a pixel set here means removing small isolated elements (speckle). Filling in means removing holes. Cleaning up can be achieved by shrinking the set (removing speckle), followed by growing it back up. Filling in can be achieved by growing the set (removing holes), and shrinking it again.

Usage

clean(px, ...)

fill(px, ...)

Arguments

px

a pixset

...

parameters that define the structuring element to use, passed on to "grow" and "shrink"

Functions

  • fill(): Fill in holes using morphological closing

Author

Simon Barthelme

Examples

Run this code
cimg.limit.openmp()
im <- load.example("birds") %>% grayscale
sub <- imsub(-im,y> 380) %>% threshold("85%")
plot(sub)
#Turn into a pixel set
px <- sub==1
layout(t(1:2))
plot(px,main="Before clean-up")
clean(px,3) %>% plot(main="After clean-up")
#Now fill in the holes
px <- clean(px,3)
plot(px,main="Before filling-in")
fill(px,28) %>% plot(main="After filling-in")

Run the code above in your browser using DataLab