Skip to contents

Largely internal function for identifying, indexing, and removing background pixels from an image.

Usage

backgroundIndex(img, bg_condition)

Arguments

img

An image array, preferably the output of png::readPNG(), jpeg::readJPEG(), or link[recolorize]{readImage}.

bg_condition

Background condition, output of backgroundCondition().

Value

A list with the following elements:

  1. flattened_img: The original image, flattened into a 2D matrix (rows = pixels, columns = channels).

  2. img_dims: Dimensions of the original image.

  3. non_bg: Pixels from flattened_img that fall outside the background masking conditions. Used for further color clustering and analysis.

  4. idx: 2D (row-column) indices for background pixels.

  5. idx_flat: Same as idx, but flattened to vector order.

Details

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.

Examples

# 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)