Learn R Programming

RStoolbox (version 0.2.4)

unsuperClass: Unsupervised Classification

Description

Unsupervised clustering of Raster* data using kmeans clustering

Usage

unsuperClass(img, nSamples = 10000, nClasses = 5, nStarts = 25,
  nIter = 100, norm = FALSE, clusterMap = TRUE,
  algorithm = "Hartigan-Wong", ...)

Arguments

img

Raster* object.

nSamples

Integer. Number of random samples to draw to fit cluster map. Only relevant if clusterMap = TRUE.

nClasses

Integer. Number of classes.

nStarts

Integer. Number of random starts for kmeans algorithm.

nIter

Integer. Maximal number of iterations allowed.

norm

Logical. If TRUE will normalize img first using normImage. Normalizing is beneficial if your predictors have different scales.

clusterMap

Logical. Fit kmeans model to a random subset of the img (see Details).

algorithm

Character. kmeans algorithm. One of c("Hartigan-Wong", "Lloyd", "MacQueen")

...

further arguments to be passed to writeRaster, e.g. filename

Details

Clustering is done using kmeans. This can be done for all pixels of the image (clusterMap=FALSE), however this can be slow and is not memory safe. Therefore if you have large raster data (> memory), as is typically the case with remote sensing imagery it is advisable to choose clusterMap=TRUE (the default). This means that a kmeans cluster model is calculated based on a random subset of pixels (nSamples). Then the distance of *all* pixels to the cluster centers is calculated in a stepwise fashion using predict. Class assignment is based on minimum euclidean distance to the cluster centers.

The solution of the kmeans algorithm often depends on the initial configuration of class centers which is chosen randomly. Therefore, kmeans is usually run with multiple random starting configurations in order to find a convergent solution from different starting configurations. The nStarts argument allows to specify how many random starts are conducted.

Examples

Run this code
# NOT RUN {
library(raster)
input <- brick(system.file("external/rlogo.grd", package="raster"))

## Plot 
olpar <- par(no.readonly = TRUE) # back-up par
par(mfrow=c(1,2))
plotRGB(input)

## Run classification
set.seed(25)
unC <- unsuperClass(input, nSamples = 100, nClasses = 5, nStarts = 5)
unC

## Plots
colors <- rainbow(5)
plot(unC$map, col = colors, legend = FALSE, axes = FALSE, box = FALSE)
legend(1,1, legend = paste0("C",1:5), fill = colors,
       title = "Classes", horiz = TRUE,  bty = "n")

par(olpar) # reset par
# }

Run the code above in your browser using DataLab