Learn R Programming

Rvision (version 0.4.2)

connectedComponents: Find Connected Components in a binary Image

Description

connectedComponents computes the connected components (i.e. areas of contiguous non-zero pixels) of a binary image.

Usage

connectedComponents(image, connectivity = 8)

Arguments

image

An an 8-bit (8U) single-channel Image object.

connectivity

The connetivity neighborhood to decide whether 2 pixels are contiguous. This parameter can take two values:

  • 4: the neighborhood of a pixel are the four pixels located above (north), below (south), to the left (west) and right (east) of the pixel.

  • 8 (the default): the neighborhood of a pixel includes the four 4-neighbors and the four pixels along the diagonal directions (northeast, northwest, southeast, and southwest).

Value

A list with 2 (or 3) items:

  • n: the number of connected components in the image.

  • labels: a 16-bit (16U) single-channel image in which each pixel of each connected component is represented by the identity number of the component, and the background pixels by zero.

  • table (if `return_table = TRUE`): a dataframe with 3 columns representing the identity of the connected components (id), and the x-y coordinates of the pixels they are composed of.

See Also

Image

Examples

Run this code
# NOT RUN {
dots <- image(system.file("sample_img/dots.jpg", package = "Rvision"))
dots_gray <- changeColorSpace(dots, "GRAY")
dots_bin <- dots_gray < 200
cc <- connectedComponents(dots_bin)
cc_mat <- as.matrix(cc$labels)
colors <- c("black", rainbow(cc$n))
cc_img <- image(array(t(col2bgr(colors[cc_mat + 1])), dim = dim(dots)))
plot(cc_img)

# }

Run the code above in your browser using DataLab