Skip to contents

Reduce a color, color near-infrared, or grayscale images to a binary image using a given color channel (red, green blue) or even color indexes. The Otsu's thresholding method (Otsu, 1979) is used to automatically perform clustering-based image thresholding.

Usage

image_binary(
  img,
  index = "R",
  r = 1,
  g = 2,
  b = 3,
  re = 4,
  nir = 5,
  return_class = "ebimage",
  threshold = c("Otsu", "adaptive"),
  k = 0.1,
  windowsize = NULL,
  has_white_bg = FALSE,
  resize = FALSE,
  fill_hull = FALSE,
  filter = FALSE,
  invert = FALSE,
  plot = TRUE,
  nrow = NULL,
  ncol = NULL,
  parallel = FALSE,
  workers = NULL,
  verbose = TRUE
)

Arguments

img

An image object.

index

A character value (or a vector of characters) specifying the target mode for conversion to binary image. See the available indexes with pliman_indexes() and image_index() for more details.

r, g, b, re, nir

The red, green, blue, red-edge, and near-infrared bands of the image, respectively. Defaults to 1, 2, 3, 4, and 5, respectively. If a multispectral image is provided (5 bands), check the order of bands, which are frequently presented in the 'BGR' format.

return_class

The class of object to be returned. If "terra returns a SpatRaster object with the number of layers equal to the number of indexes computed. If "ebimage" (default) returns a list of Image objects, where each element is one index computed.

threshold

The theshold method to be used.

  • By default (threshold = "Otsu"), a threshold value based on Otsu's method is used to reduce the grayscale image to a binary image. If a numeric value is informed, this value will be used as a threshold.

  • If threshold = "adaptive", adaptive thresholding (Shafait et al. 2008) is used, and will depend on the k and windowsize arguments.

  • If any non-numeric value different than "Otsu" and "adaptive" is used, an iterative section will allow you to choose the threshold based on a raster plot showing pixel intensity of the index.

k

a numeric in the range 0-1. when k is high, local threshold values tend to be lower. when k is low, local threshold value tend to be higher.

windowsize

windowsize controls the number of local neighborhood in adaptive thresholding. By default it is set to 1/3 * minxy, where minxy is the minimum dimension of the image (in pixels).

has_white_bg

Logical indicating whether a white background is present. If TRUE, pixels that have R, G, and B values equals to 1 will be considered as NA. This may be useful to compute an image index for objects that have, for example, a white background. In such cases, the background will not be considered for the threshold computation.

resize

Resize the image before processing? Defaults to FALSE. Use a numeric value as the percentage of desired resizing. For example, if resize = 30, the resized image will have 30% of the size of original image.

fill_hull

Fill holes in the objects? Defaults to FALSE.

filter

Performs median filtering in the binary image? (Defaults to FALSE). Provide a positive integer > 1 to indicate the size of the median filtering. Higher values are more efficient to remove noise in the background but can dramatically impact the perimeter of objects, mainly for irregular perimeters such as leaves with serrated edges.

invert

Inverts the binary image, if desired.

plot

Show image after processing?

nrow, ncol

The number of rows or columns in the plot grid. Defaults to NULL, i.e., a square grid is produced.

parallel

Processes the images asynchronously (in parallel) in separate R sessions running in the background on the same machine. It may speed up the processing time when image is a list. The number of sections is set up to 70% of available cores.

workers

A positive numeric scalar or a function specifying the maximum number of parallel processes that can be active at the same time.

verbose

If TRUE (default) a summary is shown in the console.

Value

A list containing binary images. The length will depend on the number of indexes used.

References

Otsu, N. 1979. Threshold selection method from gray-level histograms. IEEE Trans Syst Man Cybern SMC-9(1): 62–66. doi:10.1109/tsmc.1979.4310076

Shafait, F., D. Keysers, and T.M. Breuel. 2008. Efficient implementation of local adaptive thresholding techniques using integral images. Document Recognition and Retrieval XV. SPIE. p. 317–322 doi:10.1117/12.767755

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

library(pliman)
img <- image_pliman("soybean_touch.jpg")
image_binary(img, index = c("R, G"))