Skip to contents
  • image_segment() reduces a color, color near-infrared, or grayscale images to a segmented image using a given color channel (red, green blue) or even color indexes (See image_index() for more details). The Otsu's thresholding method (Otsu, 1979) is used to automatically perform clustering-based image thresholding.

  • image_segment_iter() Provides an iterative image segmentation, returning the proportions of segmented pixels.

Usage

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

image_segment_iter(
  img,
  nseg = 2,
  index = NULL,
  invert = NULL,
  threshold = NULL,
  k = 0.1,
  windowsize = NULL,
  has_white_bg = FALSE,
  plot = TRUE,
  verbose = TRUE,
  nrow = NULL,
  ncol = NULL,
  parallel = FALSE,
  workers = NULL,
  ...
)

Arguments

img

An image object or a list of image objects.

index
  • For image_segment(), a character value (or a vector of characters) specifying the target mode for conversion to binary image. See the available indexes with pliman_indexes(). See image_index() for more details.

  • For image_segment_iter() a character or a vector of characters with the same length of nseg. It can be either an available index (described above) or any operation involving the RGB values (e.g., "B/R+G").

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.

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

col_background

The color of the segmented background. Defaults to NULL (white background).

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.

fill_hull

Fill holes in the objects? Defaults to FALSE.

filter

Performs median filtering in the binary image? See more at image_filter(). Defaults to FALSE. Use a positive integer to define the size of the median filtering. Larger values are effective at removing noise, but adversely affect edges.

invert

Inverts the binary image, if desired. For image_segmentation_iter() use a vector with the same length of nseg.

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.

nseg

The number of iterative segmentation steps to be performed.

...

Additional arguments passed on to image_segment().

Value

  • image_segment() returns list containing n objects where n is the number of indexes used. Each objects contains:

    • image an image with the RGB bands (layers) for the segmented object.

    • mask A mask with logical values of 0 and 1 for the segmented image.

  • image_segment_iter() returns a list with (1) a data frame with the proportion of pixels in the segmented images and (2) the segmented images.

References

Nobuyuki Otsu, "A threshold selection method from gray-level histograms". IEEE Trans. Sys., Man., Cyber. 9 (1): 62-66. 1979. doi:10.1109/TSMC.1979.4310076

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

library(pliman)
img <- image_pliman("soybean_touch.jpg", plot = TRUE)

image_segment(img, index = c("R, G, B"))


# adaptive thresholding