Getting Started with hyperspectR

R-CMD-check pkgdown Codecov test coverage License: MIT Lifecycle: experimental

Installation

# From a local source checkout, with R >= 4.1.0
install.packages("remotes", repos = "https://cloud.r-project.org")
remotes::install_local(".", dependencies = NA, upgrade = "never",
                       build_vignettes = FALSE)

Loading the Package

library(hyperspectR)

Creating and Inspecting a Cube

hyperspectR provides synthetic data generators for testing and demonstration. hs_example_cube() creates a 30x30 pixel tissue scene with 61 spectral bands spanning 430-910 nm:

cube <- hs_example_cube()
print(cube)
#> 
#> ── hsi_cube ────────────────────────────────────────────────────────────────────
#> Dimensions: 30 rows x 30 cols x 61 bands
#> Wavelengths: 430-910 nm (61 bands)
#> FWHM: 25 nm (mean)
#> Mask: 900/900 valid pixels (100%)
#> Data range: [0.0198, 0.7306]
#> Metadata: camera, spectral_model, interpretation, processing_mode,
#> acquisition_time, region_map, sto2_ground_truth, seed

Check the dimensions:

dim(cube)
#> [1] 30 30 61

The summary provides per-band statistics:

s <- summary(cube)
s$wavelength_range
#> [1] 430 910
s$data_range
#> [1] 0.01982067 0.73056093

Subsetting

You can subset cubes spatially and spectrally while preserving the class:

sub <- cube[1:15, 1:15, 1:30]
dim(sub)
#> [1] 15 15 30

Visualization

RGB Composite

ggplot2::autoplot(cube, type = "rgb")

RGB composite of the synthetic 30 by 30 pixel example cube built from the bands nearest 640, 550 and 460 nm; rows run downwards and columns to the right.

Single Band Image

hs_plot_image(cube, wavelength = 550)

Pseudocolour image of the band nearest 550 nm in the example cube, with a viridis colour bar for the reflectance value.

Mean Spectrum

hs_plot_spectra(cube, pixels = "mean", show_sd = TRUE)

Mean reflectance spectrum of the example cube from 430 to 910 nm with a shaded band of one standard deviation; reflectance dips between 540 and 580 nm and rises towards the near infrared.

Computing Tissue Indices

The package provides a research haemoglobin fraction estimate and dimensionless band ratios. These outputs are not validated clinical measurements:

sto2 <- hs_sto2(cube)
hs_plot_index(sto2, title = "Fitted Hb fraction (%)", palette = "sto2")

Map of the fitted oxygenated haemoglobin fraction in percent; the four quadrants of the synthetic scene take four distinct levels on a blue-to-red scale.

Clinical Panel Display

hs_plot_clinical(cube, indices = c("sto2", "npi", "thi"))

Research panel in a two-by-two grid: RGB composite, fitted haemoglobin fraction map, near-infrared band ratio map and haemoglobin-band ratio map of the synthetic scene, each with rows and columns as axes and its own colour bar.

Interactive Exploration

Launch the Shiny app for interactive analysis:

hs_run_app(cube)

Next Steps

  • Read real data with hs_read_envi() or hs_read_cube()
  • Preprocess with hs_smooth(), hs_snv(), hs_msc()
  • Perform PCA with hs_pca() or MNF with hs_mnf()
  • Classify with hs_sam() or fit chromophores with hs_beer_lambert()

Citation

To cite hyperspectR in publications, please use:

@Manual{,
  title = {hyperspectR: Hyperspectral Imaging Analysis for Biomedical Applications},
  author = {R. Heller and V. Forstmeier},
  year = {2026},
  note = {R package version 0.3.0},
  url = {https://github.com/CTTIR/hyperspectR},
}