Last data update: 2014.03.03

R: Connected Components Labelling - Unique Patch Labelling
ConnCompLabelR Documentation

Connected Components Labelling – Unique Patch Labelling

Description

ConnCompLabel is a 1 pass implementation of connected components labelling. Here it is applied to identify disjunt patches within a distribution.

The raster matrix can be a raster of class 'asc' (adehabitat package), 'RasterLayer' (raster package) or 'SpatialGridDataFrame' (sp package).

Usage

ConnCompLabel(mat)

Arguments

mat

is a binary matrix of data with 0 representing background and 1 representing environment of interest. NA values are acceptable. The matrix can be a raster of class 'asc' (this & adehabitat package), 'RasterLayer' (raster package) or 'SpatialGridDataFrame' (sp package)

Value

A matrix of the same dim and class of mat in which unique components (individual patches) are numbered 1:n with 0 remaining background value.

Author(s)

Jeremy VanDerWal jjvanderwal@gmail.com

References

Chang, F., C.-J. Chen, and C.-J. Lu. 2004. A linear-time component-labeling algorithm using contour tracing technique. Comput. Vis. Image Underst. 93:206-220.

See Also

PatchStat, ClassStat

Examples

#define a simple binary matrix
tmat = { matrix(c( 0,0,0,1,0,0,1,1,0,1,
                   0,0,1,0,1,0,0,0,0,0,
                   0,1,NA,1,0,1,0,0,0,1,
                   1,0,1,1,1,0,1,0,0,1,
                   0,1,0,1,0,1,0,0,0,1,
                   0,0,1,0,1,0,0,1,1,0,
                   1,0,0,1,0,0,1,0,0,1,
                   0,1,0,0,0,1,0,0,0,1,
                   0,0,1,1,1,0,0,0,0,1,
                   1,1,1,0,0,0,0,0,0,1),nr=10,byrow=TRUE) }

#do the connected component labelling
ccl.mat = ConnCompLabel(tmat)
ccl.mat
image(t(ccl.mat[10:1,]),col=c('grey',rainbow(length(unique(ccl.mat))-1)))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(SDMTools)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/SDMTools/ConnCompLabel.Rd_%03d_medium.png", width=480, height=480)
> ### Name: ConnCompLabel
> ### Title: Connected Components Labelling - Unique Patch Labelling
> ### Aliases: ConnCompLabel
> 
> ### ** Examples
> 
> #define a simple binary matrix
> tmat = { matrix(c( 0,0,0,1,0,0,1,1,0,1,
+                    0,0,1,0,1,0,0,0,0,0,
+                    0,1,NA,1,0,1,0,0,0,1,
+                    1,0,1,1,1,0,1,0,0,1,
+                    0,1,0,1,0,1,0,0,0,1,
+                    0,0,1,0,1,0,0,1,1,0,
+                    1,0,0,1,0,0,1,0,0,1,
+                    0,1,0,0,0,1,0,0,0,1,
+                    0,0,1,1,1,0,0,0,0,1,
+                    1,1,1,0,0,0,0,0,0,1),nr=10,byrow=TRUE) }
> 
> #do the connected component labelling
> ccl.mat = ConnCompLabel(tmat)
> ccl.mat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    1    0    0    2    2    0     3
 [2,]    0    0    1    0    1    0    0    0    0     0
 [3,]    0    1   NA    1    0    1    0    0    0     4
 [4,]    1    0    1    1    1    0    1    0    0     4
 [5,]    0    1    0    1    0    1    0    0    0     4
 [6,]    0    0    1    0    1    0    0    4    4     0
 [7,]    4    0    0    1    0    0    4    0    0     4
 [8,]    0    4    0    0    0    4    0    0    0     4
 [9,]    0    0    4    4    4    0    0    0    0     4
[10,]    4    4    4    0    0    0    0    0    0     4
> image(t(ccl.mat[10:1,]),col=c('grey',rainbow(length(unique(ccl.mat))-1)))
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>