View source: R/img_extract_largest.R
img_extract_largest | R Documentation |
Extract the largest object from the image background.
img_extract_largest(x, threshold = 2/255, quiet = FALSE)
x |
a greyscale image, of type |
threshold |
grey value threshold to detect the object over the background. Either a number in |
quiet |
when TRUE, do not display the message about image inversion when providing an image on a light background. |
Threshold the input image, detect connected particles, and keep only the largest one. Note that, for ease of use, when the image has an obviously white/light background (based on img_guess_background()
), the image is automatically inverted. Indeed, thresholding select pixels whose value is above the threshold = it supposes that the object of interest has values higher (i.e. lighter) than the background, which is therefore supposed to be dark.
The extracted object, on a pure white background, as a imager::cimg()
object.
# get an image with lots of background space, several blobs and a legend
# NB: invert it so that the background is black
x <- img_read(system.file("extdata", "blob.jpg", package="morphr"), invert=TRUE)
img_show(x)
# extract its largest object
img_extract_largest(x) %>% img_show()
# now leave the image in its original dark-on-light aspect
x <- img_read(system.file("extdata", "blob.jpg", package="morphr"))
img_show(x)
# and extraction still works because the image is automatically inverted
img_extract_largest(x) %>% img_show()
# NB: use quiet=TRUE to suppress the message
# if we want to be sure to pick up the object itself and not the legend,
# we can start by chopping the legend away
x %>% img_chop(b=31) %>% img_extract_largest(quiet=TRUE) %>% img_show()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.