Skip to contents

Segments image objects using clustering by the k-means clustering algorithm

Usage

image_segment_kmeans(
  img,
  bands = 1:3,
  nclasses = 2,
  invert = FALSE,
  filter = FALSE,
  fill_hull = FALSE,
  plot = TRUE
)

Arguments

img

An Image object.

bands

A numeric integer/vector indicating the RGB band used in the segmentation. Defaults to 1:3, i.e., all the RGB bands are used.

nclasses

The number of desired classes after image segmentation.

invert

Invert the segmentation? Defaults to FALSE. If TRUE the binary matrix is inverted.

filter

Applies a median filtering in the binary matrix? Defaults to FALSE. Use a numeric integer to indicate the size of the median filter.

fill_hull

Fill holes in the objects? Defaults to FALSE.

plot

Plot the segmented image?

Value

A list with the following values:

  • image The segmented image considering only two classes (foreground and background)

  • clusters The class of each pixel. For example, if ncluster = 3, clusters will be a two-way matrix with values ranging from 1 to 3. masks A list with the binary matrices showing the segmentation.

References

Hartigan, J. A. and Wong, M. A. (1979). Algorithm AS 136: A K-means clustering algorithm. Applied Statistics, 28, 100–108. doi:10.2307/2346830

Examples

img <- image_pliman("la_leaves.jpg", plot = TRUE)

seg <- image_segment_kmeans(img)

seg <- image_segment_kmeans(img, fill_hull = TRUE, invert = TRUE, filter = 10)