View source: R/backgroundIndex.R
backgroundIndex | R Documentation |
Largely internal function for identifying, indexing, and removing background pixels from an image.
backgroundIndex(img, bg_condition)
img |
An image array, preferably the output of |
bg_condition |
Background condition, output of
|
This function flattens a 3-channel image into a 2D matrix before indexing and
removing background pixels to take advantage of faster indexing procedures.
The idx
, idx_flat
, and img_dims
elements are used to reconstruct the
original and recolored images by other functions.
A list with the following elements:
flattened_img
: The original image, flattened into a 2D matrix
(rows = pixels, columns = channels).
img_dims
: Dimensions of the original image.
non_bg
: Pixels from flattened_img
that fall outside the
background masking conditions. Used for further color clustering and
analysis.
idx
: 2D (row-column) indices for background pixels.
idx_flat
: Same as idx
, but flattened to vector order.
# get image path and read in image
img_path <- system.file("extdata/chongi.png", package = "recolorize")
img <- png::readPNG(img_path)
recolorize::plotImageArray(img)
# generate a white background condition
bg_condition <- backgroundCondition(lower = rep(0.9, 3),
upper = rep(1, 3))
# index background pixels
bg_indexed <- backgroundIndex(img, bg_condition)
# we can reconstruct the original image from the flattened array
img2 <- bg_indexed$flattened_img
dim(img2) <- bg_indexed$img_dims
# notice the original background color (light gray) now shows
recolorize::plotImageArray(img2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.