im.pca: Perform Principal Component Analysis (PCA) on a Raster Image

View source: R/im.pca.R

im.pcaR Documentation

Perform Principal Component Analysis (PCA) on a Raster Image

Description

This function applies Principal Component Analysis (PCA) to a multispectral raster image, extracting principal components from a sample of pixels and projecting the full raster into the PCA space.

Usage

im.pca(input_image, n_samples = 100, n_components = 3, plot = TRUE)

Arguments

input_image

A 'SpatRaster' object representing the input multispectral image.

n_samples

An integer specifying the number of random samples used for PCA computation (default: 100).

n_components

Number of principal components to compute (default: 3).

plot

Logical. If 'TRUE', the resulting principal components are plotted. Default is 'TRUE'.

Details

Principal Component Analysis (PCA) is a statistical technique used to transform correlated raster bands into a set of orthogonal components, capturing the most variance in fewer bands.

- A sample of 'n_samples' pixels is used to compute the PCA transformation. - The full image is then projected onto the principal component space. - The output raster contains the selected principal components. - If 'plot = TRUE', the output is visualized using a 'viridis' color scale.

Value

A 'SpatRaster' object containing the computed principal components.

See Also

[im.import()], [im.ggplot()]

Examples

library(terra)

# Create a 3-band raster
r <- rast(nrows = 10, ncols = 10, nlyrs = 3)
values(r) <- runif(ncell(r) * 3)

# Perform PCA without plotting
pca_result <- im.pca(r, n_samples = 100, plot = FALSE)

# Plot the first principal component
plot(pca_result[[1]])


imageRy documentation built on March 8, 2026, 1:06 a.m.