Skip to content

Orfeo Toolbox (OTB)

Installing and Loading the Orfeo Toolbox

Installing Orfeo Toolbox

Before using Orfeo Toolbox in R, you need to install the Orfeo Toolbox library on your system. The installation process may vary depending on your operating system.

Installing and Loading the RStoolbox Package in R

The RStoolbox package provides an interface to some of the Orfeo Toolbox functionalities.

# Install the RStoolbox package
install.packages("RStoolbox")

# Load the RStoolbox package
library(RStoolbox)

Reading and Writing Data

Reading Raster Data

# Read a raster file
raster <- raster("path/to/raster/file.tif")
print(raster)

Writing Raster Data

# Write a raster to a file
writeRaster(raster, "output_raster.tif", overwrite=TRUE)

Image Preprocessing

Image Normalization

# Normalize raster data
normalized <- normImage(raster)

Principal Component Analysis (PCA)

# Perform PCA on raster data
pca_result <- rasterPCA(raster)

Image Enhancement

# Enhance contrast of raster data
enhanced <- stretch(raster, minq=0.02, maxq=0.98)

Image Classification

Unsupervised Classification (K-means)

# Perform K-means classification
kmeans_result <- unsuperClass(raster, nSamples=1000, nClasses=5)

Supervised Classification (Random Forest)

# Perform supervised classification using Random Forest
trainingData <- read.csv("path/to/training/data.csv")
rf_model <- superClass(raster, trainData=trainingData, responseCol="class", model="rf")

Accuracy Assessment

Confusion Matrix

# Create a confusion matrix for classification results
referenceData <- read.csv("path/to/reference/data.csv")
conf_matrix <- validateMap(rf_model$map, valData=referenceData, responseCol="class")

Overall Accuracy

# Calculate overall accuracy
overall_acc <- conf_matrix$overallAccuracy
print(overall_acc)

Change Detection

Change Detection Using Difference

# Perform change detection using difference
raster1 <- raster("path/to/raster1.tif")
raster2 <- raster("path/to/raster2.tif")
change <- raster2 - raster1

Change Vector Analysis

# Perform change vector analysis
cva_result <- cva(raster1, raster2)

Visualization

Plotting Rasters

# Plot a single raster layer
plot(raster)

# Plot a raster stack
plot(stack)

Plotting Classification Results

# Plot classification map
plot(rf_model$map)

# Plot classification probability map
plot(rf_model$map$probability)

Plotting PCA Results

# Plot PCA results
plot(pca_result$map)

Advanced Analysis

Texture Analysis

# Perform texture analysis
texture <- glcm(raster, window=c(3,3), statistics="mean")

Spectral Indices

# Calculate NDVI
ndvi <- spectralIndices(raster, red="band3", nir="band4", index="NDVI")

Anomaly Detection

# Perform anomaly detection
anomalies <- anomalyDetection(raster, method="mad")