removeBackground {colordistance}R Documentation

Remove background pixels in image

Description

Take an image array (from readPNG or jpeg{readJPEG}) and remove the background pixels based on transparency (if a PNG with transparency) or color boundaries.

Usage

removeBackground(
  img,
  lower = NULL,
  upper = NULL,
  quietly = FALSE,
  alpha.channel = TRUE
)

Arguments

img

Image array, either output from readPNG or jpeg{readJPEG}.

lower, upper

RGB or HSV triplets specifying the bounds for background pixels. See loadImage.

quietly

Logical. Display a message if using transparency?

alpha.channel

Logical. If available, should alpha channel transparency be used to mask background? See details.

Details

If alpha.channel = TRUE, transparency takes precedence over color masking. If you provide a PNG with any pixels with alpha < 1, removeBackground ignores any lower and upper color boundaries and assumes transparent pixels are background. If all pixels are opaque (alpha = 1), color masking will apply.

Value

A list with a 3-dimensional RGB array and a 2-dimensional array of non-background pixels with R, G, B columns.

Examples


# remove background by transparency
img_path <- system.file("extdata/chrysochroa_NPL.png",
 package = "colordistance")
 
img_array <- png::readPNG(img_path)

img_filtered <- removeBackground(img_array)

# remove background by color
img_path <- dir(system.file("extdata/Heliconius", 
package = "colordistance"), 
recursive = TRUE, full.names = TRUE)[1]

img_array <- jpeg::readJPEG(img_path)

img_filtered <- removeBackground(img_array,
lower = rep(0.8, 3), upper = rep(1, 3))


[Package colordistance version 1.1.2 Index]