Learn R Programming

countcolors (version 0.9.1)

rectangularRange: Find pixels within a target color range defined by boundaries in each channel

Description

Searches for pixels within a set of upper and lower limits for each color channel. Essentially draws a 'box' around a region of color space in which to search for pixels.

Usage

rectangularRange(pixel.array, upper, lower, target.color = "green",
  main = "", color.pixels = TRUE, plotting = TRUE)

Arguments

pixel.array

An image represented as a 3D array (as read in by readJPEG, readPNG, or loadImage) in which to change pixel colors.

upper, lower

RGB triplet specifying the bounds of color space search. See details.

target.color

Color with which to replace specified pixels. Can be either an RGB triplet or one of the colors listed by colors.

main

Optional title to display for image.

color.pixels

Logical. Should a diagnostic image with pixels changed to target.color be returned?

plotting

Logical. Should output be plotted in the plot window?

Value

A list with the following elements:

  • pixel.idx: Coordinates of pixels within color range.

  • img.fraction: Proportion of the image within color range.

  • original.img: The original RGB array.

  • indicator.img: If color.pixels = TRUE, RGB array with color-swapped pixels.

Details

lower and upper should be vectors of length 3 in a 0-1 range, in the order R-G-B. For example, the upper bounds for white would be c(1, 1, 1), and the lower bounds might be c(0.8, 0.8, 0.8). This would search for all pixels where the red value, blue value, AND green value are all between 0.8 and 1.

Examples

Run this code
# NOT RUN {
flowers <- jpeg::readJPEG(system.file("extdata", "flowers.jpg", package =
"countcolors"))

# Define upper and lower bounds for white
lower <- rep(0.8, 3)
upper <- rep(1, 3)

white.flowers <- countcolors::rectangularRange(flowers, rep(1, 3), rep(0.85,
3), target.color = "turquoise")

# }

Run the code above in your browser using DataLab