Learn R Programming

spatstat.geom (version 3.7-0)

connected: Connected components

Description

Finds the topologically-connected components of a spatial object, such as the connected clumps of pixels in a binary image.

Usage

connected(X, ...)

# S3 method for owin connected(X, ..., polygonal=FALSE, method="C", connect=8)

# S3 method for im connected(X, ..., background, method="C", connect=8)

Arguments

Value

A pixel image (object of class "im") with factor values. The levels of the factor correspond to the connected components.

For connected.owin, if polygonal=TRUE, the result is a tessellation (object of class "tess") whose tiles are the connected components.

Details

The function connected is generic, with methods for pixel images (class "im") and windows (class "owin") described here. There are also methods for tessellations (connected.tess), point patterns (connected.ppp and connected.lpp), and linear networks (connected.linnet).

The functions described here compute the connected component transform (Rosenfeld and Pfalz, 1966) of a binary image or binary mask. The pixel values in X are first mapped to logical values. Then the algorithm identifies the connected components (topologically-connected clumps of pixels) in the foreground.

When X is converted to a logical valued image, the pixel value NA is always mapped to FALSE. If background is specified, then the value background is also mapped to FALSE. All other values are mapped to TRUE. In the special case where X is already a logical-valued image, the default is background=FALSE, so that the values NA and FALSE are mapped to FALSE.

Two pixels belong to the same connected component if they have the value TRUE and if they are neighbours. This rule is applied repeatedly until it terminates. Then each connected component contains all the pixels that can be reached by stepping from neighbour to neighbour.

Pixels are defined to be neighbours if they are physically adjacent to each other. If connect=4, each pixel has 4 neighbours, lying one step above or below, or one step to the left or right. If connect=8 (the default), each pixel has 8 neighbours, lying one step above or below, or one step to the left or right, or one diagonal step away. (Pixels at the edge of the image have fewer neighbours.) The 8-connected algorithm is the default because it gives better results when the pixel grid is coarse. The 4-connected algorithm is faster and is recommended when the pixel grid is fine.

If method="C", the computation is performed by a compiled C language implementation of the classical algorithm of Rosenfeld and Pfalz (1966). If method="interpreted", the computation is performed by an R implementation of the algorithm of Park et al (2000).

By default, the result is a factor-valued image, with levels that correspond to the connected components. The Examples show how to extract each connected component as a separate window object.

If X is a window and polygonal=TRUE, the result is a tessellation (object of class "tess") whose tiles are the connected components.

References

Park, J.-M., Looney, C.G. and Chen, H.-C. (2000) Fast connected component labeling algorithm using a divide and conquer technique. Pages 373-376 in S.Y. Shin (ed) Computers and Their Applications: Proceedings of the ISCA 15th International Conference on Computers and Their Applications, March 29-31, 2000, New Orleans, Louisiana USA. ISCA 2000, ISBN 1-880843-32-3.

Rosenfeld, A. and Pfalz, J.L. (1966) Sequential operations in digital processing. Journal of the Association for Computing Machinery 13 471-494.

See Also

connected.ppp, connected.tess, im.object, tess

Examples

Run this code
  d <- distmap(cells, dimyx=256)
  X <- levelset(d, 0.07)
  plot(X)
  Z <- connected(X)
  plot(Z)
  # or equivalently
  Z <- connected(d <= 0.07)

  # number of components
  nc <- length(levels(Z))
  # plot with randomised colour map
  plot(Z, col=hsv(h=sample(seq(0,1,length=nc), nc)))

  # how to extract the components as a list of windows
  W <- tiles(tess(image=Z))

  ## polygonal algorithm
  A <- regularpolygon(7)
  B <- regularpolygon(13, 0.3)
  D <- owin(c(-1, -0.6), c(1, 1.4))
  E <- regularpolygon(5, 0.3)
  Funky <- union.owin(D, E, setminus.owin(A, B))
  plot(Funky, col="blue")
  plot(connected(Funky, polygonal=TRUE), do.col=TRUE)

Run the code above in your browser using DataLab